The SubtleCrypto.generateKey()
method returns a Promise
of a newly generated CryptoKey
, for symmetrical algorithms, or a CryptoKeyPair
, containing two newly generated keys, for asymmetrical algorithm, that matches the algorithm, the usages and the extractability given as parameters.
Syntax
var result = crypto.generateKey(algo, extractable, keyUsages)
;
Parameters
algo
is a dictionary object defining the key generation function to use. Supported algo are: AES-CBC,AES-CTR
,AES-GCM
,RSA-OAEP
,AES-KW
,HMAC
,RSASSA-PKCS1-v1_5
,ECDSA
,ECDH
, andDH
.extractable
is aBoolean
indicating if the key can be extracted from theCryptoKey
object at a later stage.keyUsages
is anArray
indicating what can be done with the newly generated 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
result
is aPromise
that returns the generated key as aCryptoKey
or aCryptoKeyPair
.
Exceptions
The promise is rejected when the following exception is encountered:
SyntaxError
whenkeyUsages
is empty but the generated symmetric key is of type"secret"
or"private"
or the generated private component of the generated asymmetric pair of key is empty.
Specifications
Specification | Status | Comment |
---|---|---|
Web Cryptography API The definition of 'SubtleCrypto.generateKey()' in that specification. |
Candidate Recommendation | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Edge | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 37 | 34 (34) | Not supported | 13 | Not supported | |
ECDSA |
(Yes) | 36 (36) | Not supported | ? | Not supported | |
DH |
Not supported | 35 (35) | Not supported | Not supported | Not supported |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | 37 | 34.0 (34) | Not supported | ? | Not supported |
ECDSA |
(Yes) | (Yes) | 36.0 (36) | Not supported | ? | Not supported |
DH |
Not supported | (Yes) | 35.0 (35) | Not supported | Not supported | Not supported |
See also
Crypto
andCrypto.subtle
.SubtleCrypto
, the interface it belongs to.