Jump to headinggenerateKeyPairSync(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 Jump to heading
Jump to headingtype: "rsa"Must be 'rsa', 'rsa-pss', 'dsa', 'ec', 'ed25519', 'ed448', 'x25519', 'x448', or 'dh'.
Jump to headingoptions: RSAKeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "rsa",options: RSAKeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "rsa"Jump to headingoptions: RSAKeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "rsa",options: RSAKeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "rsa"Jump to headingoptions: RSAKeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "rsa",options: RSAKeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "rsa"Jump to headingoptions: RSAKeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "rsa",options: RSAKeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "rsa"Return Type Jump to heading
Jump to headinggenerateKeyPairSync(type: "rsa-pss",options: RSAPSSKeyPairOptions<"pem", "pem">,): KeyPairSyncResult<string, string>Parameters Jump to heading
Jump to headingtype: "rsa-pss"Jump to headingoptions: RSAPSSKeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "rsa-pss",options: RSAPSSKeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "rsa-pss"Jump to headingoptions: RSAPSSKeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "rsa-pss",options: RSAPSSKeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "rsa-pss"Jump to headingoptions: RSAPSSKeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "rsa-pss",options: RSAPSSKeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "rsa-pss"Jump to headingoptions: RSAPSSKeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "rsa-pss",options: RSAPSSKeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "rsa-pss"Return Type Jump to heading
Jump to headinggenerateKeyPairSync(type: "dsa",options: DSAKeyPairOptions<"pem", "pem">,): KeyPairSyncResult<string, string>Parameters Jump to heading
Jump to headingtype: "dsa"Jump to headingoptions: DSAKeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "dsa",options: DSAKeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "dsa"Jump to headingoptions: DSAKeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "dsa",options: DSAKeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "dsa"Jump to headingoptions: DSAKeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "dsa",options: DSAKeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "dsa"Jump to headingoptions: DSAKeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "dsa",options: DSAKeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "dsa"Return Type Jump to heading
Jump to headinggenerateKeyPairSync(type: "ec",options: ECKeyPairOptions<"pem", "pem">,): KeyPairSyncResult<string, string>Parameters Jump to heading
Jump to headingtype: "ec"Jump to headingoptions: ECKeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "ec",options: ECKeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "ec"Jump to headingoptions: ECKeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "ec",options: ECKeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "ec"Jump to headingoptions: ECKeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "ec",options: ECKeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "ec"Jump to headingoptions: ECKeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "ec",options: ECKeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "ec"Return Type Jump to heading
Jump to headinggenerateKeyPairSync(type: "ed25519",options: ED25519KeyPairOptions<"pem", "pem">,): KeyPairSyncResult<string, string>Parameters Jump to heading
Jump to headingtype: "ed25519"Jump to headingoptions: ED25519KeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "ed25519",options: ED25519KeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "ed25519"Jump to headingoptions: ED25519KeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "ed25519",options: ED25519KeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "ed25519"Jump to headingoptions: ED25519KeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "ed25519",options: ED25519KeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "ed25519"Jump to headingoptions: ED25519KeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "ed25519",options?: ED25519KeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "ed25519"Jump to headingoptions: ED25519KeyPairKeyObjectOptionsReturn Type Jump to heading
Jump to headinggenerateKeyPairSync(type: "ed448",options: ED448KeyPairOptions<"pem", "pem">,): KeyPairSyncResult<string, string>Parameters Jump to heading
Jump to headingtype: "ed448"Jump to headingoptions: ED448KeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "ed448",options: ED448KeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "ed448"Jump to headingoptions: ED448KeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "ed448",options: ED448KeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "ed448"Jump to headingoptions: ED448KeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "ed448",options: ED448KeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "ed448"Jump to headingoptions: ED448KeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "ed448",options?: ED448KeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "ed448"Jump to headingoptions: ED448KeyPairKeyObjectOptionsReturn Type Jump to heading
Jump to headinggenerateKeyPairSync(type: "x25519",options: X25519KeyPairOptions<"pem", "pem">,): KeyPairSyncResult<string, string>Parameters Jump to heading
Jump to headingtype: "x25519"Jump to headingoptions: X25519KeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "x25519",options: X25519KeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "x25519"Jump to headingoptions: X25519KeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "x25519",options: X25519KeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "x25519"Jump to headingoptions: X25519KeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "x25519",options: X25519KeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "x25519"Jump to headingoptions: X25519KeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "x25519",options?: X25519KeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "x25519"Jump to headingoptions: X25519KeyPairKeyObjectOptionsReturn Type Jump to heading
Jump to headinggenerateKeyPairSync(type: "x448",options: X448KeyPairOptions<"pem", "pem">,): KeyPairSyncResult<string, string>Parameters Jump to heading
Jump to headingtype: "x448"Jump to headingoptions: X448KeyPairOptions<"pem", "pem">Return Type Jump to heading
KeyPairSyncResult<string, string>Jump to headinggenerateKeyPairSync(type: "x448",options: X448KeyPairOptions<"pem", "der">,): KeyPairSyncResult<string, Buffer>Parameters Jump to heading
Jump to headingtype: "x448"Jump to headingoptions: X448KeyPairOptions<"pem", "der">Return Type Jump to heading
KeyPairSyncResult<string, Buffer>Jump to headinggenerateKeyPairSync(type: "x448",options: X448KeyPairOptions<"der", "pem">,): KeyPairSyncResult<Buffer, string>Parameters Jump to heading
Jump to headingtype: "x448"Jump to headingoptions: X448KeyPairOptions<"der", "pem">Return Type Jump to heading
KeyPairSyncResult<Buffer, string>Jump to headinggenerateKeyPairSync(type: "x448",options: X448KeyPairOptions<"der", "der">,): KeyPairSyncResult<Buffer, Buffer>Parameters Jump to heading
Jump to headingtype: "x448"Jump to headingoptions: X448KeyPairOptions<"der", "der">Return Type Jump to heading
KeyPairSyncResult<Buffer, Buffer>Jump to headinggenerateKeyPairSync(type: "x448",options?: X448KeyPairKeyObjectOptions,): KeyPairKeyObjectResultParameters Jump to heading
Jump to headingtype: "x448"Jump to headingoptions: X448KeyPairKeyObjectOptions