Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

cryptography
hashing
node.js
communitycreator

What is Node crypto.createPrivateKey?

Oluchi Oraekwe

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Cryptography, generally, is the process of converting plain text into an encoded format that is very difficult to decode.

There are different methods of achieving this, but this write up will focus on using privateKey generation to achieve this. The encoding that one will use depends on what the person wants to achieve and the level of security the person wants to achieve.

privateKey is an advanced method of encrypting a string so that someone without access cannot have access to it. In most cases, the private key is not created in isolation. A function called crypto.generateKeyPair is used to generate both the private and public key. It is left for the user to determine which key that they want to use.

The Node.js crypto module can be used to generate a key pair, a public key and private key.

The private key can be generated by first importing the crypto node module in the current working file, then creating the function, like this:

crypto.generateKeyPair(type, options, callback)

The function accepts three parameters, as follows:

  1. type: This is the string format in which you want your encoded string to be stored. Such formats are as follows: RSA, DSA, EC, Ed25519, Ed448, etc.

  2. options: This contains the bulk of the input to the functions. It houses the key pair parameters alongside others. The hashing algorithm is also specified there. The following parameters can be seen: modulusLength, publicExponent, Default value, divisorLength, namedCurve, prime, primeLength, generator, groupName, publicKeyEncoding, and privateKeyEncoding.

  3. callback: This returns the encoded output if the code runs correctly.

The sample code snippet is shown below:

//Code snippet for generating rsa key pair
const crypto = require("crypto");
const RSA = "rsa";
const wordToEncode = "This is my secret key word";
let options = {
modulusLength: 1024 * 2,
publicKeyEncoding: {
type: "spki",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
cipher: "aes-256-cbc",
passphrase: wordToEncode,
},
};
let start = Date.now();
let myCallback = (err, publicKey, privateKey) => {
if (!err) {
console.log("\n");
console.log(privateKey);
let end = Date.now();
console.log(
"\n> Process completed successfully in " +
(end - start) +
" milliseconds."
);
} else {
throw err;
}
};
// calling the function
crypto.generateKeyPair(RSA, options, myCallback);
Encoded Key output

Upon clicking the run button of the code snippet, one will see a long alpha numeric string that will be generated, which is the private key.

The sample code snippet above highlights the fact that the privateKey option requires additional input: a hashing algorithm and an input text to be hashed apart from the ones in the publicKey. The private key offers more security features than the public key. The string is usually longer than the private key.

RELATED TAGS

cryptography
hashing
node.js
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring