Tuesday, 29 March 2022

Access a .pem public key from .env file

I am storing a public key in a env variable as a string. This public key is from a .pem file. When I try to use it in my code, I get the following error

error:0909006C:PEM routines:get_name:no start line

I have tried what other users have suggested, by converting it to base64 and then using the key, but I still get the same error.

env variable for the public key

PUB_KEY='-----BEGIN PUBLIC KEY-----randomgibberish-----END PUBLIC KEY-----'

Code for converting it to base64

const pubKey = process.env.PUB_KEY
const buff = Buffer.from(pubKey).toString('base64');
console.log(buff)

Using it in the createPublicKey method here

crypto.createPublicKey({
                key: buff,
                format: 'pem',
            });

Any idea what could be going wrong? TIA



from Access a .pem public key from .env file

No comments:

Post a Comment