Node crypto.generateKeyPair function | API Reference | Bun
BuildDocsReferenceGuidesBlogDiscord/
node:crypto/
generateKeyPairNgenerateKeyPair
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
node:crypto/
generateKeyPairNgenerateKeyPair
namespace
crypto.generateKeyPair {
// 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." data-algolia-static="false" data-algolia-merged="true" data-type="Namespace">function
generateKeyPairT extends
DHKeyPairOptions>(type: 'dh',options: T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
DSAKeyPairOptions>(type: 'dsa',options: T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
ECKeyPairOptions>(type: 'ec',options: T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
ED25519KeyPairOptions = {}>(type: 'ed25519',options: undefined | T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
ED448KeyPairOptions = {}>(type: 'ed448',options: undefined | T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
MLDSAKeyPairOptions = {}>(type: 'ml-dsa-44' | 'ml-dsa-65' | 'ml-dsa-87',options: undefined | T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
MLKEMKeyPairOptions = {}>(type: 'ml-kem-1024' | 'ml-kem-512' | 'ml-kem-768',options: undefined | T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
RSAPSSKeyPairOptions>(type: 'rsa-pss',options: T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
RSAKeyPairOptions>(type: 'rsa',options: T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
SLHDSAKeyPairOptions = {}>(type: 'slh-dsa-sha2-128f' | 'slh-dsa-sha2-128s' | 'slh-dsa-sha2-192f' | 'slh-dsa-sha2-192s' | 'slh-dsa-sha2-256f' | 'slh-dsa-sha2-256s' | 'slh-dsa-shake-128f' | 'slh-dsa-shake-128s' | 'slh-dsa-shake-192f' | 'slh-dsa-shake-192s' | 'slh-dsa-shake-256f' | 'slh-dsa-shake-256s',options: undefined | T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
X25519KeyPairOptions = {}>(type: 'x25519',options: undefined | T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
function
generateKeyPairT extends
X448KeyPairOptions = {}>(type: 'x448',options: undefined | T,callback:
KeyPairExportCallbackT>): void;
Generates a new asymmetric key pair of the given type. See the supported asymmetric key types.
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.
@param type
The asymmetric key type to generate. See the supported asymmetric key types.
namespace
generateKeyPairResources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →