Skip to main content
generateKeyPairSync - crypto - Node documentation
function generateKeyPairSync

Usage in Deno

import { generateKeyPairSync } from "node:crypto";
generateKeyPairSync(
type: "rsa",
options: RSAKeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

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.

When encoding public keys, it is recommended to use 'spki'. When encoding private keys, it is recommended to use 'pkcs8' with a strong passphrase, and to keep the passphrase confidential.

const {
  generateKeyPairSync,
} = await import('node:crypto');

const {
  publicKey,
  privateKey,
} = generateKeyPairSync('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem',
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret',
  },
});

The return value { publicKey, privateKey } represents the generated key pair. When PEM encoding was selected, the respective key will be a string, otherwise it will be a buffer containing the data encoded as DER.

Parameters

type: "rsa"

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

options: RSAKeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "rsa",
options: RSAKeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "rsa"
options: RSAKeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "rsa",
options: RSAKeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "rsa"
options: RSAKeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "rsa",
options: RSAKeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "rsa"
options: RSAKeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "rsa",
): KeyPairKeyObjectResult

Parameters

type: "rsa"

Return Type

generateKeyPairSync(
type: "rsa-pss",
options: RSAPSSKeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

Parameters

type: "rsa-pss"
options: RSAPSSKeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "rsa-pss",
options: RSAPSSKeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "rsa-pss"
options: RSAPSSKeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "rsa-pss",
options: RSAPSSKeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "rsa-pss"
options: RSAPSSKeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "rsa-pss",
options: RSAPSSKeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "rsa-pss"
options: RSAPSSKeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "rsa-pss",
): KeyPairKeyObjectResult

Parameters

type: "rsa-pss"

Return Type

generateKeyPairSync(
type: "dsa",
options: DSAKeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

Parameters

type: "dsa"
options: DSAKeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "dsa",
options: DSAKeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "dsa"
options: DSAKeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "dsa",
options: DSAKeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "dsa"
options: DSAKeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "dsa",
options: DSAKeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "dsa"
options: DSAKeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "dsa",
): KeyPairKeyObjectResult

Parameters

type: "dsa"

Return Type

generateKeyPairSync(
type: "ec",
options: ECKeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

Parameters

type: "ec"
options: ECKeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "ec",
options: ECKeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "ec"
options: ECKeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "ec",
options: ECKeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "ec"
options: ECKeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "ec",
options: ECKeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "ec"
options: ECKeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "ec",
): KeyPairKeyObjectResult

Parameters

type: "ec"

Return Type

generateKeyPairSync(
type: "ed25519",
options: ED25519KeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

Parameters

type: "ed25519"
options: ED25519KeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "ed25519",
options: ED25519KeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "ed25519"
options: ED25519KeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "ed25519",
options: ED25519KeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "ed25519"
options: ED25519KeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "ed25519",
options: ED25519KeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "ed25519"
options: ED25519KeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "ed25519",
): KeyPairKeyObjectResult

Parameters

type: "ed25519"

Return Type

generateKeyPairSync(
type: "ed448",
options: ED448KeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

Parameters

type: "ed448"
options: ED448KeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "ed448",
options: ED448KeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "ed448"
options: ED448KeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "ed448",
options: ED448KeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "ed448"
options: ED448KeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "ed448",
options: ED448KeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "ed448"
options: ED448KeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "ed448",
): KeyPairKeyObjectResult

Parameters

type: "ed448"

Return Type

generateKeyPairSync(
type: "x25519",
options: X25519KeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

Parameters

type: "x25519"
options: X25519KeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "x25519",
options: X25519KeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "x25519"
options: X25519KeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "x25519",
options: X25519KeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "x25519"
options: X25519KeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "x25519",
options: X25519KeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "x25519"
options: X25519KeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "x25519",
): KeyPairKeyObjectResult

Parameters

type: "x25519"

Return Type

generateKeyPairSync(
type: "x448",
options: X448KeyPairOptions<"pem", "pem">,
): KeyPairSyncResult<string, string>

Parameters

type: "x448"
options: X448KeyPairOptions<"pem", "pem">

Return Type

KeyPairSyncResult<string, string>
generateKeyPairSync(
type: "x448",
options: X448KeyPairOptions<"pem", "der">,
): KeyPairSyncResult<string, Buffer>

Parameters

type: "x448"
options: X448KeyPairOptions<"pem", "der">

Return Type

KeyPairSyncResult<string, Buffer>
generateKeyPairSync(
type: "x448",
options: X448KeyPairOptions<"der", "pem">,
): KeyPairSyncResult<Buffer, string>

Parameters

type: "x448"
options: X448KeyPairOptions<"der", "pem">

Return Type

KeyPairSyncResult<Buffer, string>
generateKeyPairSync(
type: "x448",
options: X448KeyPairOptions<"der", "der">,
): KeyPairSyncResult<Buffer, Buffer>

Parameters

type: "x448"
options: X448KeyPairOptions<"der", "der">

Return Type

KeyPairSyncResult<Buffer, Buffer>
generateKeyPairSync(
type: "x448",
): KeyPairKeyObjectResult

Parameters

type: "x448"

Return Type