网络加密 API


¥Web Crypto API

稳定性: 1 - 实验性的

¥Stability: 1 - Experimental

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

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

使用 require('node:crypto').webcrypto 访问此模块。

¥Use require('node:crypto').webcrypto to access this module.

const { subtle } = require('node:crypto').webcrypto;

(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);

})();