Skip to content

Instantly share code, notes, and snippets.

@carlok
Last active March 27, 2017 10:20
Show Gist options
  • Save carlok/bf93b8cd9ab14e537266a539a28f6892 to your computer and use it in GitHub Desktop.
Save carlok/bf93b8cd9ab14e537266a539a28f6892 to your computer and use it in GitHub Desktop.
Random Bytes Node Module
// based on https://blog.tompawlak.org/generate-random-values-nodejs-javascript
const crypto = require('crypto');
function randomValueHex (len) {
return crypto.randomBytes(Math.ceil(len / 2))
.toString('hex') // convert to hexadecimal format
.slice(0, len); // return required number of characters
}
module.exports = {
randomValueHex: randomValueHex
};
/*
// usage:
const mRand = require('./mRandom.js');
console.log(mRand.randomValueHex(32));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment