javascript-fortuna
Javascript implementation of the Fortuna PRNG.
Installation
npm i javascript-fortuna
Basic Usage
This is a quick out-of-the-box usage example. This is not how you'd use it in production if you want it to be secure, but it will give you a decent random number.
const fortuna = ; fortuna;const randomNumber = fortuna; console;
Command-line Usage
Javascript Fortuna comes with a simple command-line app that will generate a single random number seeded by your local environment.
$ js-fortuna0.7947502068732222
Advanced Usage
To reduce predictability add entropy from dynamic sytem state inforation such as CPU usage, number of active processes, availalbe ram and disk io.
const fortuna = ;const si = ;const sha512 = ;const jsspg = ; { return async { const cpuSpeed = await si; const processes = await si; const disksIO = await si; const memory = await si; jsspgentropyVal = ; console; ; };} { return jsspgentropyVal;} let entropyInterval = ; jsspginitialized = true; fortuna; const num1 = fortuna;console; ;
Building for Browsers
This will generate a ./build/fortuna.min.js file for use in a web browser.
$ npm run webpack
Basic Browser Usage
<script src="js/fortuna.min.js"></script><script> { fortuna; var randomNumber = fortuna ;};</script>
Core Concept
Fortuna is a method of generating random numbers using AES encryption and an environment-based seed. It is more secure than simply using Math.random().
API
fortuna.init(options)
Options [{ k: v }]
- entropyFxn [function fxn()]: Custom entropy function. Must return an Array or string of length fortuna.entropySz (128 by default)
- timeBasedEntropy [bool]: Detaches the reseeding of the algorithm from the call to random().
- accumulateTimeout [int]: The amount of time in milliseconds between each timeBasedEntropy call. Requires timeBasedEntropy to be true.
fortuna.random()
Generates a random floating point value (0,1).
If you need an integer between min and max you can simply
const min = 4;const max = 10;const randomInt = ;