[Pkg-javascript-commits] [node-randomfill] 35/47: first

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 15 09:52:53 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-randomfill.

commit 9b7eb50869e63cf19d8b7627e56a67303410a230
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Wed Oct 18 08:19:03 2017 -0400

    first
---
 browser.js   | 112 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 index.js     |   8 ++++-
 package.json |   5 +--
 test.js      |  61 +++++---------------------------
 4 files changed, 110 insertions(+), 76 deletions(-)

diff --git a/browser.js b/browser.js
index 9669903..88d2262 100644
--- a/browser.js
+++ b/browser.js
@@ -3,36 +3,106 @@
 function oldBrowser () {
   throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11')
 }
-
-var Buffer = require('safe-buffer').Buffer
+var safeBuffer = require('safe-buffer')
+var randombytes = require('randombytes')
+var Buffer = safeBuffer.Buffer
+var kBufferMaxLength = safeBuffer.kMaxLength
 var crypto = global.crypto || global.msCrypto
+const 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 (crypto && crypto.getRandomValues) {
-  module.exports = randomBytes
-} else {
-  module.exports = oldBrowser
+  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 randomBytes (size, cb) {
-  // phantomjs needs to throw
-  if (size > 65536) throw new Error('requested too many random bytes')
-  // in case browserify  isn't using the Uint8Array version
-  var rawBytes = new global.Uint8Array(size)
+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')
+  }
 
-  // This will not work in older browsers.
-  // See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
-  if (size > 0) {  // getRandomValues fails on IE if size == 0
-    crypto.getRandomValues(rawBytes)
+  if (size + offset > length || size > kBufferMaxLength) {
+    throw new RangeError('buffer too small')
+  }
+}
+if (crypto && crypto.getRandomValues) {
+  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')
   }
 
-  // XXX: phantomjs doesn't like a buffer being passed here
-  var bytes = Buffer.from(rawBytes.buffer)
+  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)
+}
 
-  if (typeof cb === 'function') {
-    return process.nextTick(function () {
-      cb(null, bytes)
+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 bytes
+  return actualFill(buf, offset, size)
 }
diff --git a/index.js b/index.js
index a2d9e39..e2b5f7a 100644
--- a/index.js
+++ b/index.js
@@ -1 +1,7 @@
-module.exports = require('crypto').randomBytes
+var 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')
+}
diff --git a/package.json b/package.json
index 87ef69a..a6ccc07 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
-  "name": "randombytes",
+  "name": "randomfill",
   "version": "2.0.5",
-  "description": "random bytes from browserify stand alone",
+  "description": "random fill from browserify stand alone",
   "main": "index.js",
   "scripts": {
     "test": "standard && node test.js | tspec",
@@ -31,6 +31,7 @@
     "zuul": "^3.7.2"
   },
   "dependencies": {
+    "randombytes": "^2.0.5",
     "safe-buffer": "^5.1.0"
   }
 }
diff --git a/test.js b/test.js
index 8e34dca..17e897d 100644
--- a/test.js
+++ b/test.js
@@ -1,56 +1,13 @@
 var test = require('tape')
-var randomBytes = require('./')
-
+var crypto = require('.')
 test('sync', function (t) {
-  t.plan(4)
-  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)
-})
-
-test('async', function (t) {
-  t.plan(4)
-
-  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)
+  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()
   })
 })
-
-if (process.browser) {
-  test('requesting to much throws', function (t) {
-    t.plan(1)
-    t.throws(function () {
-      randomBytes(65537)
-    })
-  })
-
-  test('requesting to much throws async', function (t) {
-    t.plan(1)
-    t.throws(function () {
-      randomBytes(65537, function () {
-        t.ok(false, 'should not get here')
-      })
-    })
-  })
-}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-randomfill.git



More information about the Pkg-javascript-commits mailing list