Skip to content

Instantly share code, notes, and snippets.

@dehamzah
Last active April 14, 2024 16:47
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save dehamzah/3db8fec14d19af50f7fcba2e74bdfb26 to your computer and use it in GitHub Desktop.
Save dehamzah/3db8fec14d19af50f7fcba2e74bdfb26 to your computer and use it in GitHub Desktop.
Generate secret key in NodeJS
require('crypto').randomBytes(48, function(err, buffer) { var token = buffer.toString('hex'); console.log(token); });
@olliechick
Copy link

This can also be run synchronously: var token = require('crypto').randomBytes(48).toString('hex'); console.log(token);

@theo-rogers
Copy link

You don't even need the var: console.log(require('crypto').randomBytes(48).toString('hex'))
you could go even further and run it as a bash command node -e console.log(require('crypto').randomBytes(48).toString('hex'))

@jack-hermanson
Copy link

For me, running it in zsh requires quotes.
node -e "console.log(require('crypto').randomBytes(48).toString('hex'))"

@dms120
Copy link

dms120 commented Jul 5, 2022

The guys from Hapi.js recommends to use base64 encoding.
node -e "console.log(require('crypto').randomBytes(256).toString('base64'));"

@RonyMiah
Copy link

node -e "console.log( require('crypto').randomBytes(24).toString('base64url'))"

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