Crypto
crypto.generateKeyPair()
Generates public and private keys asynchronously. Select a key type supported by the target protocol and keep private material out of logs. This fixture uses disposable Ed25519 keys. Tested on Node.js 22.16.0. Example output (one line per entry): ["public private ed25519"].
Formal Syntax & Structure
nodejs syntaxnodejs
crypto.generateKeyPair(type, options, callback)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| type | string | Key algorithm. |
| options | object | Algorithm and encoding options. |
| callback | Function | Receives error and generated keys. |
Return Value
undefined
Keys delivered asynchronously.
Working Example
Example Snippetnodejs
import { generateKeyPair } from 'node:crypto';import { promisify } from 'node:util';const { publicKey, privateKey } = await promisify(generateKeyPair)('ed25519', {});console.log(publicKey.type, privateKey.type, publicKey.asymmetricKeyType);