网络加密 API
【Web Crypto API】
稳定性: 2 - 稳定
Node.js 提供了 网络加密 API 标准的实现。
【Node.js provides an implementation of the Web Crypto API standard.】
使用 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);
})();