Node.js v18.12.1 documentation


Table of contents

Web Crypto API#

Stability: 1 - Experimental实验性的

Node.js provides an implementation of the standard Web Crypto API.Node.js提供了标准Web Crypto API的实现。

Use require('node:crypto').webcrypto to access this module.使用require('node:crypto').webcrypto访问此模块。

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

})();