pax_global_header00006660000000000000000000000064134330044150014507gustar00rootroot0000000000000052 comment=f18ded32b209f0d4c637608a11ae042ae96b4c2e randombytes-2.1.0/000077500000000000000000000000001343300441500140365ustar00rootroot00000000000000randombytes-2.1.0/.gitignore000066400000000000000000000000151343300441500160220ustar00rootroot00000000000000node_modules randombytes-2.1.0/.travis.yml000066400000000000000000000004771343300441500161570ustar00rootroot00000000000000sudo: false language: node_js matrix: include: - node_js: '7' env: TEST_SUITE=test - node_js: '6' env: TEST_SUITE=test - node_js: '5' env: TEST_SUITE=test - node_js: '4' env: TEST_SUITE=test - node_js: '4' env: TEST_SUITE=phantom script: "npm run-script $TEST_SUITE" randombytes-2.1.0/.zuul.yml000066400000000000000000000000111343300441500156260ustar00rootroot00000000000000ui: tape randombytes-2.1.0/LICENSE000066400000000000000000000020621343300441500150430ustar00rootroot00000000000000MIT License Copyright (c) 2017 crypto-browserify 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. randombytes-2.1.0/README.md000066400000000000000000000010771343300441500153220ustar00rootroot00000000000000randombytes === [![Version](http://img.shields.io/npm/v/randombytes.svg)](https://www.npmjs.org/package/randombytes) [![Build Status](https://travis-ci.org/crypto-browserify/randombytes.svg?branch=master)](https://travis-ci.org/crypto-browserify/randombytes) randombytes from node that works in the browser. In node you just get crypto.randomBytes, but in the browser it uses .crypto/msCrypto.getRandomValues ```js var randomBytes = require('randombytes'); randomBytes(16);//get 16 random bytes randomBytes(16, function (err, resp) { // resp is 16 random bytes }); ``` randombytes-2.1.0/browser.js000066400000000000000000000030511343300441500160560ustar00rootroot00000000000000'use strict' // limit of Crypto.getRandomValues() // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues var MAX_BYTES = 65536 // Node supports requesting up to this number of bytes // https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js#L48 var MAX_UINT32 = 4294967295 function oldBrowser () { throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11') } var Buffer = require('safe-buffer').Buffer var crypto = global.crypto || global.msCrypto if (crypto && crypto.getRandomValues) { module.exports = randomBytes } else { module.exports = oldBrowser } function randomBytes (size, cb) { // phantomjs needs to throw if (size > MAX_UINT32) throw new RangeError('requested too many random bytes') var bytes = Buffer.allocUnsafe(size) if (size > 0) { // getRandomValues fails on IE if size == 0 if (size > MAX_BYTES) { // this is the max bytes crypto.getRandomValues // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues for (var generated = 0; generated < size; generated += MAX_BYTES) { // buffer.slice automatically checks if the end is past the end of // the buffer so we don't have to here crypto.getRandomValues(bytes.slice(generated, generated + MAX_BYTES)) } } else { crypto.getRandomValues(bytes) } } if (typeof cb === 'function') { return process.nextTick(function () { cb(null, bytes) }) } return bytes } randombytes-2.1.0/index.js000066400000000000000000000000571343300441500155050ustar00rootroot00000000000000module.exports = require('crypto').randomBytes randombytes-2.1.0/package.json000066400000000000000000000015451343300441500163310ustar00rootroot00000000000000{ "name": "randombytes", "version": "2.1.0", "description": "random bytes from browserify stand alone", "main": "index.js", "scripts": { "test": "standard && node test.js | tspec", "phantom": "zuul --phantom -- test.js", "local": "zuul --local --no-coverage -- test.js" }, "repository": { "type": "git", "url": "git@github.com:crypto-browserify/randombytes.git" }, "keywords": [ "crypto", "random" ], "author": "", "license": "MIT", "bugs": { "url": "https://github.com/crypto-browserify/randombytes/issues" }, "homepage": "https://github.com/crypto-browserify/randombytes", "browser": "browser.js", "devDependencies": { "phantomjs": "^1.9.9", "standard": "^10.0.2", "tap-spec": "^2.1.2", "tape": "^4.6.3", "zuul": "^3.7.2" }, "dependencies": { "safe-buffer": "^5.1.0" } } randombytes-2.1.0/test.js000066400000000000000000000035451343300441500153620ustar00rootroot00000000000000var test = require('tape') var randomBytes = require('./') var MAX_BYTES = 65536 var MAX_UINT32 = 4294967295 test('sync', function (t) { t.plan(9) t.equals(randomBytes(0).length, 0, 'len: ' + 0) t.equals(randomBytes(3).length, 3, 'len: ' + 3) t.equals(randomBytes(30).length, 30, 'len: ' + 30) t.equals(randomBytes(300).length, 300, 'len: ' + 300) t.equals(randomBytes(17 + MAX_BYTES).length, 17 + MAX_BYTES, 'len: ' + 17 + MAX_BYTES) t.equals(randomBytes(MAX_BYTES * 100).length, MAX_BYTES * 100, 'len: ' + MAX_BYTES * 100) t.throws(function () { randomBytes(MAX_UINT32 + 1) }) t.throws(function () { t.equals(randomBytes(-1)) }) t.throws(function () { t.equals(randomBytes('hello')) }) }) test('async', function (t) { t.plan(9) randomBytes(0, function (err, resp) { if (err) throw err t.equals(resp.length, 0, 'len: ' + 0) }) randomBytes(3, function (err, resp) { if (err) throw err t.equals(resp.length, 3, 'len: ' + 3) }) randomBytes(30, function (err, resp) { if (err) throw err t.equals(resp.length, 30, 'len: ' + 30) }) randomBytes(300, function (err, resp) { if (err) throw err t.equals(resp.length, 300, 'len: ' + 300) }) randomBytes(17 + MAX_BYTES, function (err, resp) { if (err) throw err t.equals(resp.length, 17 + MAX_BYTES, 'len: ' + 17 + MAX_BYTES) }) randomBytes(MAX_BYTES * 100, function (err, resp) { if (err) throw err t.equals(resp.length, MAX_BYTES * 100, 'len: ' + MAX_BYTES * 100) }) t.throws(function () { randomBytes(MAX_UINT32 + 1, function () { t.ok(false, 'should not get here') }) }) t.throws(function () { randomBytes(-1, function () { t.ok(false, 'should not get here') }) }) t.throws(function () { randomBytes('hello', function () { t.ok(false, 'should not get here') }) }) })