- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v20.20.6 文档
- Node.js v20.20.6
-
目录
- 网络加密 API
- 示例
- 算法矩阵
- 类:
Crypto - 类:
CryptoKey - 类:
CryptoKeyPair - 类:
SubtleCryptosubtle.decrypt(algorithm, key, data)subtle.deriveBits(algorithm, baseKey[, length])subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)subtle.digest(algorithm, data)subtle.encrypt(algorithm, key, data)subtle.exportKey(format, key)subtle.generateKey(algorithm, extractable, keyUsages)subtle.importKey(format, keyData, algorithm, extractable, keyUsages)subtle.sign(algorithm, key, data)subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgo, unwrappedKeyAlgo, extractable, keyUsages)subtle.verify(algorithm, key, signature, data)subtle.wrapKey(format, key, wrappingKey, wrapAlgo)
- 算法参数
- 类:
AlgorithmIdentifier - 类:
AesCbcParams - 类:
AesCtrParams - 类:
AesGcmParams - 类:
AesKeyGenParams - 类:
EcdhKeyDeriveParams - 类:
EcdsaParams - 类:
EcKeyGenParams - 类:
EcKeyImportParams - 类:
Ed448Params - 类:
HkdfParams - 类:
HmacImportParams - 类:
HmacKeyGenParams - 类:
Pbkdf2Params - 类:
RsaHashedImportParams - 类:
RsaHashedKeyGenParams - 类:
RsaOaepParams - 类:
RsaPssParams
- 类:
- 网络加密 API
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
网络加密 API#>
【Web Crypto API】
Node.js 提供了标准 网络加密 API 的实现。
【Node.js provides an implementation of the standard Web Crypto API.】
使用 globalThis.crypto 或 require('node:crypto').webcrypto 来访问此模块。
【Use globalThis.crypto or require('node:crypto').webcrypto to access this
module.】
const { subtle } = globalThis.crypto;
(async function() {
const key = await subtle.generateKey({
name: 'HMAC',
hash: 'SHA-256',
length: 256,
}, true, ['sign', 'verify']);
const enc = new TextEncoder();
const message = enc.encode('I love cupcakes');
const digest = await subtle.sign({
name: 'HMAC',
}, key, message);
})();
示例#>
【Examples】
生成密钥#>
【Generating keys】
<SubtleCrypto> 类可以用来生成对称(秘密)密钥或非对称密钥对(公钥和私钥)。
【The <SubtleCrypto> class can be used to generate symmetric (secret) keys or asymmetric key pairs (public key and private key).】
AES 密钥#>
【AES keys】
const { subtle } = globalThis.crypto;
async function generateAesKey(length = 256) {
const key = await subtle.generateKey({
name: 'AES-CBC',
length,
}, true, ['encrypt', 'decrypt']);
return key;
}
ECDSA 密钥对#>
【ECDSA key pairs】
const { subtle } = globalThis.crypto;
async function generateEcKey(namedCurve = 'P-521') {
const {
publicKey,
privateKey,
} = await subtle.generateKey({
name: 'ECDSA',
namedCurve,
}, true, ['sign', 'verify']);
return { publicKey, privateKey };
}
Ed25519/X25519 密钥对#>
【Ed25519/X25519 key pairs】
const { subtle } = globalThis.crypto;
async function generateEd25519Key() {
return subtle.generateKey({
name: 'Ed25519',
}, true, ['sign', 'verify']);
}
async function generateX25519Key() {
return subtle.generateKey({
name: 'X25519',
}, true, ['deriveKey']);
}
HMAC 密钥#>
【HMAC keys】
const { subtle } = globalThis.crypto;
async function generateHmacKey(hash = 'SHA-256') {
const key = await subtle.generateKey({
name: 'HMAC',
hash,
}, true, ['sign', 'verify']);
return key;
}
RSA 密钥对#>
【RSA key pairs】
const { subtle } = globalThis.crypto;
const publicExponent = new Uint8Array([1, 0, 1]);
async function generateRsaKey(modulusLength = 2048, hash = 'SHA-256') {
const {
publicKey,
privateKey,
} = await subtle.generateKey({
name: 'RSASSA-PKCS1-v1_5',
modulusLength,
publicExponent,
hash,
}, true, ['sign', 'verify']);
return { publicKey, privateKey };
}
加密与解密#>
【Encryption and decryption】
const crypto = globalThis.crypto;
async function aesEncrypt(plaintext) {
const ec = new TextEncoder();
const key = await generateAesKey();
const iv = crypto.getRandomValues(new Uint8Array(16));
const ciphertext = await crypto.subtle.encrypt({
name: 'AES-CBC',
iv,
}, key, ec.encode(plaintext));
return {
key,
iv,
ciphertext,
};
}
async function aesDecrypt(ciphertext, key, iv) {
const dec = new TextDecoder();
const plaintext = await crypto.subtle.decrypt({
name: 'AES-CBC',
iv,
}, key, ciphertext);
return dec.decode(plaintext);
}
导出和导入密钥#>
【Exporting and importing keys】
const { subtle } = globalThis.crypto;
async function generateAndExportHmacKey(format = 'jwk', hash = 'SHA-512') {
const key = await subtle.generateKey({
name: 'HMAC',
hash,
}, true, ['sign', 'verify']);
return subtle.exportKey(format, key);
}
async function importHmacKey(keyData, format = 'jwk', hash = 'SHA-512') {
const key = await subtle.importKey(format, keyData, {
name: 'HMAC',
hash,
}, true, ['sign', 'verify']);
return key;
}
封装和解包密钥#>
【Wrapping and unwrapping keys】
const { subtle } = globalThis.crypto;
async function generateAndWrapHmacKey(format = 'jwk', hash = 'SHA-512') {
const [
key,
wrappingKey,
] = await Promise.all([
subtle.generateKey({
name: 'HMAC', hash,
}, true, ['sign', 'verify']),
subtle.generateKey({
name: 'AES-KW',
length: 256,
}, true, ['wrapKey', 'unwrapKey']),
]);
const wrappedKey = await subtle.wrapKey(format, key, wrappingKey, 'AES-KW');
return { wrappedKey, wrappingKey };
}
async function unwrapHmacKey(
wrappedKey,
wrappingKey,
format = 'jwk',
hash = 'SHA-512') {
const key = await subtle.unwrapKey(
format,
wrappedKey,
wrappingKey,
'AES-KW',
{ name: 'HMAC', hash },
true,
['sign', 'verify']);
return key;
}
签名并验证#>
【Sign and verify】
const { subtle } = globalThis.crypto;
async function sign(key, data) {
const ec = new TextEncoder();
const signature =
await subtle.sign('RSASSA-PKCS1-v1_5', key, ec.encode(data));
return signature;
}
async function verify(key, signature, data) {
const ec = new TextEncoder();
const verified =
await subtle.verify(
'RSASSA-PKCS1-v1_5',
key,
signature,
ec.encode(data));
return verified;
}
派生位和密钥#>
【Deriving bits and keys】
const { subtle } = globalThis.crypto;
async function pbkdf2(pass, salt, iterations = 1000, length = 256) {
const ec = new TextEncoder();
const key = await subtle.importKey(
'raw',
ec.encode(pass),
'PBKDF2',
false,
['deriveBits']);
const bits = await subtle.deriveBits({
name: 'PBKDF2',
hash: 'SHA-512',
salt: ec.encode(salt),
iterations,
}, key, length);
return bits;
}
async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
const ec = new TextEncoder();
const keyMaterial = await subtle.importKey(
'raw',
ec.encode(pass),
'PBKDF2',
false,
['deriveKey']);
const key = await subtle.deriveKey({
name: 'PBKDF2',
hash: 'SHA-512',
salt: ec.encode(salt),
iterations,
}, keyMaterial, {
name: 'AES-GCM',
length,
}, true, ['encrypt', 'decrypt']);
return key;
}
摘要#>
【Digest】
const { subtle } = globalThis.crypto;
async function digest(data, algorithm = 'SHA-512') {
const ec = new TextEncoder();
const digest = await subtle.digest(algorithm, ec.encode(data));
return digest;
}
算法矩阵#>
【Algorithm matrix】
该表列出了 Node.js Web Crypto API 实现所支持的算法,以及每个算法所支持的 API:
【The table details the algorithms supported by the Node.js Web Crypto API implementation and the APIs supported for each:】
| Algorithm | generateKey | exportKey | importKey | encrypt | decrypt | wrapKey | unwrapKey | deriveBits | deriveKey | sign | verify | digest |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
'RSASSA-PKCS1-v1_5' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'RSA-PSS' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'RSA-OAEP' | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||||
'ECDSA' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'Ed25519' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'Ed448' 1 | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'ECDH' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'X25519' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'X448' 1 | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'AES-CTR' | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||||
'AES-CBC' | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||||
'AES-GCM' | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | |||||
'AES-KW' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'HMAC' | ✔ | ✔ | ✔ | ✔ | ✔ | |||||||
'HKDF' | ✔ | ✔ | ✔ | ✔ | ||||||||
'PBKDF2' | ✔ | ✔ | ✔ | ✔ | ||||||||
'SHA-1' | ✔ | |||||||||||
'SHA-256' | ✔ | |||||||||||
'SHA-384' | ✔ | |||||||||||
'SHA-512' | ✔ |
类:Crypto#>
【Class: Crypto】
globalThis.crypto 是 Crypto 类的一个实例。Crypto 是一个单例,提供对其余加密 API 的访问。
crypto.subtle#>
提供对 SubtleCrypto API 的访问。
【Provides access to the SubtleCrypto API.】
crypto.getRandomValues(typedArray)#>
typedArray<Buffer> | <TypedArray>- 返回:<Buffer> | <TypedArray>
生成加密强度的随机值。给定的 typedArray 会被填充随机值,并返回对 typedArray 的引用。
【Generates cryptographically strong random values. The given typedArray is
filled with random values, and a reference to typedArray is returned.】
所提供的 typedArray 必须是基于整数的 <TypedArray> 实例,即不接受 Float32Array 和 Float64Array。
【The given typedArray must be an integer-based instance of <TypedArray>,
i.e. Float32Array and Float64Array are not accepted.】
如果给定的 typedArray 大于 65,536 字节,将会抛出错误。
【An error will be thrown if the given typedArray is larger than 65,536 bytes.】
crypto.randomUUID()#>
- 返回:<string>
生成一个随机的 RFC 4122 版本 4 UUID。该 UUID 是使用加密伪随机数生成器生成的。
【Generates a random RFC 4122 version 4 UUID. The UUID is generated using a cryptographic pseudorandom number generator.】
类:CryptoKey#>
【Class: CryptoKey】
cryptoKey.algorithm#>
一个对象,详细说明了可以使用该密钥的算法以及其他特定于算法的参数。
【An object detailing the algorithm for which the key can be used along with additional algorithm-specific parameters.】
只读。
【Read-only.】
cryptoKey.extractable#>
- 类型:<boolean>
当为 true 时,<CryptoKey> 可以使用 subtleCrypto.exportKey() 或 subtleCrypto.wrapKey() 提取。
【When true, the <CryptoKey> can be extracted using either
subtleCrypto.exportKey() or subtleCrypto.wrapKey().】
只读。
【Read-only.】
cryptoKey.type#>
- 类型:<string> 其中之一:'secret'、'private' 或 'public'。
一个字符串,用于标识密钥是对称('secret')还是非对称('private' 或 'public')密钥。
【A string identifying whether the key is a symmetric ('secret') or
asymmetric ('private' or 'public') key.】
cryptoKey.usages#>
- 类型: <string[]>
一个字符串数组,用于标识该密钥可以使用的操作。
【An array of strings identifying the operations for which the key may be used.】
可能的用法是:
【The possible usages are:】
'encrypt'- 密钥可用于加密数据。'decrypt'- 密钥可用于解密数据。'sign'- 该密钥可用于生成数字签名。'verify'- 该密钥可用于验证数字签名。'deriveKey'- 该密钥可用于派生新密钥。'deriveBits'- 该密钥可用于派生位。'wrapKey'- 该密钥可以用于封装另一个密钥。'unwrapKey'- 该密钥可以用于解封另一个密钥。
有效的密钥用途取决于密钥算法(由 cryptokey.algorithm.name 标识)。
【Valid key usages depend on the key algorithm (identified by
cryptokey.algorithm.name).】
| Key Type | 'encrypt' | 'decrypt' | 'sign' | 'verify' | 'deriveKey' | 'deriveBits' | 'wrapKey' | 'unwrapKey' |
|---|---|---|---|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | ✔ | ✔ | ||||
'AES-CTR' | ✔ | ✔ | ✔ | ✔ | ||||
'AES-GCM' | ✔ | ✔ | ✔ | ✔ | ||||
'AES-KW' | ✔ | ✔ | ||||||
'ECDH' | ✔ | ✔ | ||||||
'X25519' | ✔ | ✔ | ||||||
'X448' 1 | ✔ | ✔ | ||||||
'ECDSA' | ✔ | ✔ | ||||||
'Ed25519' | ✔ | ✔ | ||||||
'Ed448' 1 | ✔ | ✔ | ||||||
'HDKF' | ✔ | ✔ | ||||||
'HMAC' | ✔ | ✔ | ||||||
'PBKDF2' | ✔ | ✔ | ||||||
'RSA-OAEP' | ✔ | ✔ | ✔ | ✔ | ||||
'RSA-PSS' | ✔ | ✔ | ||||||
'RSASSA-PKCS1-v1_5' | ✔ | ✔ |
类:CryptoKeyPair#>
【Class: CryptoKeyPair】
CryptoKeyPair 是一个简单的字典对象,具有 publicKey 和 privateKey 属性,表示一个非对称密钥对。
【The CryptoKeyPair is a simple dictionary object with publicKey and
privateKey properties, representing an asymmetric key pair.】
cryptoKeyPair.privateKey#>
- 类型:<CryptoKey> 一个
{CryptoKey},其type将是'private'。
cryptoKeyPair.publicKey#>
- 类型:<CryptoKey> 一个
{CryptoKey},其type将是'public'。
类:SubtleCrypto#>
【Class: SubtleCrypto】
subtle.decrypt(algorithm, key, data)#>
algorithm: <RsaOaepParams> | <AesCtrParams> | <AesCbcParams> | <AesGcmParams>key: <CryptoKey>data: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>- 返回:<Promise> 以 <ArrayBuffer> 完成
使用在 algorithm 中指定的方法和参数以及 key 提供的密钥材料,subtle.decrypt() 尝试解密提供的 data。如果成功,返回的 Promise 将会解析为一个包含明文结果的 <ArrayBuffer>。
【Using the method and parameters specified in algorithm and the keying
material provided by key, subtle.decrypt() attempts to decipher the
provided data. If successful, the returned promise will be resolved with
an <ArrayBuffer> containing the plaintext result.】
目前支持的算法包括:
【The algorithms currently supported include:】
'RSA-OAEP''AES-CTR''AES-CBC''AES-GCM'
subtle.deriveBits(algorithm, baseKey[, length])#>
algorithm: <AlgorithmIdentifier> | <EcdhKeyDeriveParams> | <HkdfParams> | <Pbkdf2Params>baseKey: <CryptoKey>length:<number> | <null> 默认值:null- 返回:<Promise> 以 <ArrayBuffer> 完成
使用 algorithm 中指定的方法和参数以及 baseKey 提供的密钥材料,subtle.deriveBits() 尝试生成 length 位。
【Using the method and parameters specified in algorithm and the keying
material provided by baseKey, subtle.deriveBits() attempts to generate
length bits.】
当未提供 length 或为 null 时,将生成给定算法的最大比特数。这适用于 'ECDH'、'X25519' 和 'X448' 算法,对于其他算法,length 必须是一个数字。
【When length is not provided or null the maximum number of bits for a given
algorithm is generated. This is allowed for the 'ECDH', 'X25519', and 'X448'
algorithms, for other algorithms length is required to be a number.】
如果成功,返回的 promise 将会被解析为一个包含生成数据的 <ArrayBuffer>。
【If successful, the returned promise will be resolved with an <ArrayBuffer> containing the generated data.】
目前支持的算法包括:
【The algorithms currently supported include:】
'ECDH''X25519''X448'1'HKDF''PBKDF2'
subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)#>
algorithm: <AlgorithmIdentifier> | <EcdhKeyDeriveParams> | <HkdfParams> | <Pbkdf2Params>baseKey: <CryptoKey>derivedKeyAlgorithm: <HmacKeyGenParams> | <AesKeyGenParams>extractable: <boolean>keyUsages: <string[]> 参见 主要用途。- 返回:<Promise> 以 <CryptoKey> 完成
使用 algorithm 中指定的方法和参数,以及由 baseKey 提供的密钥材料,subtle.deriveKey() 尝试根据 derivedKeyAlgorithm 中的方法和参数生成一个新的 <CryptoKey>。
【Using the method and parameters specified in algorithm, and the keying
material provided by baseKey, subtle.deriveKey() attempts to generate
a new <CryptoKey> based on the method and parameters in derivedKeyAlgorithm.】
调用 subtle.deriveKey() 相当于先调用 subtle.deriveBits() 生成原始密钥材料,然后将结果传入 subtle.importKey() 方法,并使用 deriveKeyAlgorithm、extractable 和 keyUsages 参数作为输入。
【Calling subtle.deriveKey() is equivalent to calling subtle.deriveBits() to
generate raw keying material, then passing the result into the
subtle.importKey() method using the deriveKeyAlgorithm, extractable, and
keyUsages parameters as input.】
目前支持的算法包括:
【The algorithms currently supported include:】
'ECDH''X25519''X448'1'HKDF''PBKDF2'
subtle.digest(algorithm, data)#>
algorithm: <string> | <Object>data: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>- 返回:<Promise> 以 <ArrayBuffer> 完成
使用由 algorithm 指定的方法,subtle.digest() 会尝试生成 data 的摘要。如果成功,返回的 Promise 将被解析为一个包含计算得出的摘要的 <ArrayBuffer>。
【Using the method identified by algorithm, subtle.digest() attempts to
generate a digest of data. If successful, the returned promise is resolved
with an <ArrayBuffer> containing the computed digest.】
如果 algorithm 被提供为 <string>,它必须是以下之一:
【If algorithm is provided as a <string>, it must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果 algorithm 作为 <Object> 提供,它必须有一个 name 属性,其值必须是上述之一。
【If algorithm is provided as an <Object>, it must have a name property
whose value is one of the above.】
subtle.encrypt(algorithm, key, data)#>
algorithm: <RsaOaepParams> | <AesCtrParams> | <AesCbcParams> | <AesGcmParams>key: <CryptoKey>data: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>- 返回:<Promise> 以 <ArrayBuffer> 完成
使用由 algorithm 指定的方法和参数,以及由 key 提供的密钥材料,subtle.encrypt() 尝试对 data 进行加密。如果成功,返回的 Promise 将被解析为包含加密结果的 <ArrayBuffer>。
【Using the method and parameters specified by algorithm and the keying
material provided by key, subtle.encrypt() attempts to encipher data.
If successful, the returned promise is resolved with an <ArrayBuffer>
containing the encrypted result.】
目前支持的算法包括:
【The algorithms currently supported include:】
'RSA-OAEP''AES-CTR''AES-CBC''AES-GCM'
subtle.exportKey(format, key)#>
format: <string> 必须是'raw'、'pkcs8'、'spki'或'jwk'之一。key: <CryptoKey>- 返回:<Promise> 使用 <ArrayBuffer> | <Object> 完成。
如果支持,将给定的密钥导出为指定的格式。
【Exports the given key into the specified format, if supported.】
如果 <CryptoKey> 无法提取,返回的 Promise 将会被拒绝。
【If the <CryptoKey> is not extractable, the returned promise will reject.】
当 format 为 'pkcs8' 或 'spki' 且导出成功时,返回的 promise 将被解析为一个包含导出密钥数据的 <ArrayBuffer>。
【When format is either 'pkcs8' or 'spki' and the export is successful,
the returned promise will be resolved with an <ArrayBuffer> containing the
exported key data.】
当 format 为 'jwk' 且导出成功时,返回的 promise 将会以一个符合 JSON 网钥 规范的 JavaScript 对象来解析。
【When format is 'jwk' and the export is successful, the returned promise
will be resolved with a JavaScript object conforming to the JSON Web Key
specification.】
| Key Type | 'spki' | 'pkcs8' | 'jwk' | 'raw' |
|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | ||
'AES-CTR' | ✔ | ✔ | ||
'AES-GCM' | ✔ | ✔ | ||
'AES-KW' | ✔ | ✔ | ||
'ECDH' | ✔ | ✔ | ✔ | ✔ |
'ECDSA' | ✔ | ✔ | ✔ | ✔ |
'Ed25519' | ✔ | ✔ | ✔ | ✔ |
'Ed448' 1 | ✔ | ✔ | ✔ | ✔ |
'HDKF' | ||||
'HMAC' | ✔ | ✔ | ||
'PBKDF2' | ||||
'RSA-OAEP' | ✔ | ✔ | ✔ | |
'RSA-PSS' | ✔ | ✔ | ✔ | |
'RSASSA-PKCS1-v1_5' | ✔ | ✔ | ✔ |
subtle.generateKey(algorithm, extractable, keyUsages)#>
algorithm: <AlgorithmIdentifier> | <RsaHashedKeyGenParams> | <EcKeyGenParams> | <HmacKeyGenParams> | <AesKeyGenParams>
extractable: <boolean>keyUsages: <string[]> 参见 主要用途。- 返回:<Promise> 以 <CryptoKey> | <CryptoKeyPair> 完成
使用 algorithm 中提供的方法和参数,subtle.generateKey() 尝试生成新的密钥材料。根据使用的方法,该方法可能生成单个 <CryptoKey> 或 <CryptoKeyPair>。
【Using the method and parameters provided in algorithm, subtle.generateKey()
attempts to generate new keying material. Depending the method used, the method
may generate either a single <CryptoKey> or a <CryptoKeyPair>.】
支持的 <CryptoKeyPair>(公钥和私钥)生成算法包括:
【The <CryptoKeyPair> (public and private key) generating algorithms supported include:】
支持的 <CryptoKey>(密钥)生成算法包括:
【The <CryptoKey> (secret key) generating algorithms supported include:】
'HMAC''AES-CTR''AES-CBC''AES-GCM''AES-KW'
subtle.importKey(format, keyData, algorithm, extractable, keyUsages)#>
format: <string> 必须是'raw'、'pkcs8'、'spki'或'jwk'之一。keyData: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer> | <Object>
algorithm: <AlgorithmIdentifier> | <RsaHashedImportParams> | <EcKeyImportParams> | <HmacImportParams>
extractable: <boolean>keyUsages: <string[]> 参见 主要用途。- 返回:<Promise> 以 <CryptoKey> 完成
subtle.importKey() 方法尝试将提供的 keyData 按给定的 format 进行解析,以使用提供的 algorithm、extractable 和 keyUsages 参数创建一个 <CryptoKey> 实例。如果导入成功,返回的 promise 将会被解析为创建的 <CryptoKey>。
【The subtle.importKey() method attempts to interpret the provided keyData
as the given format to create a <CryptoKey> instance using the provided
algorithm, extractable, and keyUsages arguments. If the import is
successful, the returned promise will be resolved with the created <CryptoKey>.】
如果导入 'PBKDF2' 密钥,extractable 必须为 false。
【If importing a 'PBKDF2' key, extractable must be false.】
目前支持的算法包括:
【The algorithms currently supported include:】
| Key Type | 'spki' | 'pkcs8' | 'jwk' | 'raw' |
|---|---|---|---|---|
'AES-CBC' | ✔ | ✔ | ||
'AES-CTR' | ✔ | ✔ | ||
'AES-GCM' | ✔ | ✔ | ||
'AES-KW' | ✔ | ✔ | ||
'ECDH' | ✔ | ✔ | ✔ | ✔ |
'X25519' | ✔ | ✔ | ✔ | ✔ |
'X448' 1 | ✔ | ✔ | ✔ | ✔ |
'ECDSA' | ✔ | ✔ | ✔ | ✔ |
'Ed25519' | ✔ | ✔ | ✔ | ✔ |
'Ed448' 1 | ✔ | ✔ | ✔ | ✔ |
'HDKF' | ✔ | |||
'HMAC' | ✔ | ✔ | ||
'PBKDF2' | ✔ | |||
'RSA-OAEP' | ✔ | ✔ | ✔ | |
'RSA-PSS' | ✔ | ✔ | ✔ | |
'RSASSA-PKCS1-v1_5' | ✔ | ✔ | ✔ |
subtle.sign(algorithm, key, data)#>
algorithm: <AlgorithmIdentifier> | <RsaPssParams> | <EcdsaParams> | <Ed448Params>key: <CryptoKey>data: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>- 返回:<Promise> 以 <ArrayBuffer> 完成
使用 algorithm 提供的方法和参数以及 key 提供的密钥材料,subtle.sign() 尝试生成 data 的加密签名。如果成功,返回的 Promise 将会被解决,并返回一个包含生成签名的 <ArrayBuffer>。
【Using the method and parameters given by algorithm and the keying material
provided by key, subtle.sign() attempts to generate a cryptographic
signature of data. If successful, the returned promise is resolved with
an <ArrayBuffer> containing the generated signature.】
目前支持的算法包括:
【The algorithms currently supported include:】
'RSASSA-PKCS1-v1_5''RSA-PSS''ECDSA''Ed25519''Ed448'1'HMAC'
subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgo, unwrappedKeyAlgo, extractable, keyUsages)#>
format: <string> 必须是'raw'、'pkcs8'、'spki'或'jwk'之一。wrappedKey: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>unwrappingKey: <CryptoKey>
unwrapAlgo: <AlgorithmIdentifier> | <RsaOaepParams> | <AesCtrParams> | <AesCbcParams> | <AesGcmParams>unwrappedKeyAlgo: <AlgorithmIdentifier> | <RsaHashedImportParams> | <EcKeyImportParams> | <HmacImportParams>
extractable: <boolean>keyUsages: <string[]> 参见 主要用途。- 返回:<Promise> 以 <CryptoKey> 完成
在密码学中,“封装密钥”指的是导出然后加密密钥材料。subtle.unwrapKey() 方法尝试解密一个封装的密钥并创建一个 <CryptoKey> 实例。它等同于先对加密的密钥数据调用 subtle.decrypt()(使用 wrappedKey、unwrapAlgo 和 unwrappingKey 参数作为输入),然后将结果传入 subtle.importKey() 方法,使用 unwrappedKeyAlgo、extractable 和 keyUsages 参数作为输入。如果成功,返回的 Promise 会解析为一个 <CryptoKey> 对象。
【In cryptography, "wrapping a key" refers to exporting and then encrypting the
keying material. The subtle.unwrapKey() method attempts to decrypt a wrapped
key and create a <CryptoKey> instance. It is equivalent to calling
subtle.decrypt() first on the encrypted key data (using the wrappedKey,
unwrapAlgo, and unwrappingKey arguments as input) then passing the results
in to the subtle.importKey() method using the unwrappedKeyAlgo,
extractable, and keyUsages arguments as inputs. If successful, the returned
promise is resolved with a <CryptoKey> object.】
目前支持的环绕算法包括:
【The wrapping algorithms currently supported include:】
'RSA-OAEP''AES-CTR''AES-CBC''AES-GCM''AES-KW'
支持的解包密钥算法包括:
【The unwrapped key algorithms supported include:】
'RSASSA-PKCS1-v1_5''RSA-PSS''RSA-OAEP''ECDSA''Ed25519''Ed448'1'ECDH''X25519''X448'1'HMAC''AES-CTR''AES-CBC''AES-GCM''AES-KW'
subtle.verify(algorithm, key, signature, data)#>
algorithm: <AlgorithmIdentifier> | <RsaPssParams> | <EcdsaParams> | <Ed448Params>key: <CryptoKey>signature: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>data: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>- 返回:<Promise> 以 <boolean> 完成
使用 algorithm 中给定的方法和参数以及 key 提供的密钥材料,subtle.verify() 尝试验证 signature 是否是 data 的有效加密签名。返回的 Promise 会解析为 true 或 false。
【Using the method and parameters given in algorithm and the keying material
provided by key, subtle.verify() attempts to verify that signature is
a valid cryptographic signature of data. The returned promise is resolved
with either true or false.】
目前支持的算法包括:
【The algorithms currently supported include:】
'RSASSA-PKCS1-v1_5''RSA-PSS''ECDSA''Ed25519''Ed448'1'HMAC'
subtle.wrapKey(format, key, wrappingKey, wrapAlgo)#>
format: <string> 必须是'raw'、'pkcs8'、'spki'或'jwk'之一。key: <CryptoKey>wrappingKey: <CryptoKey>wrapAlgo: <AlgorithmIdentifier> | <RsaOaepParams> | <AesCtrParams> | <AesCbcParams> | <AesGcmParams>- 返回:<Promise> 以 <ArrayBuffer> 完成
在密码学中,“封装密钥”是指导出然后加密密钥材料。subtle.wrapKey() 方法将密钥材料导出为 format 指定的格式,然后使用 wrapAlgo 指定的方法和参数以及 wrappingKey 提供的密钥材料进行加密。这相当于使用 format 和 key 作为参数调用 subtle.exportKey(),然后将结果传递给 subtle.encrypt() 方法,并使用 wrappingKey 和 wrapAlgo 作为输入。如果成功,返回的 Promise 将会被解析为包含加密密钥数据的 <ArrayBuffer>。
【In cryptography, "wrapping a key" refers to exporting and then encrypting the
keying material. The subtle.wrapKey() method exports the keying material into
the format identified by format, then encrypts it using the method and
parameters specified by wrapAlgo and the keying material provided by
wrappingKey. It is the equivalent to calling subtle.exportKey() using
format and key as the arguments, then passing the result to the
subtle.encrypt() method using wrappingKey and wrapAlgo as inputs. If
successful, the returned promise will be resolved with an <ArrayBuffer>
containing the encrypted key data.】
目前支持的环绕算法包括:
【The wrapping algorithms currently supported include:】
'RSA-OAEP''AES-CTR''AES-CBC''AES-GCM''AES-KW'
算法参数#>
【Algorithm parameters】
算法参数对象定义了各种 <SubtleCrypto> 方法使用的方法和参数。虽然这里描述为“类”,它们实际上是简单的 JavaScript 字典对象。
【The algorithm parameter objects define the methods and parameters used by the various <SubtleCrypto> methods. While described here as "classes", they are simple JavaScript dictionary objects.】
类:AlgorithmIdentifier#>
【Class: AlgorithmIdentifier】
algorithmIdentifier.name#>
- 类型:<string>
类:AesCbcParams#>
【Class: AesCbcParams】
aesCbcParams.iv#>
- 类型: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>
提供初始化向量。它的长度必须正好为16字节,且应不可预测并且符合密码学上的随机性。
【Provides the initialization vector. It must be exactly 16-bytes in length and should be unpredictable and cryptographically random.】
aesCbcParams.name#>
- 类型:<string> 必须是
'AES-CBC'。
类:AesCtrParams#>
【Class: AesCtrParams】
aesCtrParams.counter#>
- 类型: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>
计数器块的初始值。长度必须正好为16字节。
【The initial value of the counter block. This must be exactly 16 bytes long.】
AES-CTR 方法使用块的最右边 length 位作为计数器,剩余的位作为随机数。
【The AES-CTR method uses the rightmost length bits of the block as the
counter and the remaining bits as the nonce.】
aesCtrParams.length#>
- 类型:<number>
aesCtrParams.counter中用作计数器的位数。
aesCtrParams.name#>
- 类型:<string> 必须是
'AES-CTR'。
类:AesGcmParams#>
【Class: AesGcmParams】
aesGcmParams.additionalData#>
- 类型:<ArrayBuffer> | <TypedArray> | <DataView> | <Buffer> | <undefined>
使用 AES-GCM 方法时,additionalData 是额外输入,它不会被加密,但会包含在数据的认证中。additionalData 的使用是可选的。
【With the AES-GCM method, the additionalData is extra input that is not
encrypted but is included in the authentication of the data. The use of
additionalData is optional.】
aesGcmParams.iv#>
- 类型: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>
初始化向量在每次使用给定密钥进行加密操作时必须是唯一的。
【The initialization vector must be unique for every encryption operation using a given key.】
理想情况下,这是一个确定性的 12 字节值,其计算方式确保在使用相同密钥的所有调用中都是唯一的。或者,初始化向量可以由至少 12 个加密随机字节组成。有关构建 AES-GCM 初始化向量的更多信息,请参阅 NIST SP 800-38D 第 8 节。
【Ideally, this is a deterministic 12-byte value that is computed in such a way that it is guaranteed to be unique across all invocations that use the same key. Alternatively, the initialization vector may consist of at least 12 cryptographically random bytes. For more information on constructing initialization vectors for AES-GCM, refer to Section 8 of NIST SP 800-38D.】
aesGcmParams.name#>
- 类型:<string> 必须是
'AES-GCM'。
aesGcmParams.tagLength#>
- 类型:<number> 生成的认证标签的位大小。此值必须是
32、64、96、104、112、120或128之一。默认值:128。
类:AesKeyGenParams#>
【Class: AesKeyGenParams】
aesKeyGenParams.length#>
- 类型:<number>
要生成的 AES 密钥的长度。它必须是 128、192 或 256。
【The length of the AES key to be generated. This must be either 128, 192,
or 256.】
aesKeyGenParams.name#>
- 类型: <string> 必须是
'AES-CBC'、'AES-CTR'、'AES-GCM'或'AES-KW'中的一个
类:EcdhKeyDeriveParams#>
【Class: EcdhKeyDeriveParams】
ecdhKeyDeriveParams.name#>
- 类型:<string> 必须是
'ECDH'、'X25519'或'X448'。
ecdhKeyDeriveParams.public#>
- 类型:<CryptoKey>
ECDH 密钥派生的操作方式是将一方的私钥和另一方的公钥作为输入——使用两者生成一个共同的共享密钥。ecdhKeyDeriveParams.public 属性设置为另一方的公钥。
【ECDH key derivation operates by taking as input one parties private key and
another parties public key -- using both to generate a common shared secret.
The ecdhKeyDeriveParams.public property is set to the other parties public
key.】
类:EcdsaParams#>
【Class: EcdsaParams】
ecdsaParams.hash#>
如果表示为 <string>,该值必须是以下之一:
【If represented as a <string>, the value must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果表示为一个 <Object>,该对象必须有一个 name 属性,其值必须是上述列出的值之一。
【If represented as an <Object>, the object must have a name property
whose value is one of the above listed values.】
ecdsaParams.name#>
- 类型:<string> 必须为 'ECDSA'。
类:EcKeyGenParams#>
【Class: EcKeyGenParams】
ecKeyGenParams.name#>
- 类型:<string> 必须是
'ECDSA'或'ECDH'之一。
ecKeyGenParams.namedCurve#>
- 类型: <string> 必须是
'P-256'、'P-384'或'P-521'之一。
类:EcKeyImportParams#>
【Class: EcKeyImportParams】
ecKeyImportParams.name#>
- 类型:<string> 必须是
'ECDSA'或'ECDH'之一。
ecKeyImportParams.namedCurve#>
- 类型: <string> 必须是
'P-256'、'P-384'或'P-521'之一。
类:Ed448Params#>
【Class: Ed448Params】
ed448Params.name#>
- 类型:<string> 必须为
'Ed448'。
ed448Params.context#>
- 类型:<ArrayBuffer> | <TypedArray> | <DataView> | <Buffer> | <undefined>
context 成员表示要与消息关联的可选上下文数据。Node.js 的 Web Crypto API 实现仅支持零长度上下文,这等同于根本不提供上下文。
【The context member represents the optional context data to associate with
the message.
The Node.js Web Crypto API implementation only supports zero-length context
which is equivalent to not providing context at all.】
类:HkdfParams#>
【Class: HkdfParams】
hkdfParams.hash#>
如果表示为 <string>,该值必须是以下之一:
【If represented as a <string>, the value must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果表示为一个 <Object>,该对象必须有一个 name 属性,其值必须是上述列出的值之一。
【If represented as an <Object>, the object must have a name property
whose value is one of the above listed values.】
hkdfParams.info#>
- 类型: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>
向 HKDF 算法提供特定于应用的上下文输入。该输入可以为空,但必须提供。
【Provides application-specific contextual input to the HKDF algorithm. This can be zero-length but must be provided.】
hkdfParams.name#>
- 类型:<string> 必须为
'HKDF'。
hkdfParams.salt#>
- 类型: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>
盐值显著提高了 HKDF 算法的强度。它应当是随机的或伪随机的,并且长度应与摘要函数的输出相同(例如,如果使用 'SHA-256' 作为摘要函数,盐值应为 256 位的随机数据)。
【The salt value significantly improves the strength of the HKDF algorithm.
It should be random or pseudorandom and should be the same length as the
output of the digest function (for instance, if using 'SHA-256' as the
digest, the salt should be 256-bits of random data).】
类:HmacImportParams#>
【Class: HmacImportParams】
hmacImportParams.hash#>
如果表示为 <string>,该值必须是以下之一:
【If represented as a <string>, the value must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果表示为一个 <Object>,该对象必须有一个 name 属性,其值必须是上述列出的值之一。
【If represented as an <Object>, the object must have a name property
whose value is one of the above listed values.】
hmacImportParams.length#>
- 类型:<number>
HMAC 密钥中可选的位数。这是可选的,在大多数情况下应省略。
【The optional number of bits in the HMAC key. This is optional and should be omitted for most cases.】
hmacImportParams.name#>
- 类型:<string> 必须为
'HMAC'。
类:HmacKeyGenParams#>
【Class: HmacKeyGenParams】
hmacKeyGenParams.hash#>
如果表示为 <string>,该值必须是以下之一:
【If represented as a <string>, the value must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果表示为一个 <Object>,该对象必须有一个 name 属性,其值必须是上述列出的值之一。
【If represented as an <Object>, the object must have a name property
whose value is one of the above listed values.】
hmacKeyGenParams.length#>
- 类型:<number>
用于生成 HMAC 密钥的位数。如果省略,将由所使用的哈希算法确定长度。这是可选的,在大多数情况下应省略。
【The number of bits to generate for the HMAC key. If omitted, the length will be determined by the hash algorithm used. This is optional and should be omitted for most cases.】
hmacKeyGenParams.name#>
- 类型:<string> 必须为
'HMAC'。
类:Pbkdf2Params#>
【Class: Pbkdf2Params】
pbkdb2Params.hash#>
如果表示为 <string>,该值必须是以下之一:
【If represented as a <string>, the value must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果表示为一个 <Object>,该对象必须有一个 name 属性,其值必须是上述列出的值之一。
【If represented as an <Object>, the object must have a name property
whose value is one of the above listed values.】
pbkdf2Params.iterations#>
- 类型:<number>
PBKDF2 算法在导出位时应进行的迭代次数。
【The number of iterations the PBKDF2 algorithm should make when deriving bits.】
pbkdf2Params.name#>
- 类型:<string> 必须为
'PBKDF2'。
pbkdf2Params.salt#>
- 类型: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>
应至少为 16 个随机或伪随机字节。
【Should be at least 16 random or pseudorandom bytes.】
类:RsaHashedImportParams#>
【Class: RsaHashedImportParams】
rsaHashedImportParams.hash#>
如果表示为 <string>,该值必须是以下之一:
【If represented as a <string>, the value must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果表示为一个 <Object>,该对象必须有一个 name 属性,其值必须是上述列出的值之一。
【If represented as an <Object>, the object must have a name property
whose value is one of the above listed values.】
rsaHashedImportParams.name#>
- 类型: <string> 必须是
'RSASSA-PKCS1-v1_5'、'RSA-PSS'或'RSA-OAEP'之一。
类:RsaHashedKeyGenParams#>
【Class: RsaHashedKeyGenParams】
rsaHashedKeyGenParams.hash#>
如果表示为 <string>,该值必须是以下之一:
【If represented as a <string>, the value must be one of:】
'SHA-1''SHA-256''SHA-384''SHA-512'
如果表示为一个 <Object>,该对象必须有一个 name 属性,其值必须是上述列出的值之一。
【If represented as an <Object>, the object must have a name property
whose value is one of the above listed values.】
rsaHashedKeyGenParams.modulusLength#>
- 类型:<number>
RSA 模数的位长度。作为最佳实践,这个长度应至少为 2048。
【The length in bits of the RSA modulus. As a best practice, this should be
at least 2048.】
rsaHashedKeyGenParams.name#>
- 类型: <string> 必须是
'RSASSA-PKCS1-v1_5'、'RSA-PSS'或'RSA-OAEP'之一。
rsaHashedKeyGenParams.publicExponent#>
- 类型:<Uint8Array>
RSA 公共指数。这必须是一个 <Uint8Array>,其中包含一个大端、无符号的整数,该整数必须适合 32 位。<Uint8Array> 可以包含任意数量的前导零位。该值必须是一个质数。除非有理由使用不同的值,否则将公用指数设置为 new Uint8Array([1, 0, 1])(65537)。
【The RSA public exponent. This must be a <Uint8Array> containing a big-endian,
unsigned integer that must fit within 32-bits. The <Uint8Array> may contain an
arbitrary number of leading zero-bits. The value must be a prime number. Unless
there is reason to use a different value, use new Uint8Array([1, 0, 1])
(65537) as the public exponent.】
类:RsaOaepParams#>
【Class: RsaOaepParams】
rsaOaepParams.label#>
- 类型: <ArrayBuffer> | <TypedArray> | <DataView> | <Buffer>
一组额外的字节,这些字节不会被加密,但会绑定到生成的密文上。
【An additional collection of bytes that will not be encrypted, but will be bound to the generated ciphertext.】
rsaOaepParams.label 参数是可选的。
【The rsaOaepParams.label parameter is optional.】
rsaOaepParams.name#>
- 类型:<string> 必须为
'RSA-OAEP'。
类:RsaPssParams#>
【Class: RsaPssParams】
rsaPssParams.name#>
- 类型:<string> 必须是
'RSA-PSS'。
rsaPssParams.saltLength#>
- 类型:<number>
要使用的随机盐的长度(以字节为单位)。
【The length (in bytes) of the random salt to use.】