安全上下文
此特征只可用于
安全上下文
(HTTPS),在某些或所有
支持浏览器
.
使用
generateKey()
方法在
SubtleCrypto
interface to generate a new key (for symmetric algorithms) or key pair (for public-key algorithms).
const result = crypto.subtle.generateKey(algorithm, extractable, keyUsages);
algorithm
is a dictionary object defining the type of key to generate and providing extra algorithm-specific parameters.
RsaHashedKeyGenParams
对象。
EcKeyGenParams
对象。
HmacKeyGenParams
对象。
AesKeyGenParams
对象。
extractable
是
布尔
indicating whether it will be possible to export the key using
SubtleCrypto.exportKey()
or
SubtleCrypto.wrapKey()
.
keyUsages
是
数组
indicating what can be done with the newly generated key. Possible values for array elements are:
encrypt
: The key may be used to
encrypt
messages.
decrypt
: The key may be used to
decrypt
messages.
sign
: The key may be used to
sign
messages.
verify
: The key may be used to
verify
signatures.
deriveKey
: The key may be used in
deriving a new key
.
deriveBits
: The key may be used in
deriving bits
.
wrapKey
: The key may be used to
wrap a key
.
unwrapKey
: The key may be used to
unwrap a key
.
result
是
Promise
that fulfills with a
CryptoKey
(for symmetric algorithms) or a
CryptoKeyPair
(for public-key algorithms).
The promise is rejected when the following exception is encountered:
SyntaxError
CryptoKey
类型
secret
or
private
but
keyUsages
is empty.
SyntaxError
CryptoKeyPair
及其
privateKey.usages
attribute is empty.
注意 : You can try the working examples on GitHub.
This code generates an RSA-OAEP encryption key pair. See the complete code on GitHub.
let keyPair = window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
true,
["encrypt", "decrypt"]
);
This code generates an ECDSA signing key pair. See the complete code on GitHub.
let keyPair = window.crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-384"
},
true,
["sign", "verify"]
);
This code generates an HMAC signing key. See the complete code on GitHub.
let key = window.crypto.subtle.generateKey(
{
name: "HMAC",
hash: {name: "SHA-512"}
},
true,
["sign", "verify"]
);
This code generates an AES-GCM encryption key. See the complete code on GitHub.
let key = window.crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256
},
true,
["encrypt", "decrypt"]
);
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web Cryptography API
The definition of 'SubtleCrypto.generateKey()' in that specification. |
推荐 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
generateKey
|
Chrome 37 |
Edge
部分支持
12
|
Firefox
34
|
IE
部分支持
11
|
Opera 24 | Safari 7 | WebView Android 37 | Chrome Android 37 |
Firefox Android
34
|
Opera Android 24 | Safari iOS 7 | Samsung Internet Android 6.0 |
完整支持
部分支持
见实现注意事项。
用户必须明确启用此特征。
SubtleCrypto