网络加密 API
¥Web Crypto API
稳定性: 2 - 稳定的
¥Stability: 2 - Stable
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);
})();