pax_global_header00006660000000000000000000000064126466407420014525gustar00rootroot0000000000000052 comment=3dcd47425a3dfe858ee8debcd4db0c1222110bc3 random-bytes-1.0.0/000077500000000000000000000000001264664074200141275ustar00rootroot00000000000000random-bytes-1.0.0/.gitignore000066400000000000000000000000561264664074200161200ustar00rootroot00000000000000 .DS_Store* *.log *.gz node_modules coverage random-bytes-1.0.0/.travis.yml000066400000000000000000000010471264664074200162420ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.2" - "5.4" sudo: false before_install: # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul" script: # Run test script, depending on istanbul install - "test ! -z $(npm -ps ls istanbul) || npm test" - "test -z $(npm -ps ls istanbul) || npm run-script test-travis" after_script: - "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" random-bytes-1.0.0/HISTORY.md000066400000000000000000000000731264664074200156120ustar00rootroot000000000000001.0.0 / 2016-01-17 ================== * Initial release random-bytes-1.0.0/LICENSE000066400000000000000000000021361264664074200151360ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. random-bytes-1.0.0/README.md000066400000000000000000000041151264664074200154070ustar00rootroot00000000000000# random-bytes [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] Generate strong pseudo-random bytes. This module is a simple wrapper around the Node.js core `crypto.randomBytes` API, with the following additions: * A `Promise` interface for environments with promises. * For Node.js versions that do not wait for the PRNG to be seeded, this module will wait a bit. ## Installation ```sh $ npm install random-bytes ``` ## API ```js var randomBytes = require('random-bytes') ``` ### randomBytes(size, callback) Generates strong pseudo-random bytes. The `size` argument is a number indicating the number of bytes to generate. ```js randomBytes(12, function (error, bytes) { if (error) throw error // do something with the bytes }) ``` ### randomBytes(size) Generates strong pseudo-random bytes and return a `Promise`. The `size` argument is a number indicating the number of bytes to generate. **Note**: To use promises in Node.js _prior to 0.12_, promises must be "polyfilled" using `global.Promise = require('bluebird')`. ```js randomBytes(18).then(function (string) { // do something with the string }) ``` ### randomBytes.sync(size) A synchronous version of above. ```js var bytes = randomBytes.sync(18) ``` ## License [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/random-bytes.svg [npm-url]: https://npmjs.org/package/random-bytes [node-version-image]: https://img.shields.io/node/v/random-bytes.svg [node-version-url]: http://nodejs.org/download/ [travis-image]: https://img.shields.io/travis/crypto-utils/random-bytes/master.svg [travis-url]: https://travis-ci.org/crypto-utils/random-bytes [coveralls-image]: https://img.shields.io/coveralls/crypto-utils/random-bytes/master.svg [coveralls-url]: https://coveralls.io/r/crypto-utils/random-bytes?branch=master [downloads-image]: https://img.shields.io/npm/dm/random-bytes.svg [downloads-url]: https://npmjs.org/package/random-bytes random-bytes-1.0.0/index.js000066400000000000000000000037121264664074200155770ustar00rootroot00000000000000/*! * random-bytes * Copyright(c) 2016 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var crypto = require('crypto') /** * Module variables. * @private */ var generateAttempts = crypto.randomBytes === crypto.pseudoRandomBytes ? 1 : 3 /** * Module exports. * @public */ module.exports = randomBytes module.exports.sync = randomBytesSync /** * Generates strong pseudo-random bytes. * * @param {number} size * @param {function} [callback] * @return {Promise} * @public */ function randomBytes(size, callback) { // validate callback is a function, if provided if (callback !== undefined && typeof callback !== 'function') { throw new TypeError('argument callback must be a function') } // require the callback without promises if (!callback && !global.Promise) { throw new TypeError('argument callback is required') } if (callback) { // classic callback style return generateRandomBytes(size, generateAttempts, callback) } return new Promise(function executor(resolve, reject) { generateRandomBytes(size, generateAttempts, function onRandomBytes(err, str) { if (err) return reject(err) resolve(str) }) }) } /** * Generates strong pseudo-random bytes sync. * * @param {number} size * @return {Buffer} * @public */ function randomBytesSync(size) { var err = null for (var i = 0; i < generateAttempts; i++) { try { return crypto.randomBytes(size) } catch (e) { err = e } } throw err } /** * Generates strong pseudo-random bytes. * * @param {number} size * @param {number} attempts * @param {function} callback * @private */ function generateRandomBytes(size, attempts, callback) { crypto.randomBytes(size, function onRandomBytes(err, buf) { if (!err) return callback(null, buf) if (!--attempts) return callback(err) setTimeout(generateRandomBytes.bind(null, size, attempts, callback), 10) }) } random-bytes-1.0.0/package.json000066400000000000000000000016531264664074200164220ustar00rootroot00000000000000{ "name": "random-bytes", "description": "URL and cookie safe UIDs", "version": "1.0.0", "contributors": [ "Douglas Christopher Wilson " ], "license": "MIT", "repository": "crypto-utils/random-bytes", "devDependencies": { "bluebird": "3.1.1", "istanbul": "0.4.2", "mocha": "2.3.4", "proxyquire": "1.2.0" }, "files": [ "LICENSE", "HISTORY.md", "README.md", "index.js" ], "engines": { "node": ">= 0.8" }, "scripts": { "test": "mocha --trace-deprecation --reporter spec --bail --check-leaks test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --trace-deprecation --reporter dot --check-leaks test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --trace-deprecation --reporter spec --check-leaks test/" }, "keywords": [ "bytes", "generator", "random", "safe" ] } random-bytes-1.0.0/test/000077500000000000000000000000001264664074200151065ustar00rootroot00000000000000random-bytes-1.0.0/test/test.js000066400000000000000000000116371264664074200164330ustar00rootroot00000000000000var assert = require('assert') var crypto = require('crypto') var proxyquire = require('proxyquire') var Promise = global.Promise || require('bluebird') var randomBytes = proxyquire('..', { crypto: { randomBytes: cryptoRandomBytes } }) // Add Promise to mocha's global list global.Promise = global.Promise describe('randomBytes(size)', function () { describe('with global Promise', function () { before(function () { global.Promise = Promise }) after(function () { global.Promise = undefined }) it('should return a Buffer of the correct length', function () { return randomBytes(18).then(function (buf) { assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) }) }) describe('when PRNG not seeded', function () { afterEach(function () { cryptoRandomBytes.seeded = undefined }) it('should still generate bytes when later seeded', function () { cryptoRandomBytes.seeded = 1 return randomBytes(18).then(function (buf) { assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) }) }) it('should attempt generation three times', function () { cryptoRandomBytes.seeded = 2 return randomBytes(18).then(function (buf) { assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) }) }) it('should reject if never seeded', function () { cryptoRandomBytes.seeded = false return randomBytes(18).then(unexpectedResolve, function (err) { assert.notEqual(err.message.indexOf('PRNG not seeded'), -1) }) }) }) }) describe('without global Promise', function () { before(function () { global.Promise = undefined }) after(function () { global.Promise = Promise }) it('should require callback', function () { assert.throws(function () { randomBytes(18) }, /argument callback.*required/) }) it('should error for bad callback', function () { assert.throws(function () { randomBytes(18, 'silly') }, /argument callback.*function/) }) it('should return a Buffer of the correct length', function (done) { randomBytes(18, function (err, buf) { if (err) return done(err) assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) done() }) }) describe('when PRNG not seeded', function () { afterEach(function () { cryptoRandomBytes.seeded = undefined }) it('should still generate bytes when later seeded', function () { cryptoRandomBytes.seeded = 1 randomBytes(18, function (err, buf) { if (err) return done(err) assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) done() }) }) it('should attempt generation three times', function () { cryptoRandomBytes.seeded = 2 randomBytes(18, function (err, buf) { if (err) return done(err) assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) done() }) }) it('should error if never seeded', function () { cryptoRandomBytes.seeded = false randomBytes(18, function (err) { assert.ok(err) assert.notEqual(err.message.indexOf('PRNG not seeded'), -1) done() }) }) }) }) }) describe('randomBytes.sync()', function () { it('should return a Buffer of the correct length', function () { var buf = randomBytes.sync(18) assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) }) describe('when PRNG not seeded', function () { afterEach(function () { cryptoRandomBytes.seeded = undefined }) it('should still generate bytes when later seeded', function () { cryptoRandomBytes.seeded = 1 var buf = randomBytes.sync(18) assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) }) it('should attempt generation three times', function () { cryptoRandomBytes.seeded = 2 var buf = randomBytes.sync(18) assert.ok(Buffer.isBuffer(buf)) assert.equal(18, buf.length) }) it('should error if never seeded', function () { cryptoRandomBytes.seeded = false assert.throws(randomBytes.sync.bind(randomBytes, 18), /PRNG not seeded/) }) }) }) function cryptoRandomBytes(size, callback) { if (cryptoRandomBytes.seeded === undefined) { return crypto.randomBytes(size, callback) } // The crazy not seeded error var err = new Error('error:24064064:random number generator:SSLEAY_RAND_BYTES:PRNG not seeded') if (typeof cryptoRandomBytes.seeded === 'number' && !--cryptoRandomBytes.seeded) { // Reset seeded state cryptoRandomBytes.seeded = undefined } if (!callback) { throw err } callback(err) } function unexpectedResolve() { throw new Error('unexpected resolve') }