-
-
Save tejasmanohar/34c3a434d05b53beb358 to your computer and use it in GitHub Desktop.
simple Node.js CSPRNG
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
/** | |
* Dependencies | |
*/ | |
import { inRange } from 'lodash' | |
import { randomBytes } from 'crypto' | |
/** | |
* CSPRNG | |
* Generates numbers between start and up to but not including, end | |
* @param {number} start - start of range, min: 0 | |
* @param {number} end - end of range, max: 255 | |
*/ | |
function random (start, end) { | |
const rand = randomBytes(1)[0] | |
return inRange(rand, start, end) | |
? rand | |
: random(start, end) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment