Web Crypto
subtle.sign()
Produces a signature or MAC depending on algorithm. HMAC uses a shared secret and cannot establish which holder created a message. Tested on Node.js 22.16.0. Example output (one line per entry): ["32 true"].
Formal Syntax & Structure
nodejs syntaxnodejs
subtle.sign(algorithm, key, data)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| algorithm | string | object | Signing algorithm. |
| key | CryptoKey | Signing key. |
| data | BufferSource | Message. |
Return Value
Promise<ArrayBuffer>
Signature or MAC.
Working Example
Example Snippetnodejs
import { webcrypto as crypto } from 'node:crypto';const k = await crypto.subtle.generateKey({ name: 'HMAC', hash: 'SHA-256' }, false, ['sign', 'verify']);const p = new TextEncoder().encode('order:7');const mac = await crypto.subtle.sign('HMAC', k, p);console.log(mac.byteLength, await crypto.subtle.verify('HMAC', k, mac, p));