Node.js v25.3.0 文档


网络加密 API#>

【Web Crypto API】

Node.js 提供了 网络加密 API 标准的实现。

【Node.js provides an implementation of the Web Crypto API standard.】

使用 globalThis.cryptorequire('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);

})(); 

Web 加密 API 中的现代算法#>

【Modern Algorithms in the Web Cryptography API】

稳定性: 1.1 - 处于活跃开发中

Node.js 提供了以下来自 Web Cryptography API 中的现代算法 WICG 提案的功能实现:

【Node.js provides an implementation of the following features from the Modern Algorithms in the Web Cryptography API WICG proposal:】

算法:

【Algorithms:】

  • 'AES-OCB'1
  • 'Argon2d'2
  • 'Argon2i'2
  • 'Argon2id'2
  • 'ChaCha20-Poly1305'
  • 'cSHAKE128'
  • 'cSHAKE256'
  • 'KMAC128'1
  • 'KMAC256'1
  • 'ML-DSA-44'3
  • 'ML-DSA-65'3
  • 'ML-DSA-87'3
  • 'ML-KEM-512'3
  • 'ML-KEM-768'3
  • 'ML-KEM-1024'3
  • 'SHA3-256'
  • 'SHA3-384'
  • 'SHA3-512'

密钥格式:

【Key Formats:】

  • 'raw-public'
  • 'raw-secret'
  • 'raw-seed'

方法:

【Methods:】

Web Cryptography API 中的安全曲线#>

【Secure Curves in the Web Cryptography API】

稳定性: 1.1 - 处于活跃开发中

Node.js 提供了来自 Web Cryptography API 中的安全曲线 WICG 提案的以下功能实现:

【Node.js provides an implementation of the following features from the Secure Curves in the Web Cryptography API WICG proposal:】

算法:

【Algorithms:】

  • 'Ed448'
  • 'X448'

示例#>

【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;
} 

检查运行时算法支持#>

【Checking for runtime algorithm support】

SubtleCrypto.supports() 允许在 Web Crypto API 中进行功能检测,可用于检测给定的算法标识符(包括其参数)是否支持指定的操作。

此示例使用 Argon2(如果可用)或 PBKDF2(否则)从密码派生密钥;然后使用 AES-OCB(如果可用)或 AES-GCM(否则)加密和解密一些文本。

【This example derives a key from a password using Argon2, if available, or PBKDF2, otherwise; and then encrypts and decrypts some text with it using AES-OCB, if available, and AES-GCM, otherwise.】

const { SubtleCrypto, crypto } = globalThis;

const password = 'correct horse battery staple';
const derivationAlg =
  SubtleCrypto.supports?.('importKey', 'Argon2id') ?
    'Argon2id' :
    'PBKDF2';
const encryptionAlg =
  SubtleCrypto.supports?.('importKey', 'AES-OCB') ?
    'AES-OCB' :
    'AES-GCM';
const passwordKey = await crypto.subtle.importKey(
  derivationAlg === 'Argon2id' ? 'raw-secret' : 'raw',
  new TextEncoder().encode(password),
  derivationAlg,
  false,
  ['deriveKey'],
);
const nonce = crypto.getRandomValues(new Uint8Array(16));
const derivationParams =
  derivationAlg === 'Argon2id' ?
    {
      nonce,
      parallelism: 4,
      memory: 2 ** 21,
      passes: 1,
    } :
    {
      salt: nonce,
      iterations: 100_000,
      hash: 'SHA-256',
    };
const key = await crypto.subtle.deriveKey(
  {
    name: derivationAlg,
    ...derivationParams,
  },
  passwordKey,
  {
    name: encryptionAlg,
    length: 256,
  },
  false,
  ['encrypt', 'decrypt'],
);
const plaintext = 'Hello, world!';
const iv = crypto.getRandomValues(new Uint8Array(16));
const encrypted = await crypto.subtle.encrypt(
  { name: encryptionAlg, iv },
  key,
  new TextEncoder().encode(plaintext),
);
const decrypted = new TextDecoder().decode(await crypto.subtle.decrypt(
  { name: encryptionAlg, iv },
  key,
  encrypted,
)); 

算法矩阵#>

【Algorithm matrix】

该表列出了 Node.js Web Crypto API 实现所支持的算法以及每个算法所支持的 API:

【The tables details the algorithms supported by the Node.js Web Crypto API implementation and the APIs supported for each:】

密钥管理 API#>

【Key Management APIs】

Algorithmsubtle.generateKey()subtle.exportKey()subtle.importKey()subtle.getPublicKey()
'AES-CBC'
'AES-CTR'
'AES-GCM'
'AES-KW'
'AES-OCB'
'Argon2d'
'Argon2i'
'Argon2id'
'ChaCha20-Poly1305'4
'ECDH'
'ECDSA'
'Ed25519'
'Ed448'5
'HKDF'
'HMAC'
'KMAC128'4
'KMAC256'4
'ML-DSA-44'4
'ML-DSA-65'4
'ML-DSA-87'4
'ML-KEM-512'4
'ML-KEM-768'4
'ML-KEM-1024'4
'PBKDF2'
'RSA-OAEP'
'RSA-PSS'
'RSASSA-PKCS1-v1_5'
'X25519'
'X448'5

加密操作 API#>

【Crypto Operation APIs】

列图例:

AlgorithmEncryptionSignatures and MACKey or Bits DerivationKey WrappingKey EncapsulationDigest
'AES-CBC'
'AES-CTR'
'AES-GCM'
'AES-KW'
'AES-OCB'
'Argon2d'
'Argon2i'
'Argon2id'
'ChaCha20-Poly1305'4
'cSHAKE128'4
'cSHAKE256'4
'ECDH'
'ECDSA'
'Ed25519'
'Ed448'5
'HKDF'
'HMAC'
'KMAC128'4
'KMAC256'4
'ML-DSA-44'4
'ML-DSA-65'4
'ML-DSA-87'4
'ML-KEM-512'4
'ML-KEM-768'4
'ML-KEM-1024'4
'PBKDF2'
'RSA-OAEP'
'RSA-PSS'
'RSASSA-PKCS1-v1_5'
'SHA-1'
'SHA-256'
'SHA-384'
'SHA-512'
'SHA3-256'4
'SHA3-384'4
'SHA3-512'4
'X25519'
'X448'5

类:Crypto#>

【Class: Crypto

globalThis.cryptoCrypto 类的一个实例。Crypto 是一个单例,提供对其余加密 API 的访问。

crypto.subtle#>

提供对 SubtleCrypto API 的访问。

【Provides access to the SubtleCrypto API.】

crypto.getRandomValues(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> 实例,即不接受 Float32ArrayFloat64Array

【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()#>

生成一个随机的 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#>

  • 类型:KeyAlgorithm|RsaHashedKeyAlgorithm|EcKeyAlgorithm|AesKeyAlgorithm|HmacKeyAlgorithm|KmacKeyAlgorithm

一个对象,详细说明了可以使用该密钥的算法以及其他特定于算法的参数。

【An object detailing the algorithm for which the key can be used along with additional algorithm-specific parameters.】

只读。

【Read-only.】

cryptoKey.extractable#>

当为true时,可以使用subtle.exportKey()subtle.wrapKey()提取<CryptoKey>

【When true, the <CryptoKey> can be extracted using either subtle.exportKey() or subtle.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#>

一个字符串数组,用于标识该密钥可以使用的操作。

【An array of strings identifying the operations for which the key may be used.】

可能的用法是:

【The possible usages are:】

有效的密钥用途取决于密钥算法(由 cryptokey.algorithm.name 标识)。

【Valid key usages depend on the key algorithm (identified by cryptokey.algorithm.name).】

列图例:

Supported Key AlgorithmEncryptionSignatures and MACKey or Bits DerivationKey WrappingKey Encapsulation
'AES-CBC'
'AES-CTR'
'AES-GCM'
'AES-KW'
'AES-OCB'
'Argon2d'
'Argon2i'
'Argon2id'
'ChaCha20-Poly1305'4
'ECDH'
'ECDSA'
'Ed25519'
'Ed448'5
'HDKF'
'HMAC'
'KMAC128'4
'KMAC256'4
'ML-DSA-44'4
'ML-DSA-65'4
'ML-DSA-87'4
'ML-KEM-512'4
'ML-KEM-768'4
'ML-KEM-1024'4
'PBKDF2'
'RSA-OAEP'
'RSA-PSS'
'RSASSA-PKCS1-v1_5'
'X25519'
'X448'5

类:CryptoKeyPair#>

【Class: CryptoKeyPair

CryptoKeyPair 是一个简单的字典对象,具有 publicKeyprivateKey 属性,表示一个非对称密钥对。

【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

静态方法:SubtleCrypto.supports(operation, algorithm[, lengthOrAdditionalAlgorithm])#>

【Static method: SubtleCrypto.supports(operation, algorithm[, lengthOrAdditionalAlgorithm])

稳定性: 1.1 - 处于活跃开发中

  • operation <string> “加密”、“解密”、“签名”、“验证”、“摘要”、“生成密钥”、“派生密钥”、“派生比特”、“导入密钥”、“导出密钥”、“获取公钥”、“封装密钥”、“解封密钥”、“封装比特”、“封装密钥”、“解封比特”或“解封密钥”
  • algorithm <string>
  • lengthOrAdditionalAlgorithm <null> | <number> | <string> | <undefined> 根据操作的不同,这个参数可能被忽略,在操作为“deriveBits”时表示 length 参数的值,在操作为“deriveKey”时表示要导出的 key 的算法,在操作为“wrapKey”时表示在封装 key 之前要导出的 key 的算法,在操作为“unwrapKey”时表示在解封 key 后要导入的 key 的算法,或者在操作为“encapsulateKey”或“decapsulateKey”时表示在加密/解密封装后的 key 后要导入的 key 的算法。默认值: 当操作为“deriveBits”时为 null,否则为 undefined
  • 返回值:<boolean> 表示该实现是否支持给定的操作

允许在 Web Crypto API 中进行功能检测,可以用来检测给定的算法标识符(包括其参数)是否支持给定的操作。

【Allows feature detection in Web Crypto API, which can be used to detect whether a given algorithm identifier (including its parameters) is supported for the given operation.】

有关此方法的示例用法,请参见 检查运行时算法支持

【See Checking for runtime algorithm support for an example use of this method.】

subtle.decapsulateBits(decapsulationAlgorithm, decapsulationKey, ciphertext)#>

稳定性: 1.1 - 处于活跃开发中

消息接收者使用他们的非对称私钥来解密“封装密钥”(密文),从而恢复一个临时的对称密钥(表示为 <ArrayBuffer>),然后使用该密钥来解密消息。

【A message recipient uses their asymmetric private key to decrypt an "encapsulated key" (ciphertext), thereby recovering a temporary symmetric key (represented as <ArrayBuffer>) which is then used to decrypt a message.】

目前支持的算法包括:

【The algorithms currently supported include:】

  • 'ML-KEM-512'[^现代算法]
  • 'ML-KEM-768'[^现代算法]
  • 'ML-KEM-1024'[^现代算法]

subtle.decapsulateKey(decapsulationAlgorithm, decapsulationKey, ciphertext, sharedKeyAlgorithm, extractable, usages)#>

稳定性: 1.1 - 处于活跃开发中

消息接收者使用他们的非对称私钥来解密“封装密钥”(密文),从而恢复一个临时的对称密钥(表示为 <CryptoKey>),然后使用该密钥来解密消息。

【A message recipient uses their asymmetric private key to decrypt an "encapsulated key" (ciphertext), thereby recovering a temporary symmetric key (represented as <CryptoKey>) which is then used to decrypt a message.】

目前支持的算法包括:

【The algorithms currently supported include:】

  • 'ML-KEM-512'[^现代算法]
  • 'ML-KEM-768'[^现代算法]
  • 'ML-KEM-1024'[^现代算法]

subtle.decrypt(algorithm, key, data)#>

使用在 algorithm 中指定的方法和参数,以及由 key 提供的密钥材料,此方法尝试解密提供的 data。如果成功,返回的 promise 将会以包含明文结果的 <ArrayBuffer> 解析。

【Using the method and parameters specified in algorithm and the keying material provided by key, this method 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:】

  • 'AES-CBC'
  • 'AES-CTR'
  • 'AES-GCM'
  • 'AES-OCB'[^现代算法]
  • 'ChaCha20-Poly1305'4
  • 'RSA-OAEP'

subtle.deriveBits(algorithm, baseKey[, length])#>

使用 algorithm 中指定的方法和参数,以及由 baseKey 提供的密钥材料,该方法尝试生成 length 位。

【Using the method and parameters specified in algorithm and the keying material provided by baseKey, this method attempts to generate length bits.】

当未提供 length 或其为 null 时,会生成给定算法的最大位数。对于 'ECDH''X25519''X448'5 算法,这是允许的;对于其他算法,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'5 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:】

  • 'Argon2d'[^现代算法]
  • 'Argon2i'[^现代算法]
  • 'Argon2id'[^现代算法]
  • 'ECDH'
  • 'HKDF'
  • 'PBKDF2'
  • 'X25519'
  • 'X448'5

subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)#>

使用 algorithm 中指定的方法和参数,以及 baseKey 提供的密钥材料,此方法尝试根据 derivedKeyAlgorithm 中的方法和参数生成新的 <CryptoKey>

【Using the method and parameters specified in algorithm, and the keying material provided by baseKey, this method attempts to generate a new <CryptoKey> based on the method and parameters in derivedKeyAlgorithm.】

调用此方法等同于调用 subtle.deriveBits() 来生成原始密钥材料,然后将结果传入 subtle.importKey() 方法,使用 deriveKeyAlgorithmextractablekeyUsages 参数作为输入。

【Calling this method 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:】

  • 'Argon2d'[^现代算法]
  • 'Argon2i'[^现代算法]
  • 'Argon2id'[^现代算法]
  • 'ECDH'
  • 'HKDF'
  • 'PBKDF2'
  • 'X25519'
  • 'X448'5

subtle.digest(algorithm, data)#>

使用由 algorithm 指定的方法,该方法尝试生成 data 的摘要。如果成功,返回的 Promise 将被解决,并返回包含计算出摘要的 <ArrayBuffer>

【Using the method identified by algorithm, this method 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:】

  • 'cSHAKE128'[^现代算法]
  • 'cSHAKE256'[^现代算法]
  • 'SHA-1'
  • 'SHA-256'
  • 'SHA-384'
  • 'SHA-512'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-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.encapsulateBits(encapsulationAlgorithm, encapsulationKey)#>

稳定性: 1.1 - 处于活跃开发中

使用消息接收者的非对称公钥来加密一个临时对称密钥。这个加密后的密钥就是所谓的“封装密钥”,表示为 EncapsulatedBits。

【Uses a message recipient's asymmetric public key to encrypt a temporary symmetric key. This encrypted key is the "encapsulated key" represented as EncapsulatedBits.】

目前支持的算法包括:

【The algorithms currently supported include:】

  • 'ML-KEM-512'[^现代算法]
  • 'ML-KEM-768'[^现代算法]
  • 'ML-KEM-1024'[^现代算法]

subtle.encapsulateKey(encapsulationAlgorithm, encapsulationKey, sharedKeyAlgorithm, extractable, usages)#>

稳定性: 1.1 - 处于活跃开发中

使用消息接收者的非对称公钥来加密一个临时对称密钥。这个加密后的密钥就是所谓的“封装密钥”,表示为 EncapsulatedKey。

【Uses a message recipient's asymmetric public key to encrypt a temporary symmetric key. This encrypted key is the "encapsulated key" represented as EncapsulatedKey.】

目前支持的算法包括:

【The algorithms currently supported include:】

  • 'ML-KEM-512'[^现代算法]
  • 'ML-KEM-768'[^现代算法]
  • 'ML-KEM-1024'[^现代算法]

subtle.encrypt(algorithm, key, data)#>

使用由 algorithm 指定的方法和参数以及 key 提供的密钥材料,该方法尝试对 data 进行加密。如果成功,返回的 promise 将被解析为包含加密结果的 <ArrayBuffer>

【Using the method and parameters specified by algorithm and the keying material provided by key, this method attempts to encipher data. If successful, the returned promise is resolved with an <ArrayBuffer> containing the encrypted result.】

目前支持的算法包括:

【The algorithms currently supported include:】

  • 'AES-CBC'
  • 'AES-CTR'
  • 'AES-GCM'
  • 'AES-OCB'[^现代算法]
  • 'ChaCha20-Poly1305'4
  • 'RSA-OAEP'

subtle.exportKey(format, key)#>

如果支持,将给定的密钥导出为指定的格式。

【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.】

Supported Key Algorithm'spki''pkcs8''jwk''raw''raw-secret''raw-public''raw-seed'
'AES-CBC'
'AES-CTR'
'AES-GCM'
'AES-KW'
'AES-OCB'4
'ChaCha20-Poly1305'4
'ECDH'
'ECDSA'
'Ed25519'
'Ed448'5
'HMAC'
'KMAC128'4
'KMAC256'4
'ML-DSA-44'4
'ML-DSA-65'4
'ML-DSA-87'4
'ML-KEM-512'4
'ML-KEM-768'4
'ML-KEM-1024'4
'RSA-OAEP'
'RSA-PSS'
'RSASSA-PKCS1-v1_5'

subtle.getPublicKey(key, keyUsages)#>

稳定性: 1.1 - 处于活跃开发中

从给定的私钥派生公钥。

【Derives the public key from a given private key.】

subtle.generateKey(algorithm, extractable, keyUsages)#>

使用 algorithm 中提供的参数,该方法尝试生成新的密钥材料。根据所使用的算法,将生成单个 <CryptoKey><CryptoKeyPair>

【Using the parameters provided in algorithm, this method attempts to generate new keying material. Depending on the algorithm used either a single <CryptoKey> or a <CryptoKeyPair> is generated.】

支持的 <CryptoKeyPair>(公钥和私钥)生成算法包括:

【The <CryptoKeyPair> (public and private key) generating algorithms supported include:】

  • 'ECDH'
  • 'ECDSA'
  • 'Ed25519'
  • 'Ed448'5
  • 'ML-DSA-44'[^现代算法]
  • 'ML-DSA-65'[^现代算法]
  • 'ML-DSA-87'[^现代算法]
  • 'ML-KEM-512'[^现代算法]
  • 'ML-KEM-768'[^现代算法]
  • 'ML-KEM-1024'[^现代算法]
  • 'RSA-OAEP'
  • 'RSA-PSS'
  • 'RSASSA-PKCS1-v1_5'
  • 'X25519'
  • 'X448'5

支持的 <CryptoKey>(密钥)生成算法包括:

【The <CryptoKey> (secret key) generating algorithms supported include:】

  • 'AES-CBC'
  • 'AES-CTR'
  • 'AES-GCM'
  • 'AES-KW'
  • 'AES-OCB'[^现代算法]
  • 'ChaCha20-Poly1305'4
  • 'HMAC'
  • 'KMAC128'4
  • 'KMAC256'4

subtle.importKey(format, keyData, algorithm, extractable, keyUsages)#>

此方法尝试将提供的 keyData 解释为给定的 format,使用提供的 algorithmextractablekeyUsages 参数创建一个 <CryptoKey> 实例。如果导入成功,返回的 Promise 将会解析为键材料的 <CryptoKey> 表示。

【This 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 a <CryptoKey> representation of the key material.】

如果导入 KDF 算法的密钥,extractable 必须为 false

【If importing KDF algorithm keys, extractable must be false.】

目前支持的算法包括:

【The algorithms currently supported include:】

Supported Key Algorithm'spki''pkcs8''jwk''raw''raw-secret''raw-public''raw-seed'
'AES-CBC'
'AES-CTR'
'AES-GCM'
'AES-KW'
'AES-OCB'4
'Argon2d'4
'Argon2i'4
'Argon2id'4
'ChaCha20-Poly1305'4
'ECDH'
'ECDSA'
'Ed25519'
'Ed448'5
'HDKF'
'HMAC'
'KMAC128'4
'KMAC256'4
'ML-DSA-44'4
'ML-DSA-65'4
'ML-DSA-87'4
'ML-KEM-512'4
'ML-KEM-768'4
'ML-KEM-1024'4
'PBKDF2'
'RSA-OAEP'
'RSA-PSS'
'RSASSA-PKCS1-v1_5'
'X25519'
'X448'5

subtle.sign(algorithm, key, data)#>

使用由 algorithm 提供的方法和参数以及 key 提供的密钥材料,该方法尝试生成 data 的加密签名。如果成功,返回的 Promise 将使用包含生成签名的 <ArrayBuffer> 被解析。

【Using the method and parameters given by algorithm and the keying material provided by key, this method 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:】

  • 'ECDSA'
  • 'Ed25519'
  • 'Ed448'5
  • 'HMAC'
  • 'KMAC128'4
  • 'KMAC256'4
  • 'ML-DSA-44'[^现代算法]
  • 'ML-DSA-65'[^现代算法]
  • 'ML-DSA-87'[^现代算法]
  • 'RSA-PSS'
  • 'RSASSA-PKCS1-v1_5'

subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgo, unwrappedKeyAlgo, extractable, keyUsages)#>

在密码学中,“封装密钥”指的是导出然后加密密钥材料。这种方法尝试解密封装的密钥并创建一个 <CryptoKey> 实例。它相当于先对加密密钥数据调用 subtle.decrypt()(使用 wrappedKeyunwrapAlgounwrappingKey 参数作为输入),然后将结果传递给 subtle.importKey() 方法,使用 unwrappedKeyAlgoextractablekeyUsages 参数作为输入。如果成功,返回的 promise 将解析为一个 <CryptoKey> 对象。

【In cryptography, "wrapping a key" refers to exporting and then encrypting the keying material. This 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 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:】

  • 'AES-CBC'
  • 'AES-CTR'
  • 'AES-GCM'
  • 'AES-KW'
  • 'AES-OCB'[^现代算法]
  • 'ChaCha20-Poly1305'4
  • 'RSA-OAEP'

支持的解包密钥算法包括:

【The unwrapped key algorithms supported include:】

  • 'AES-CBC'
  • 'AES-CTR'
  • 'AES-GCM'
  • 'AES-KW'
  • 'AES-OCB'[^现代算法]
  • 'ChaCha20-Poly1305'4
  • 'ECDH'
  • 'ECDSA'
  • 'Ed25519'
  • 'Ed448'5
  • 'HMAC'
  • 'KMAC128'5
  • 'KMAC256'5
  • 'ML-DSA-44'[^现代算法]
  • 'ML-DSA-65'[^现代算法]
  • 'ML-DSA-87'[^现代算法]
  • 'ML-KEM-512'[^现代算法]
  • 'ML-KEM-768'[^现代算法]
  • 'ML-KEM-1024'4v
  • 'RSA-OAEP'
  • 'RSA-PSS'
  • 'RSASSA-PKCS1-v1_5'
  • 'X25519'
  • 'X448'5

subtle.verify(algorithm, key, signature, data)#>

使用 algorithm 中提供的方法和参数以及 key 提供的密钥材料,该方法尝试验证 signature 是否为 data 的有效加密签名。返回的 Promise 将解析为 truefalse

【Using the method and parameters given in algorithm and the keying material provided by key, this method 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:】

  • 'ECDSA'
  • 'Ed25519'
  • 'Ed448'5
  • 'HMAC'
  • 'KMAC128'5
  • 'KMAC256'5
  • 'ML-DSA-44'[^现代算法]
  • 'ML-DSA-65'[^现代算法]
  • 'ML-DSA-87'[^现代算法]
  • 'RSA-PSS'
  • 'RSASSA-PKCS1-v1_5'

subtle.wrapKey(format, key, wrappingKey, wrapAlgo)#>

在密码学中,“封装密钥”指的是导出然后加密密钥材料。此方法将密钥材料导出为由 format 指定的格式,然后使用 wrapAlgo 指定的方法和参数以及 wrappingKey 提供的密钥材料进行加密。它相当于使用 formatkey 作为参数调用 subtle.exportKey(),然后将结果传递给 subtle.encrypt() 方法,并使用 wrappingKeywrapAlgo 作为输入。如果成功,返回的 promise 将会以包含加密密钥数据的 <ArrayBuffer> 解析。

【In cryptography, "wrapping a key" refers to exporting and then encrypting the keying material. This 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:】

  • 'AES-CBC'
  • 'AES-CTR'
  • 'AES-GCM'
  • 'AES-KW'
  • 'AES-OCB'[^现代算法]
  • 'ChaCha20-Poly1305'4
  • 'RSA-OAEP'

算法参数#>

【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.】

类:Algorithm#>

【Class: Algorithm

Algorithm.name#>

类:AeadParams#>

【Class: AeadParams

aeadParams.additionalData#>

额外输入数据未被加密,但包含在数据的认证中。additionalData 的使用是可选的。

【Extra input that is not encrypted but is included in the authentication of the data. The use of additionalData is optional.】

aeadParams.iv#>

初始化向量在每次使用给定密钥进行加密操作时必须是唯一的。

【The initialization vector must be unique for every encryption operation using a given key.】

aeadParams.name#>
  • 类型:<string> 必须是 'AES-GCM''AES-OCB''ChaCha20-Poly1305'
aeadParams.tagLength#>
  • 类型:<number> 生成的身份验证标签的位数。

类:AesDerivedKeyParams#>

【Class: AesDerivedKeyParams

aesDerivedKeyParams.name#>
  • 类型:<string> 必须是 'AES-CBC''AES-CTR''AES-GCM''AES-OCB''AES-KW' 之一
aesDerivedKeyParams.length#>

要派生的 AES 密钥的长度。此长度必须为 128192256

【The length of the AES key to be derived. This must be either 128, 192, or 256.】

类:AesCbcParams#>

【Class: AesCbcParams

aesCbcParams.iv#>

提供初始化向量。它的长度必须正好为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#>

计数器块的初始值。长度必须正好为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'

类:AesKeyAlgorithm#>

【Class: AesKeyAlgorithm

aesKeyAlgorithm.length#>

AES 密钥的长度(以位为单位)。

【The length of the AES key in bits.】

aesKeyAlgorithm.name#>

类:AesKeyGenParams#>

【Class: AesKeyGenParams

aesKeyGenParams.length#>

要生成的 AES 密钥的长度。它必须是 128192256

【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' 中的一个

类:Argon2Params#>

【Class: Argon2Params

argon2Params.associatedData#>

表示可选的关联数据。

【Represents the optional associated data.】

argon2Params.memory#>

表示以千字节为单位的内存大小。它必须至少是并行度的 8 倍。

【Represents the memory size in kibibytes. It must be at least 8 times the degree of parallelism.】

argon2Params.name#>
  • 类型:<string> 必须是 'Argon2d''Argon2i''Argon2id' 之一。
argon2Params.nonce#>

表示 nonce,它是密码哈希应用的盐值。

【Represents the nonce, which is a salt for password hashing applications.】

argon2Params.parallelism#>

表示并行度。

【Represents the degree of parallelism.】

argon2Params.passes#>

表示传递次数。

【Represents the number of passes.】

argon2Params.secretValue#>

表示可选的密钥值。

【Represents the optional secret value.】

argon2Params.version#>

表示 Argon2 的版本号。默认且目前唯一定义的版本是 190x13)。

【Represents the Argon2 version number. The default and currently only defined version is 19 (0x13).】

类:ContextParams#>

【Class: ContextParams

contextParams.name#>
  • 类型:<string> 必须是 Ed4485'ML-DSA-44'4'ML-DSA-65'4,或 'ML-DSA-87'4
contextParams.context#>

context 成员表示要与消息关联的可选上下文数据。

【The context member represents the optional context data to associate with the message.】

类:CShakeParams#>

【Class: CShakeParams

cShakeParams.customization#>

customization 成员表示自定义字符串。Node.js 的 Web Crypto API 实现仅支持零长度的自定义,这等同于根本没有提供自定义。

【The customization member represents the customization string. The Node.js Web Crypto API implementation only supports zero-length customization which is equivalent to not providing customization at all.】

cShakeParams.functionName#>

functionName 成员表示函数名称,由 NIST 用于基于 cSHAKE 定义函数。Node.js Web Crypto API 实现仅支持长度为零的 functionName,这相当于根本不提供 functionName。

【The functionName member represents represents the function name, used by NIST to define functions based on cSHAKE. The Node.js Web Crypto API implementation only supports zero-length functionName which is equivalent to not providing functionName at all.】

cShakeParams.length#>
  • 类型:<number> 表示请求的输出长度(以位为单位)。
cShakeParams.name#>
  • 类型:<string> 必须是 'cSHAKE128'4'cSHAKE256'4

类:EcdhKeyDeriveParams#>

【Class: EcdhKeyDeriveParams

ecdhKeyDeriveParams.name#>
  • 类型:<string> 必须是 'ECDH''X25519''X448'5
ecdhKeyDeriveParams.public#>

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'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-512'[^现代算法]

如果表示为 Algorithm,该对象的 name 属性必须是上述列出的值之一。

【If represented as an Algorithm, the object's name property must be one of the above listed values.】

ecdsaParams.name#>
  • 类型:<string> 必须为 'ECDSA'。

类:EcKeyAlgorithm#>

【Class: EcKeyAlgorithm

ecKeyAlgorithm.name#>
ecKeyAlgorithm.namedCurve#>

类: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' 之一。

类:EncapsulatedBits#>

【Class: EncapsulatedBits

用于消息加密的临时对称秘密密钥(表示为 <ArrayBuffer>)以及使用该共享密钥加密的密文(可以与消息一起传送给消息接收者)。接收者使用他们的私钥来确定共享密钥,从而能够解密消息。

【A temporary symmetric secret key (represented as <ArrayBuffer>) for message encryption and the ciphertext (that can be transmitted to the message recipient along with the message) encrypted by this shared key. The recipient uses their private key to determine what the shared key is which then allows them to decrypt the message.】

encapsulatedBits.ciphertext#>
encapsulatedBits.sharedKey#>

类:EncapsulatedKey#>

【Class: EncapsulatedKey

用于消息加密的临时对称秘密密钥(表示为 <CryptoKey>)以及使用该共享密钥加密的密文(可以与消息一起传送给消息接收者)。接收者使用他们的私钥来确定共享密钥,从而能够解密消息。

【A temporary symmetric secret key (represented as <CryptoKey>) for message encryption and the ciphertext (that can be transmitted to the message recipient along with the message) encrypted by this shared key. The recipient uses their private key to determine what the shared key is which then allows them to decrypt the message.】

encapsulatedKey.ciphertext#>
encapsulatedKey.sharedKey#>

类: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'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-512'[^现代算法]

如果表示为 Algorithm,该对象的 name 属性必须是上述列出的值之一。

【If represented as an Algorithm, the object's name property must be one of the above listed values.】

hkdfParams.info#>

向 HKDF 算法提供特定于应用的上下文输入。该输入可以为空,但必须提供。

【Provides application-specific contextual input to the HKDF algorithm. This can be zero-length but must be provided.】

hkdfParams.name#>
hkdfParams.salt#>

盐值显著提高了 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'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-512'[^现代算法]

如果表示为 Algorithm,该对象的 name 属性必须是上述列出的值之一。

【If represented as an Algorithm, the object's name property must be one of the above listed values.】

hmacImportParams.length#>

HMAC 密钥中可选的位数。这是可选的,在大多数情况下应省略。

【The optional number of bits in the HMAC key. This is optional and should be omitted for most cases.】

hmacImportParams.name#>

类:HmacKeyAlgorithm#>

【Class: HmacKeyAlgorithm

hmacKeyAlgorithm.hash#>
  • 类型:Algorithm
hmacKeyAlgorithm.length#>

HMAC 密钥的长度(以位为单位)。

【The length of the HMAC key in bits.】

hmacKeyAlgorithm.name#>

类: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'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-512'[^现代算法]

如果表示为 Algorithm,该对象的 name 属性必须是上述列出的值之一。

【If represented as an Algorithm, the object's name property must be one of the above listed values.】

hmacKeyGenParams.length#>

用于生成 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#>

类:KeyAlgorithm#>

【Class: KeyAlgorithm

keyAlgorithm.name#>

类:KmacImportParams#>

【Class: KmacImportParams

kmacImportParams.length#>

KMAC 密钥中可选的位数。这是可选的,在大多数情况下应省略。

【The optional number of bits in the KMAC key. This is optional and should be omitted for most cases.】

kmacImportParams.name#>
  • 类型:<string> 必须是 'KMAC128''KMAC256'

类:KmacKeyAlgorithm#>

【Class: KmacKeyAlgorithm

kmacKeyAlgorithm.length#>

KMAC 密钥的长度(以位为单位)。

【The length of the KMAC key in bits.】

kmacKeyAlgorithm.name#>

类:KmacKeyGenParams#>

【Class: KmacKeyGenParams

kmacKeyGenParams.length#>

用于生成 KMAC 密钥的比特数。如果省略,将由所使用的 KMAC 算法确定长度。这是可选的,在大多数情况下应省略。

【The number of bits to generate for the KMAC key. If omitted, the length will be determined by the KMAC algorithm used. This is optional and should be omitted for most cases.】

kmacKeyGenParams.name#>
  • 类型:<string> 必须是 'KMAC128''KMAC256'

类:KmacParams#>

【Class: KmacParams

kmacParams.algorithm#>
  • 类型:<string> 必须是 'KMAC128''KMAC256'
kmacParams.customization#>

customization 成员表示可选的自定义字符串。

【The customization member represents the optional customization string.】

kmacParams.length#>

输出的字节长度。这个值必须是正整数。

【The length of the output in bytes. This must be a positive integer.】

类:Pbkdf2Params#>

【Class: Pbkdf2Params

pbkdf2Params.hash#>

如果表示为 <string>,该值必须是以下之一:

【If represented as a <string>, the value must be one of:】

  • 'SHA-1'
  • 'SHA-256'
  • 'SHA-384'
  • 'SHA-512'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-512'[^现代算法]

如果表示为 Algorithm,该对象的 name 属性必须是上述列出的值之一。

【If represented as an Algorithm, the object's name property must be one of the above listed values.】

pbkdf2Params.iterations#>

PBKDF2 算法在导出位时应进行的迭代次数。

【The number of iterations the PBKDF2 algorithm should make when deriving bits.】

pbkdf2Params.name#>
  • 类型:<string> 必须为 'PBKDF2'
pbkdf2Params.salt#>

应至少为 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'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-512'[^现代算法]

如果表示为 Algorithm,该对象的 name 属性必须是上述列出的值之一。

【If represented as an Algorithm, the object's name property must be one of the above listed values.】

rsaHashedImportParams.name#>
  • 类型: <string> 必须是 'RSASSA-PKCS1-v1_5''RSA-PSS''RSA-OAEP' 之一。

类:RsaHashedKeyAlgorithm#>

【Class: RsaHashedKeyAlgorithm

rsaHashedKeyAlgorithm.hash#>
  • 类型:Algorithm
rsaHashedKeyAlgorithm.modulusLength#>

RSA 模数的长度(以位为单位)。

【The length in bits of the RSA modulus.】

rsaHashedKeyAlgorithm.name#>
rsaHashedKeyAlgorithm.publicExponent#>

RSA 公共指数。

【The RSA public exponent.】

类: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'
  • 'SHA3-256'[^现代算法]
  • 'SHA3-384'[^现代算法]
  • 'SHA3-512'[^现代算法]

如果表示为 Algorithm,该对象的 name 属性必须是上述列出的值之一。

【If represented as an Algorithm, the object's name property must be one of the above listed values.】

rsaHashedKeyGenParams.modulusLength#>

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#>

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#>

一组额外的字节,这些字节不会被加密,但会绑定到生成的密文上。

【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#>

要使用的随机盐的长度(以字节为单位)。

【The length (in bytes) of the random salt to use.】

Footnotes

  1. Requires OpenSSL >= 3.0 2 3

  2. Requires OpenSSL >= 3.2 2 3

  3. Requires OpenSSL >= 3.5 2 3 4 5 6

  4. See Modern Algorithms in the Web Cryptography API 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  5. See Secure Curves in the Web Cryptography API 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Node.js 中文网 - 粤ICP备13048890号