Web Crypto
subtle.generateKey()
Creates key material with explicit usage restrictions. Prefer non-extractable secret keys when export is unnecessary. Tested on Node.js 22.16.0. Example output (one line per entry): ["secret false 256"].
Formal Syntax & Structure
nodejs syntaxnodejs
subtle.generateKey(algorithm, extractable, keyUsages)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| algorithm | object | Key algorithm and parameters. |
| extractable | boolean | Whether export is allowed. |
| keyUsages | string[] | Allowed operations. |
Return Value
Promise<CryptoKey | CryptoKeyPair>
Generated key material.
Working Example
Example Snippetnodejs
import { webcrypto as crypto } from 'node:crypto';const k = await crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);console.log(k.type, k.extractable, k.algorithm.length);