The createPublicKey()
function generates a new key object, with a public key, and returns it. This function no longer necessitates complex implementation methodologies and setups because the crypto module is built into Node.js.
The crypto module of Node.js includes the createPublicKey()
function. The syntax is:
crypto.createPublicKey(key)
The createPublicKey()
function only takes in one parameter:
key
the data that will be used to make the public key object. It can be any of the following types:
According to the Node.js Documentation if the key is a string or a Buffer, the format is presumed to be ‘pem’. If the key is a KeyObject of type ‘private’, the public key is extracted from the specified private key. Otherwise, the key must be a String or a Buffer.
createPublicKey()
produces a KeyObject that holds the public key as its return value.
The following code sample shows how to use the createPublicKey()
function to generate a new key object with a public key:
//import crypto moduleimport crypto from 'crypto';//generate key pairconst { generateKeyPair } = require('crypto');generateKeyPair('rsa', {modulusLength: 4096,publicKeyEncoding: {type: 'pkcs1',format: 'pem'},privateKeyEncoding: {type: 'pkcs1',format: 'pem',cipher: 'aes-256-cbc',passphrase: 'top secret'}// Handle errors and use the generated key pair}, (err, publicKey, privateKey) => {publicKey});//Assign key to variableconst publicKeyString = `-----BEGIN RSA PUBLIC KEY-----MIICCgKCAgEAzXlBBes4SpVk5BxPVuY+LR5+pxcjTNVzlVl4ugix3husiWPSfnYlK+z0zmWwGRBXncVgjRb/SliBhHDKSyqJ+gvk8RU1rO7qSFK0hGTrO63EKy4fbGgzghO0RMi+IznBqDWldd1XeBnxVUlHhjBsYNSWdFCSh6rwNTtG2Dn8GYWpXs5AUXZnL97WQot7cAekZA+P5iNjsAZoh74tY0+bRRLfIvKaXPFiH1I72R7Ky8F1NwBmOx0kLk++7OGlpDDmJuI/1d3SDmznoVWeEo1evgnCYrjGeF1QCAWe0SSRWYaDCddy2dUi1BfUIwyMHN4OSxX6TKohAPEhkbU7dU4CUyH6oEaXddhhl427UYrtv9WY32kKnY1dq/3ZjZ1LRE3cwPywHQichjyGMOZF6l6tPE6ZVQHlrgsT3GTaSEO8I4F5Z0eaEAqZiqeg9sOf9DfLdTvA5oZ9NKymnHqWndEM3gIV3KA4LP+qwVlsD/zFcuxWRHjM5r1KXvQgrLAHqfCQ7LwhUivjCjIfp2nYFYrzlDlUUZzg1AAKxhRJIUt58o7RnS/29zonPur5caj1iOrhfuTvkCM4uQyampk1A5+98aOObA97jCQ7mWbn8St8Hpdqim1c2v0sQXeyRdm8aZ5xzJ5cZ2p2NWGvGoocx4c8i9GQuHUVktQE6nuMS7aKQlECAwEAAQ==-----END RSA PUBLIC KEY-----`;//Use the function to create and export the public key objectconst publickKeyObject = crypto.createPublicKey(publicKeyString);publickKeyObject.export({ format: 'pem', type: 'pkcs1' });console.log(publickKeyObject)
We loaded the crypto module first, then generated the key pairs in the second line. The publicKeyString
variable holds the key. To return a keyObject
containing a public key, the publicKeyString
is supplied as an argument to the createPublicKey()
function. The publicKeyObject
is exported in PEM format and as a PKCS1 type.
The properties needed for encoding the pair of keys are:
Format: The format must be ‘pem’ or ‘der’, but the default format is ‘pem’.
Type: The type must be pkcs1 or pkcs8.
Cipher: The algorithm to be used for encoding the public or private key.
Passphrase: It is used for decryption if the private key is encrypted.
PEM, short for Privacy Enhanced Mail, is a file format for storing cryptographic keys.
The Public-Key Cryptography Standards (PKCS) is a mechanism for securely exchanging information over the Internet using a public key.
DER stands for Distinguished Encoding Rules. It is a binary format for the PEM file that follows certain structures for public keys.