Skip to content

Instantly share code, notes, and snippets.

@tomi
Last active March 14, 2024 03:28
Show Gist options
  • Save tomi/0675e58919af4554b198cee3f84405e5 to your computer and use it in GitHub Desktop.
Save tomi/0675e58919af4554b198cee3f84405e5 to your computer and use it in GitHub Desktop.
Saving and reading private key to env variables using base64

Encoding the private key as base64

  • Copy the private key to clipboard
  • Run command pbpaste | base64 | pbcopy
  • The private key is now base64 encoded in the clipboard. Paste it to env variable e.g. to heroku or to .env file

Decoding the private key from base64 in node.js

const private_key = new Buffer(process.env.PRIVATE_KEY, 'base64').toString('ascii');
@thinkjrs
Copy link

❤️ this and thank you. On systems with xclip and without pbcopy and pbpaste, try:

xclip -selection clipboard -o | base64 -w 0 | xclip -selection clipboard

ℹ️ The base64 -w 0 also removes the newlines from the output per https://superuser.com/a/1225139.

@hadbits
Copy link

hadbits commented May 21, 2023

You can always conveniently encode using notepad++ MIME Tools as well:
https://www.technipages.com/how-to-base64-encode-and-decode-in-notepad/

@ammarorokushorking
Copy link

0xB8c77482e45F1F44dE1745F52C74426C631bDD52

@guendev
Copy link

guendev commented Sep 18, 2023

new Buffer is deprecated. We should use Buffer.from(env.GOOGLE_CREDENTIALS, 'base64').toString('ascii') instead.

@ammarorokushorking
Copy link

Private key find

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment