W3cubDocs

/DOM

SubtleCrypto.importKey

The SubtleCrypto.importKey() method returns a Promise of the CryptoKey generated from the data given in parameters.

Syntax

var result = crypto.subtle.importKey(format, keyData, algo, extractable, usages);

Parameters

  • format is an enumerated value describing the data format of the key to imported. It can be one of the following:
  • keyData is an ArrayBuffer or a JSONWebKey containing the key in the given format.
  • algo is a dictionary object defining the algorithm that was used to generate the key being imported.
  • extractable is a Boolean indicating if the key can be extracted from the CryptoKey object at a later stage.
  • usages is an Array indicating what can be done with the key. Possible values of the array are:
    • "encrypt", allowing the key to be used for encrypting messages.
    • "decrypt", allowing the key to be used for decrypting messages.
    • "sign", allowing the key to be used for signing messages.
    • "verify", allowing the key to be used for verifying the signature of messages.
    • "deriveKey", allowing the key to be used as a base key when deriving a new key.
    • "deriveBits", allowing the key to be used as a base key when deriving bits of data for use in cryptographic primitives.
    • "wrapKey", allowing the key to wrap a symmetric key for usage (transfer, storage) in unsecure environments.
    • "unwrapKey", allowing the key to unwrap a symmetric key for usage (transfer, storage) in unsecure environments.

Return value

Exceptions

The promise is rejected when one of the following exceptions is encountered:

  • SyntaxError when keyUsages is empty but the unwrapped key is of type "secret" or "private".
  • TypeError when trying to use an invalid format or if the keyData is not suited for that format.

Specifications

Specification Status Comment
Web Cryptography API
The definition of 'SubtleCrypto.importKey()' in that specification.
Recommendation Initial definition.

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 37 12 34
34
32 — 34
Disabled
Disabled From version 32 until version 34 (exclusive): this feature is behind the dom.webcrypto.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
11
11
Returns KeyOperation instead of Promise
24 7
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support 37 37 12 34
34
32 — 34
Disabled
Disabled From version 32 until version 34 (exclusive): this feature is behind the dom.webcrypto.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
24 7 6.0

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey