Last active
March 27, 2017 10:20
-
-
Save carlok/bf93b8cd9ab14e537266a539a28f6892 to your computer and use it in GitHub Desktop.
Random Bytes Node Module
This file contains hidden or 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
// 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