Skip to content

Instantly share code, notes, and snippets.

@skeggse
Created March 3, 2014 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skeggse/9337261 to your computer and use it in GitHub Desktop.
Save skeggse/9337261 to your computer and use it in GitHub Desktop.
Generate crypto-random hex from command line because /dev/urandom isn't good enough for some reason
#!/usr/bin/env node
var crypto = require('crypto');
var chars = parseInt(process.argv[2], 10) || 32;
var blockSize = 1024 * 1024;
function sync(count) {
console.log(crypto.randomBytes(Math.ceil(count / 2)).toString('hex').slice(0, count));
}
function more(remain) {
if (remain <= blockSize)
return sync(remain);
crypto.randomBytes(blockSize / 2, function(err, data) {
if (err) {
console.error(err.message);
return process.exit(1);
}
process.stdout.write(data.toString('hex'));
more(remain - blockSize);
});
}
more(chars);
@skeggse
Copy link
Author

skeggse commented Mar 4, 2014

It does appear to be a little faster than the bash solution I tested (which included converting to hex).

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