crypto.createPrivateKey
is a function that accepts one argument and returns a new key (object(KeyObject)
) that contains a private key.
crypto.createPrivateKey(key)
The
crypto.createPrivatekey
function was added in node version 11.6.0, earlier versions of node don’t support this function.
If the key is not a string or buffer, then the key must be an object with the following properties.
string
ArrayBuffer
Buffer
TypedArray
DataView
Object
pem
and the format must be a string
.der
; otherwise, it is ignored. The type must be a string
.string
or Buffer
.The crypto.createPrivateKey
function returns a new Key Object
.
The code snippet below shows how to use the crypto.createPrivateKey
function.
The createPrivateKey
function uses the key
argument to create and return a new key object.
const crypto = require("crypto");//generate encrypted privateKeyconst {publicKey, privateKey } = crypto.generateKeyPairSync('rsa',{modulusLength: 4096,publicKeyEncoding: {type: 'spki',format: 'pem'},privateKeyEncoding: {type: 'pkcs8',format: 'pem',cipher: 'aes-256-cbc',passphrase: ''}});//generate key Objectconst keyObject = crypto.createPrivateKey({key: privateKey,format: "pem",type: "pkcs1",passphrase: "",encoding: "utf-8"});export default keyObject;
crypto.generateKeyPairSync
function to synchronously get privateKey
.crypto.generateKeyPairSync
to the crypto.createPrivateKey
function. This, in turn, returns a keyObject
.keyObject
; thus, making it available for use on other modules.Free Resources