• type: {string} Must be 'rsa', 'dsa', 'ec', 'ed25519', 'ed448', 'x25519', 'x448', or 'dh'.
    • options: {Object}
      • modulusLength: {number} Key size in bits (RSA, DSA).
      • publicExponent: {number} Public exponent (RSA). Default: 0x10001.
      • divisorLength: {number} Size of q in bits (DSA).
      • namedCurve: {string} Name of the curve to use (EC).
      • prime: {Buffer} The prime parameter (DH).
      • primeLength: {number} Prime length in bits (DH).
      • generator: {number} Custom generator (DH). Default: 2.
      • groupName: {string} Diffie-Hellman group name (DH). See [crypto.getDiffieHellman()][].
      • publicKeyEncoding: {Object} See [keyObject.export()][].
      • privateKeyEncoding: {Object} See [keyObject.export()][].
    • callback: {Function}
      • err: {Error}
      • publicKey: {string | Buffer | KeyObject}
      • privateKey: {string | Buffer | KeyObject}

    Generates a new asymmetric key pair of the given type. RSA, 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:

    1. const { generateKeyPair } = require('crypto');
    2. generateKeyPair('rsa', {
    3. modulusLength: 4096,
    4. publicKeyEncoding: {
    5. type: 'spki',
    6. format: 'pem'
    7. },
    8. privateKeyEncoding: {
    9. type: 'pkcs8',
    10. format: 'pem',
    11. cipher: 'aes-256-cbc',
    12. passphrase: 'top secret'
    13. }
    14. }, (err, publicKey, privateKey) => {
    15. // Handle errors and use the generated key pair.
    16. });

    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.