Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sethblack/javascript-fortuna
Javascript implementation of the Fortuna PRNG
https://github.com/sethblack/javascript-fortuna
cryptography fortuna-prng javascript javascript-fortuna prng random random-number-generators
Last synced: about 1 month ago
JSON representation
Javascript implementation of the Fortuna PRNG
- Host: GitHub
- URL: https://github.com/sethblack/javascript-fortuna
- Owner: sethblack
- License: apache-2.0
- Created: 2017-11-14T14:46:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-22T21:45:44.000Z (over 5 years ago)
- Last Synced: 2024-10-07T13:07:10.222Z (about 1 month ago)
- Topics: cryptography, fortuna-prng, javascript, javascript-fortuna, prng, random, random-number-generators
- Language: JavaScript
- Size: 151 KB
- Stars: 10
- Watchers: 4
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
```javascript
const fortuna = require('javascript-fortuna');fortuna.init();
const randomNumber = fortuna.random();console.log(`I picked ${randomNumber}!`);
```## Command-line Usage
Javascript Fortuna comes with a simple command-line app that will generate a single random number seeded by your local environment.
```shell
$ js-fortuna
0.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.
```javascript
const fortuna = require('javascript-fortuna');
const si = require('systeminformation');
const sha512 = require('js-sha512');
const jsspg = require('javascript-strong-password-generator');function entropyAccumFunction() {
return new Promise(async (resolve) => {
const cpuSpeed = await si.cpu();
const processes = await si.processes();
const disksIO = await si.disksIO();
const memory = await si.mem();jsspg.entropyVal = sha512(`${JSON.stringify(cpuSpeed)}:${JSON.stringify(processes)}:${JSON.stringify(disksIO)}:${JSON.stringify(memory)}`);
console.log(`ent: ${jsspg.entropyVal}`);
resolve();
});
}function entropyFunction() {
return jsspg.entropyVal;
}let entropyInterval = setInterval(async () => {
await entropyAccumFunction();
}, 250);jsspg.initialized = true;
fortuna.init({ timeBasedEntropy: true, accumulateTimeout: 100, entropyFxn: entropyFunction });
const num1 = fortuna.random();
console.log(`I picked ${num1}!`);setTimeout(() => {
const num1 = fortuna.random();
console.log(`I picked ${num1}!`);
fortuna.stopTimer();
clearInterval(entropyInterval);
}, 5000);
```# Building for Browsers
This will generate a ./build/fortuna.min.js file for use in a web browser.
```shell
$ npm run webpack
```## Basic Browser Usage
```javascript
(function () {
fortuna.init();var randomNumber = fortuna.random()
alert('I picked ' + randomNumber + '!');
})();```
# 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
```javascript
const min = 4;
const max = 10;
const randomInt = parseInt((fortuna.random() * (max - min)) + min);
```# Visual Inspection
![Random Pixel Test](https://github.com/sethblack/javascript-fortuna/blob/master/boAllenNoEntropy.png)