In node.js crypto module, I generate a public/private key, but how do I save it to a file and load it back into memory from file? So far I have this
https://nodejs.org/api/crypto.html
const crypto = require("crypto")
const fs = require('fs');
// The `generateKeyPairSync` method accepts two arguments:
// 1. The type ok keys we want, which in this case is "rsa"
// 2. An object with the properties of the key
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
})
fs.writeFileSync("public.pem", publicKey);
fs.writeFileSync("private.pem", privateKey);
But I get this error
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of PublicKeyObject
Does anyone know?
Thanks