generateKeyPair

external fun generateKeyPair(    type: KeyType.rsa,     options: RSAKeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)

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 and publicKey / 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.

Since

v10.12.0

Parameters

type

Must be 'rsa', 'rsa-pss', 'dsa', 'ec', 'ed25519', 'ed448', 'x25519', 'x448', or 'dh'.


external fun generateKeyPair(    type: KeyType.rsa,     options: RSAKeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsa,     options: RSAKeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsa,     options: RSAKeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsa,     options: RSAKeyPairKeyObjectOptions,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsaPss,     options: RSAPSSKeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsaPss,     options: RSAPSSKeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsaPss,     options: RSAPSSKeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsaPss,     options: RSAPSSKeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.rsaPss,     options: RSAPSSKeyPairKeyObjectOptions,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.dsa,     options: DSAKeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.dsa,     options: DSAKeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.dsa,     options: DSAKeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.dsa,     options: DSAKeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.dsa,     options: DSAKeyPairKeyObjectOptions,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ec,     options: ECKeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ec,     options: ECKeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ec,     options: ECKeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ec,     options: ECKeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ec,     options: ECKeyPairKeyObjectOptions,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed25519,     options: ED25519KeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed25519,     options: ED25519KeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed25519,     options: ED25519KeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed25519,     options: ED25519KeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed25519,     options: ED25519KeyPairKeyObjectOptions?,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed448,     options: ED448KeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed448,     options: ED448KeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed448,     options: ED448KeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed448,     options: ED448KeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.ed448,     options: ED448KeyPairKeyObjectOptions?,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x25519,     options: X25519KeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x25519,     options: X25519KeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x25519,     options: X25519KeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x25519,     options: X25519KeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x25519,     options: X25519KeyPairKeyObjectOptions?,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x448,     options: X448KeyPairPemPemOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x448,     options: X448KeyPairPemDerOptions,     callback: (<Error class: unknown class>?, publicKey: String, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x448,     options: X448KeyPairDerPemOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: String) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x448,     options: X448KeyPairDerDerOptions,     callback: (<Error class: unknown class>?, publicKey: Buffer<*>, privateKey: Buffer<*>) -> Unit)(source)
external fun generateKeyPair(    type: KeyType.x448,     options: X448KeyPairKeyObjectOptions?,     callback: (<Error class: unknown class>?, publicKey: KeyObject, privateKey: KeyObject) -> Unit)(source)