Skip to content

Commit 8800190

Browse files
authored
Merge pull request #173 from crypto-browserify/randomfill
add rndom fill
2 parents acd8789 + 01d5b5c commit 8800190

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ matrix:
1717
- node_js: '4'
1818
env: TEST_SUITE=browser BROWSER_NAME=firefox BROWSER_VERSION="-2..latest"
1919
- node_js: '4'
20-
env: TEST_SUITE=browser BROWSER_NAME=safari BROWSER_VERSION="5..latest"
20+
env: TEST_SUITE=browser BROWSER_NAME=safari BROWSER_VERSION="7..latest"
2121
- node_js: '4'
2222
env: TEST_SUITE=browser BROWSER_NAME=android BROWSER_VERSION="5.0..latest"
2323
script: "npm run-script $TEST_SUITE"

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ exports.privateDecrypt = publicEncrypt.privateDecrypt
6565
// }
6666
// })
6767

68+
var rf = require('randomfill')
69+
70+
exports.randomFill = rf.randomFill
71+
exports.randomFillSync = rf.randomFillSync
72+
6873
exports.createCredentials = function () {
6974
throw new Error([
7075
'sorry, createCredentials is not implemented yet',

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
"inherits": "^2.0.1",
2828
"pbkdf2": "^3.0.3",
2929
"public-encrypt": "^4.0.0",
30-
"randombytes": "^2.0.0"
30+
"randombytes": "^2.0.0",
31+
"randomfill": "^1.0.3"
3132
},
3233
"devDependencies": {
3334
"hash-test-vectors": "~1.3.2",
3435
"pseudorandombytes": "^2.0.0",
36+
"safe-buffer": "^5.1.1",
3537
"standard": "^5.0.2",
3638
"tape": "~2.3.2",
3739
"zuul": "^3.6.0"

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ try {
1212
require('./public-encrypt')
1313
require('./random-bytes')
1414
require('./sign')
15+
require('./random-fill')
1516
} catch (e) {
1617
console.log('no secure rng avaiable')
1718
}

test/random-fill.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var test = require('tape')
2+
var crypto = require('../')
3+
var Buffer = require('safe-buffer').Buffer
4+
5+
test('get error message', function (t) {
6+
try {
7+
var b = crypto.randomFillSync(Buffer.alloc(10))
8+
t.ok(Buffer.isBuffer(b))
9+
t.end()
10+
} catch (err) {
11+
t.ok(/not supported/.test(err.message), '"not supported" is in error message')
12+
t.end()
13+
}
14+
})
15+
16+
test('randomfill', function (t) {
17+
t.plan(5)
18+
t.equal(crypto.randomFillSync(Buffer.alloc(10)).length, 10)
19+
t.ok(Buffer.isBuffer(crypto.randomFillSync(Buffer.alloc(10))))
20+
crypto.randomFill(Buffer.alloc(10), function (ex, bytes) {
21+
t.error(ex)
22+
t.equal(bytes.length, 10)
23+
t.ok(Buffer.isBuffer(bytes))
24+
t.end()
25+
})
26+
})
27+
28+
test('seems random', function (t) {
29+
var L = 1000
30+
var b = crypto.randomFillSync(Buffer.alloc(L))
31+
32+
var mean = [].reduce.call(b, function (a, b) {
33+
return a + b
34+
}, 0) / L
35+
36+
// test that the random numbers are plausably random.
37+
// Math.random() will pass this, but this will catch
38+
// terrible mistakes such as this blunder:
39+
// https://github.com/dominictarr/crypto-browserify/commit/3267955e1df7edd1680e52aeede9a89506ed2464#commitcomment-7916835
40+
41+
// this doesn't check that the bytes are in a random *order*
42+
// but it's better than nothing.
43+
44+
var expected = 256 / 2
45+
var smean = Math.sqrt(mean)
46+
47+
// console.log doesn't work right on testling, *grumble grumble*
48+
console.log(JSON.stringify([expected - smean, mean, expected + smean]))
49+
t.ok(mean < expected + smean)
50+
t.ok(mean > expected - smean)
51+
52+
t.end()
53+
})

0 commit comments

Comments
 (0)