pax_global_header00006660000000000000000000000064132415532400014511gustar00rootroot0000000000000052 comment=f0c99be53fbb798afc0879cf8dcb13db3a7266f2 randomfill-1.0.4/000077500000000000000000000000001324155324000136425ustar00rootroot00000000000000randomfill-1.0.4/.gitignore000066400000000000000000000000151324155324000156260ustar00rootroot00000000000000node_modules randomfill-1.0.4/.travis.yml000066400000000000000000000004771324155324000157630ustar00rootroot00000000000000sudo: 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" randomfill-1.0.4/.zuul.yml000066400000000000000000000000111324155324000154320ustar00rootroot00000000000000ui: tape randomfill-1.0.4/LICENSE000066400000000000000000000020621324155324000146470ustar00rootroot00000000000000MIT 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. randomfill-1.0.4/README.md000066400000000000000000000007131324155324000151220ustar00rootroot00000000000000randomfill === [![Version](http://img.shields.io/npm/v/randomfill.svg)](https://www.npmjs.org/package/randomfill) randomfill 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 randomFill = require('randomfill'); var buf randomFill.randomFillSync(16);//get 16 random bytes randomFill.randomFill(16, function (err, resp) { // resp is 16 random bytes }); ``` randomfill-1.0.4/browser.js000066400000000000000000000056671324155324000157010ustar00rootroot00000000000000'use strict' function oldBrowser () { throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11') } var safeBuffer = require('safe-buffer') var randombytes = require('randombytes') var Buffer = safeBuffer.Buffer var kBufferMaxLength = safeBuffer.kMaxLength var crypto = global.crypto || global.msCrypto var kMaxUint32 = Math.pow(2, 32) - 1 function assertOffset (offset, length) { if (typeof offset !== 'number' || offset !== offset) { // eslint-disable-line no-self-compare throw new TypeError('offset must be a number') } if (offset > kMaxUint32 || offset < 0) { throw new TypeError('offset must be a uint32') } if (offset > kBufferMaxLength || offset > length) { throw new RangeError('offset out of range') } } function assertSize (size, offset, length) { if (typeof size !== 'number' || size !== size) { // eslint-disable-line no-self-compare throw new TypeError('size must be a number') } if (size > kMaxUint32 || size < 0) { throw new TypeError('size must be a uint32') } if (size + offset > length || size > kBufferMaxLength) { throw new RangeError('buffer too small') } } if ((crypto && crypto.getRandomValues) || !process.browser) { exports.randomFill = randomFill exports.randomFillSync = randomFillSync } else { exports.randomFill = oldBrowser exports.randomFillSync = oldBrowser } function randomFill (buf, offset, size, cb) { if (!Buffer.isBuffer(buf) && !(buf instanceof global.Uint8Array)) { throw new TypeError('"buf" argument must be a Buffer or Uint8Array') } if (typeof offset === 'function') { cb = offset offset = 0 size = buf.length } else if (typeof size === 'function') { cb = size size = buf.length - offset } else if (typeof cb !== 'function') { throw new TypeError('"cb" argument must be a function') } assertOffset(offset, buf.length) assertSize(size, offset, buf.length) return actualFill(buf, offset, size, cb) } function actualFill (buf, offset, size, cb) { if (process.browser) { var ourBuf = buf.buffer var uint = new Uint8Array(ourBuf, offset, size) crypto.getRandomValues(uint) if (cb) { process.nextTick(function () { cb(null, buf) }) return } return buf } if (cb) { randombytes(size, function (err, bytes) { if (err) { return cb(err) } bytes.copy(buf, offset) cb(null, buf) }) return } var bytes = randombytes(size) bytes.copy(buf, offset) return buf } function randomFillSync (buf, offset, size) { if (typeof offset === 'undefined') { offset = 0 } if (!Buffer.isBuffer(buf) && !(buf instanceof global.Uint8Array)) { throw new TypeError('"buf" argument must be a Buffer or Uint8Array') } assertOffset(offset, buf.length) if (size === undefined) size = buf.length - offset assertSize(size, offset, buf.length) return actualFill(buf, offset, size) } randomfill-1.0.4/index.js000066400000000000000000000004121324155324000153040ustar00rootroot00000000000000var crypto = require('crypto') if (typeof crypto.randomFill === 'function' && typeof crypto.randomFillSync === 'function') { exports.randomFill = crypto.randomFill exports.randomFillSync = crypto.randomFillSync } else { module.exports = require('./browser') } randomfill-1.0.4/package.json000066400000000000000000000016011324155324000161260ustar00rootroot00000000000000{ "name": "randomfill", "version": "1.0.4", "description": "random fill 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": "https://github.com/crypto-browserify/randomfill.git" }, "keywords": [ "crypto", "random" ], "author": "", "license": "MIT", "bugs": { "url": "https://github.com/crypto-browserify/randomfill/issues" }, "homepage": "https://github.com/crypto-browserify/randomfill", "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": { "randombytes": "^2.0.5", "safe-buffer": "^5.1.0" } } randomfill-1.0.4/test.js000066400000000000000000000014651324155324000151650ustar00rootroot00000000000000var test = require('tape') var crypto = require('./browser') var Buffer = require('safe-buffer').Buffer test('sync', function (t) { t.test('first', function (t) { const buf = Buffer.alloc(10) const before = buf.toString('hex') crypto.randomFillSync(buf, 5, 5) const after = buf.toString('hex') t.notEqual(before, after) t.equal(before.slice(0, 10), after.slice(0, 10)) t.end() }) }) test('async', function (t) { t.test('first', function (t) { const buf = Buffer.alloc(10) const before = buf.toString('hex') crypto.randomFill(buf, 5, 5, function (err, bufa) { t.error(err) const after = bufa.toString('hex') t.notEqual(before, after) t.equal(before.slice(0, 10), after.slice(0, 10)) t.ok(buf === bufa, 'same buffer') t.end() }) }) })