This is a node.js package that implements hat's main API, but uses a cryptographically secure pseudo-random number generator to generate the random identifiers. This is especially beneficial when using older versions of V8) that exhibit this bug.
In our use cases,
cryptohattakes up to 3x more time to produce a random string than
cryptohatalso exposes an alternative API that produces random numbers up to 2x faster than
hat, while still using a CSPRNG.
PrerequisitesThis package should work on any reasonably modern browser or node.js version.
Every commit is tested using continuous integration on node.js 0.10 and above. Releases are also tested against the most recent versions of Chrome, Firefox, Safari, and Internet Explorer.
InstallationInstall using npm or bower.
npm install email@example.com --save bower install firstname.lastname@example.org --save
hat(bits, base)API. The function takes in the desired number of bits of randomness and the base/radix that will be used to represent the returned random number, and returns a string. The returned strings are guaranteed to have the same length for a given bits/base combination. As long as you don't need the
cryptohatcan be used as a drop-in replacement.
var hat = require('cryptohat'); hat(); // '39a00e331acce7516a8ea69b85e191f0' hat(); // '00549f7401ac0ba9ea1b791f50bc7b1e' hat(53, 10); // '6738095220277140'
For maximum throughput, use the
cryptohat.generatorAPI. It takes exactly the same arguments as the
hatAPI, but it returns a generator function. Calling the function yields random numbers or identifiers.
var rng1 = cryptohat.generator(53, 0); rng1(); // 4438236126178078 rng1(); // 187896805323588 var rng2 = cryptohat.generator(32, 10); rng2(); // '0053668130' rng2(); // '1939036909'
DevelopmentAfter cloning the repository, install the dependencies.
npm install node node_modules/.bin/bower install
Make sure the tests pass after making a change.
SKIP_BENCHMARKS=1 npm test
When adding new functionality, make sure it has good test coverage and that it does not regress the code's performance.
SKIP_BENCHMARKS=1 npm run cov npm test
When adding new functionality, also make sure that the documentation looks reasonable.
npm run doc
When modifying code around or inside feature detection blocks (combinations of
typeof), make sure the tests pass at least in Chrome and Firefox, by opening test/index.html in the browsers.
open test/index.html # On OSX. xdg-open test/index.html # On Linux.
When testing against a browser in a VM (e.g., for Internet Explorer), spawn a local Web server inside the source tree and visit it inside the VM (e.g.,
If you submit a pull request, Travis CI will run the test suite against your code on the node versions that we support. Please fix any errors that it reports.