function generateKeyPair
Usage in Deno
import { generateKeyPair } from "node:crypto";
generateKeyPair(type: "rsa",options: RSAKeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
Generates a new asymmetric key pair of the given type
. RSA, RSA-PSS, DSA, EC,
Ed25519, Ed448, X25519, X448, and DH are currently supported.
If a publicKeyEncoding
or privateKeyEncoding
was specified, this function
behaves as if keyObject.export()
had been called on its result. Otherwise,
the respective part of the key is returned as a KeyObject
.
It is recommended to encode public keys as 'spki'
and private keys as'pkcs8'
with encryption for long-term storage:
const { generateKeyPair, } = await import('node:crypto'); generateKeyPair('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'spki', format: 'pem', }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: 'top secret', }, }, (err, publicKey, privateKey) => { // Handle errors and use the generated key pair. });
On completion, callback
will be called with err
set to undefined
andpublicKey
/ privateKey
representing the generated key pair.
If this method is invoked as its util.promisify()
ed version, it returns
a Promise
for an Object
with publicKey
and privateKey
properties.
options: RSAKeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "rsa",options: RSAKeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: RSAKeyPairOptions<"pem", "der">
void
generateKeyPair(type: "rsa",options: RSAKeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: RSAKeyPairOptions<"der", "pem">
void
generateKeyPair(type: "rsa",options: RSAKeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: RSAKeyPairOptions<"der", "der">
void
generateKeyPair(type: "rsa",options: RSAKeyPairKeyObjectOptions,): void
options: RSAKeyPairKeyObjectOptions
void
generateKeyPair(type: "rsa-pss",options: RSAPSSKeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
options: RSAPSSKeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "rsa-pss",options: RSAPSSKeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: RSAPSSKeyPairOptions<"pem", "der">
void
generateKeyPair(type: "rsa-pss",options: RSAPSSKeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: RSAPSSKeyPairOptions<"der", "pem">
void
generateKeyPair(type: "rsa-pss",options: RSAPSSKeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: RSAPSSKeyPairOptions<"der", "der">
void
generateKeyPair(type: "rsa-pss",options: RSAPSSKeyPairKeyObjectOptions,): void
options: RSAPSSKeyPairKeyObjectOptions
void
generateKeyPair(type: "dsa",options: DSAKeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
options: DSAKeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "dsa",options: DSAKeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: DSAKeyPairOptions<"pem", "der">
void
generateKeyPair(type: "dsa",options: DSAKeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: DSAKeyPairOptions<"der", "pem">
void
generateKeyPair(type: "dsa",options: DSAKeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: DSAKeyPairOptions<"der", "der">
void
generateKeyPair(type: "dsa",options: DSAKeyPairKeyObjectOptions,): void
options: DSAKeyPairKeyObjectOptions
void
generateKeyPair(type: "ec",options: ECKeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
options: ECKeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "ec",options: ECKeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: ECKeyPairOptions<"pem", "der">
void
generateKeyPair(type: "ec",options: ECKeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: ECKeyPairOptions<"der", "pem">
void
generateKeyPair(type: "ec",options: ECKeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: ECKeyPairOptions<"der", "der">
void
generateKeyPair(type: "ec",options: ECKeyPairKeyObjectOptions,): void
options: ECKeyPairKeyObjectOptions
void
generateKeyPair(type: "ed25519",options: ED25519KeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
options: ED25519KeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "ed25519",options: ED25519KeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: ED25519KeyPairOptions<"pem", "der">
void
generateKeyPair(type: "ed25519",options: ED25519KeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: ED25519KeyPairOptions<"der", "pem">
void
generateKeyPair(type: "ed25519",options: ED25519KeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: ED25519KeyPairOptions<"der", "der">
void
generateKeyPair(type: "ed25519",options: ED25519KeyPairKeyObjectOptions | undefined,): void
options: ED25519KeyPairKeyObjectOptions | undefined
void
generateKeyPair(type: "ed448",options: ED448KeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
options: ED448KeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "ed448",options: ED448KeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: ED448KeyPairOptions<"pem", "der">
void
generateKeyPair(type: "ed448",options: ED448KeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: ED448KeyPairOptions<"der", "pem">
void
generateKeyPair(type: "ed448",options: ED448KeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: ED448KeyPairOptions<"der", "der">
void
generateKeyPair(type: "ed448",options: ED448KeyPairKeyObjectOptions | undefined,): void
options: ED448KeyPairKeyObjectOptions | undefined
void
generateKeyPair(type: "x25519",options: X25519KeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
options: X25519KeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "x25519",options: X25519KeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: X25519KeyPairOptions<"pem", "der">
void
generateKeyPair(type: "x25519",options: X25519KeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: X25519KeyPairOptions<"der", "pem">
void
generateKeyPair(type: "x25519",options: X25519KeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: X25519KeyPairOptions<"der", "der">
void
generateKeyPair(type: "x25519",options: X25519KeyPairKeyObjectOptions | undefined,): void
options: X25519KeyPairKeyObjectOptions | undefined
void
generateKeyPair(type: "x448",options: X448KeyPairOptions<"pem", "pem">,callback: (err: Error | null,publicKey: string,privateKey: string,) => void,): void
options: X448KeyPairOptions<"pem", "pem">
void
generateKeyPair(type: "x448",options: X448KeyPairOptions<"pem", "der">,callback: (err: Error | null,publicKey: string,privateKey: Buffer,) => void,): void
options: X448KeyPairOptions<"pem", "der">
void
generateKeyPair(type: "x448",options: X448KeyPairOptions<"der", "pem">,callback: (err: Error | null,publicKey: Buffer,privateKey: string,) => void,): void
options: X448KeyPairOptions<"der", "pem">
void
generateKeyPair(type: "x448",options: X448KeyPairOptions<"der", "der">,callback: (err: Error | null,publicKey: Buffer,privateKey: Buffer,) => void,): void
options: X448KeyPairOptions<"der", "der">
void
generateKeyPair(type: "x448",options: X448KeyPairKeyObjectOptions | undefined,): void
options: X448KeyPairKeyObjectOptions | undefined
void