Node.js crypto.generateKeyPairSync() Method
Last Updated :
11 Oct, 2021
The
crypto.generateKeyPairSync() method is an inbuilt application programming interface of crypto module which is used to generate a new asymmetric key pair of the specified type. For example, the currently supported key types are RSA, DSA, EC, Ed25519, Ed448, X25519, X448, and DH. Moreover, if option's publicKeyEncoding or privateKeyEncoding is stated here, then this function acts as if keyObject.export() had been called on its output. Else, the particular part of the key is returned as a KeyObject.
However, it is suggested to encode the public keys as 'spki' and private keys as 'pkcs8' with a strong passphrase, in order to keep the passphrase secret.
Syntax:
crypto.generateKeyPairSync( type, options )
Parameters: This method accept two parameters as mentioned above and described below:
- type: It holds a string and it must include one or more of the following algorithms: 'rsa', 'dsa', 'ec', 'ed25519', 'ed448', 'x25519', 'x448', or 'dh'.
- options: It is of type object. It can hold the following parameters:
- modulusLength: It holds a number. It is the key size in bits and is applicable for RSA, and DSA algorithm only.
- publicExponent: It holds a number. It is the Public exponent of RSA algorithm. Its by default value is 0x10001.
- divisorLength: It holds a number. It is the size of q in bits of DSA algorithm.
- namedCurve: It holds a string. It is the name of the curve to be used in EC algorithm.
- prime: It holds a buffer. It is the prime parameter of DH algorithm.
- primeLength: It holds a number. It is the prime length of DH algorithm in bits.
- generator: It holds a number. It is the custom generator of DH algorithm. Its by default value is 2.
- groupName: It holds string. It is the Diffie-Hellman group name of DH algorithm.
- publicKeyEncoding: It holds a string.
- privateKeyEncoding: It holds an Object.
Return Value: It returns a new asymmetric key pair of the given type i.e, It returns an object that includes a private key and a public key that holds the string, buffer, and KeyObject.
Below examples illustrate the use of
crypto.generateKeyPairSync() method in Node.js:
Example 1:
javascript
// Node.js program to demonstrate the
// crypto.generateKeyPairSync() method
// Including generateKeyPairSync from crypto module
const { generateKeyPairSync } = require('crypto');
// Including publicKey and privateKey from
// generateKeyPairSync() method with its
// parameters
const { publicKey, privateKey } = generateKeyPairSync('ec', {
namedCurve: 'secp256k1', // Options
publicKeyEncoding: {
type: 'spki',
format: 'der'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'der'
}
});
// Prints asymmetric key pair
console.log("The public key is: ", publicKey);
console.log();
console.log("The private key is: ", privateKey);
Output:
The public key is: <Buffer 30 56 30 10 06 07
2a 86 48 ce 3d 02 01 06 052b 81 04 00 0a 03 42
00 04 d9 88 53 5b 21 84 f8 73 14 c8 0b 31 e2 2a
28 a5 4c 8f 68 23 65 84 d9 fe 20 3f ... >
The private key is: Buffer 30 81 84 02 01 00 30
10 06 07 2a 86 48 ce 3d 02 01 06 05 2b 81 04 00
0a 04 6d 30 6b 02 01 01 04 20 50 4a 87 c3 8c
968f 2b 41 f8 66 99 8a 95 ae 45 75 ... >
Example 2:
javascript
// Node.js program to demonstrate the
// crypto.generateKeyPairSync() method
// Including generateKeyPairSync from crypto module
const { generateKeyPairSync } = require('crypto');
// Including publicKey and privateKey from
// generateKeyPairSync() method with its
// parameters
const { publicKey, privateKey } = generateKeyPairSync('dsa', {
modulusLength: 570,
publicKeyEncoding: {
type: 'spki',
format: 'der'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'der'
}
});
// Prints asymmetric key pair after encoding
console.log("The public key is: ",
publicKey.toString('base64'));
console.log();
console.log("The private key is: ",
privateKey.toString('base64'));
Output:
The public key is: MIIBETCBwAYHKoZIzjgEATCBtAJJAM6084jk1Y6s/0sWQCs3k59AjV1GgAHb8gmB+Lxd/YVid+GySyss8tqhVQl49xho1DHoeJMNsVO6mcRqaSlSCPgmzqGaOvn2mQIdAKL5nGKJjDZF8Pb1SVvwWivhPShJiiHC2JjgrN8CSAqhzmg26/kEHYTZ3yNEGuguDhLvMAPdVG9pjTahLBytn8JQa3yQwLuPB4MzKfJ4d0pvKVZVnkMsatUe2ZkjnKoCjGlzwggd+QNMAAJJAMvsOBUjUKLhpkw4FZP7LIz0yYyOV1yYy84t8qSO42Yf6sNUfK6INnkFbpLHjFLcaDkFPqE5oRCIUqIVOhH0I7jNcGCN2m+ZWg==
The private key is: MIHnAgEAMIHABgcqhkjOOAQBMIG0AkkAzrTziOTVjqz/SxZAKzeTn0CNXUaAAdvyCYH4vF39hWJ34bJLKyzy2qFVCXj3GGjUMeh4kw2xU7qZxGppKVII+CbOoZo6+faZAh0AovmcYomMNkXw9vVJW/BaK+E9KEmKIcLYmOCs3wJICqHOaDbr+QQdhNnfI0Qa6C4OEu8wA91Ub2mNNqEsHK2fwlBrfJDAu48HgzMp8nh3Sm8pVlWeQyxq1R7ZmSOcqgKMaXPCCB35BB8CHQChh8yXsr/3dx3OdV6JmIWPcHe6dTHh6IOEIHer
Here, 'dsa' key type is used.
Reference: https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypairsync_type_options
Similar Reads
Node.js cipher.final() Method The cipher.final() method in Node.js is used to signal to the cipher object that the encryption or decryption process is complete. This method must be called after all data has been passed to the cipher object using the cipher.update() method. The cipher.final() method returns the remaining encrypte
2 min read
Node.js cipher.update() Method The cipher.update() method is an inbuilt application programming interface of class Cipher within crypto module which is used to update the cipher with data according to the given encoding format. Syntax: const cipher.update(data[, inputEncoding][, outputEncoding]) Parameters: This method takes the
2 min read
Node.js crypto.getCiphers() Method The crypto.getCiphers() method returns an array the names of all the supported cipher algorithms. Syntax: crypto.getCiphers() Parameters: This method doesn't accepts any parameters. Return Value: It returns the names of all the supported cipher algorithms. Below example illustrate the use of crypto.
2 min read
Node.js crypto.createECDH() Method The crypto.createECDH() method is an inbuilt application programming interface of crypto module which is used to create an Elliptic Curve Diffie-Hellman i.e, (ECDH) key exchange object with the help of a predefined curve which is defined by the curveName string. Moreover you can use crypto.getCurves
2 min read
Node.js crypto.createDecipheriv() Method The crypto.createDecipheriv() method is an inbuilt application programming interface of crypto module which is used to create a Decipher object, with the stated algorithm, key and initialization vector i.e, (iv). Syntax: crypto.createDecipheriv( algorithm, key, iv, options ) Parameters: This method
3 min read
Node crypto.createCipheriv() Method The crypto.createCipheriv() method is an inbuilt application programming interface of the crypto module which is used to create a Cipher object, with the stated algorithm, key, and initialization vector (iv).Syntax: crypto.createCipheriv( algorithm, key, iv, options )Parameters: This method accepts
2 min read
Node.js crypto.getDiffieHellman() Method The crypto.getDiffieHellman() method is used to create a predefined DiffieHellmanGroup key exchange object. Here, the favored groups are 'modp1', 'modp2', 'modp5', which are defined in RFC 2412 and 'modp14', 'modp15', 'modp16', 'modp17', 'modp18', defined in RFC 3526. Syntax: crypto.getDiffieHellman
2 min read
Node.js crypto.pbkdf2() Method The crypto.pbkdf2() method gives an asynchronous Password-Based Key Derivation Function 2 i.e. (PBKDF2) implementation. Moreover, a particular HMAC digest algorithm which is defined by digest is implemented to derive a key of the required byte length (keylen) from the stated password, salt, and iter
2 min read
Node crypto.createHash() Method The crypto.createHash() method is used to create a Hash object that can be used to create hash digests by using the stated algorithm. Syntax:crypto.createHash( algorithm, options )Parameters: This method accepts two parameters as mentioned above and described below:algorithm: It is dependent on the
2 min read
Node.js crypto.createHmac() Method The crypto.createHmac() method is used to create an Hmac object that uses the stated 'algorithm' and 'key'.Syntax:crypto.createHmac( algorithm, key, options )Parameters: This method accepts three parameters as mentioned above and described below:algorithm: It is dependent on the accessible algorithm
2 min read