secure-random
A simple JavaScript component to normalize the creation of cryptographically strong random values.
Why?
Context switching between the browser and Node.js and creating cryptographically secure random numbers is annoying. This normalizes the behavior. Used by CryptoCoinJS and BitcoinJS.
Install
Node.js/Browserify
npm install --save secure-random
Component
component install jprichardson/secure-random
Bower
bower install secure-random
Script
<script src="/path/to/secure-random.js"></script>
Usage
secureRandom(byteCount, options)
- byteCount: is the number of bytes to return.
- options: options to pass. Only valid value at this time
type
. type
can be
either Array
, Uint8Array
, or Buffer
. Buffer
is only valid in Node.js or
Browserify environments - it will throw an error otherwise.
return an Array
:
var bytes = secureRandom(10)
console.log(bytes.length)
or:
var bytes = secureRandom(10, {type: 'Array'})
console.log(bytes.length)
return a Buffer
:
var bytes = secureRandom(10, {type: 'Buffer'})
console.log(bytes.length)
return a Uint8Array
:
var bytes = secureRandom(10, {type: 'Uint8Array'})
console.log(bytes.length)
randomArray(byteLength)
Sugar for secureRandom(byteLength, {type: 'Array'})
.
var secureRandom = require('secure-random')
var data = secureRandom.randomArray(10)
randomUint8Array(byteLength)
Sugar for secureRandom(byteLength, {type: 'Uint8Array'})
.
var secureRandom = require('secure-random')
var data = secureRandom.randomUint8Array(10)
randomBuffer(byteLength)
Sugar for secureRandom(byteLength, {type: 'Buffer'})
.
var secureRandom = require('secure-random')
var data = secureRandom.randomBuffer(10)
References
License
(MIT License)
Copyright 2013-2014, JP Richardson