Last active
April 14, 2024 16:47
-
-
Save dehamzah/3db8fec14d19af50f7fcba2e74bdfb26 to your computer and use it in GitHub Desktop.
Generate secret key in NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('crypto').randomBytes(48, function(err, buffer) { var token = buffer.toString('hex'); console.log(token); }); |
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'))
For me, running it in zsh requires quotes.
node -e "console.log(require('crypto').randomBytes(48).toString('hex'))"
The guys from Hapi.js recommends to use base64 encoding.
node -e "console.log(require('crypto').randomBytes(256).toString('base64'));"
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
This can also be run synchronously:
var token = require('crypto').randomBytes(48).toString('hex'); console.log(token);