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

NameTypeDescription
algorithmobjectKey algorithm and parameters.
extractablebooleanWhether export is allowed.
keyUsagesstring[]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);
Node.js Reference