Skip to content

Commit 4640573

Browse files
committed
Merge branch 'add-pseudoRandomBytes' of https://github.com/plediii/crypto-browserify
2 parents dc26d5f + 969a5e0 commit 4640573

File tree

3 files changed

+95
-53
lines changed

3 files changed

+95
-53
lines changed

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
var rng = require('./rng')
2+
var rng = exports.rng = require('./rng')
3+
var prng = exports.prng = require('./prng');
34

45
function error () {
56
var m = [].slice.call(arguments).join(' ')
@@ -17,10 +18,19 @@ exports.createHmac = require('./create-hmac')
1718
exports.randomBytes = function(size, callback) {
1819
if (callback && callback.call) {
1920
try {
20-
callback.call(this, undefined, new Buffer(rng(size)))
21+
callback.call(this, undefined, rng(size))
2122
} catch (err) { callback(err) }
2223
} else {
23-
return new Buffer(rng(size))
24+
return rng(size)
25+
}
26+
}
27+
exports.pseudoRandomBytes = function(size, callback) {
28+
if (callback && callback.call) {
29+
try {
30+
callback.call(this, undefined, prng(size))
31+
} catch (err) { callback(err) }
32+
} else {
33+
return prng(size)
2434
}
2535
}
2636

prng.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
(function() {
3+
var g = ('undefined' === typeof window ? global : window) || {}
4+
var _crypto = (
5+
g.crypto || g.msCrypto || require('crypto')
6+
)
7+
module.exports = function(size) {
8+
// Modern Browsers
9+
if(_crypto.getRandomValues) {
10+
var bytes = new Buffer(size); //in browserify, this is an extended Uint8Array
11+
/* This will not work in older browsers.
12+
* See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
13+
*/
14+
15+
_crypto.getRandomValues(bytes);
16+
return bytes;
17+
}
18+
else if (_crypto.pseudoRandomBytes) {
19+
return _crypto.pseudoRandomBytes(size)
20+
}
21+
else
22+
throw new Error(
23+
'pseudo random number generation not yet implemented for this browser\n'+
24+
'use chrome, FireFox or Internet Explorer 11'
25+
)
26+
}
27+
}())

test/random-bytes.js

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
var test = require('tape')
22
var crypto = require('../')
33

4-
test('get error message', function (t) {
5-
6-
try {
7-
var b = crypto.randomBytes(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-
17-
test('randomBytes', function (t) {
18-
t.plan(5);
19-
t.equal(crypto.randomBytes(10).length, 10);
20-
t.ok(Buffer.isBuffer(crypto.randomBytes(10)))
21-
crypto.randomBytes(10, function(ex, bytes) {
22-
t.error(ex);
23-
t.equal(bytes.length, 10);
24-
t.ok(Buffer.isBuffer(bytes))
25-
t.end();
4+
var randomBytesFunctions = ['randomBytes', 'pseudoRandomBytes'];
5+
for (var idx in randomBytesFunctions) {
6+
// Both randomBytes and pseudoRandomBytes should provide the same interface
7+
var randomBytesName = randomBytesFunctions[idx];
8+
var randomBytes = crypto[randomBytesName]
9+
test('get error message', function (t) {
10+
11+
try {
12+
var b = randomBytes(10)
13+
t.ok(Buffer.isBuffer(b))
14+
t.end()
15+
} catch (err) {
16+
t.ok(/not supported/.test(err.message), '"not supported" is in error message')
17+
t.end()
18+
}
19+
20+
})
21+
22+
test(randomBytesName, function (t) {
23+
t.plan(5);
24+
t.equal(randomBytes(10).length, 10);
25+
t.ok(Buffer.isBuffer(randomBytes(10)))
26+
randomBytes(10, function(ex, bytes) {
27+
t.error(ex);
28+
t.equal(bytes.length, 10);
29+
t.ok(Buffer.isBuffer(bytes))
30+
t.end();
31+
});
2632
});
27-
});
28-
29-
test('randomBytes seem random', function (t) {
30-
31-
var L = 1000
32-
var b = crypto.randomBytes(L)
33-
34-
var mean = [].reduce.call(b, function (a, b) { return a + b}, 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-
//console.log doesn't work right on testling, *grumble grumble*
47-
console.log(JSON.stringify([expected - smean, mean, expected + smean]))
48-
t.ok(mean < expected + smean)
49-
t.ok(mean > expected - smean)
50-
51-
t.end()
52-
53-
})
54-
33+
34+
test(randomBytesName + ' seem random', function (t) {
35+
36+
var L = 1000
37+
var b = randomBytes(L)
38+
39+
var mean = [].reduce.call(b, function (a, b) { return a + b}, 0) / L
40+
41+
// test that the random numbers are plausably random.
42+
// Math.random() will pass this, but this will catch
43+
// terrible mistakes such as this blunder:
44+
// https://github.com/dominictarr/crypto-browserify/commit/3267955e1df7edd1680e52aeede9a89506ed2464#commitcomment-7916835
45+
46+
// this doesn't check that the bytes are in a random *order*
47+
// but it's better than nothing.
48+
49+
var expected = 256/2
50+
var smean = Math.sqrt(mean)
51+
//console.log doesn't work right on testling, *grumble grumble*
52+
console.log(JSON.stringify([expected - smean, mean, expected + smean]))
53+
t.ok(mean < expected + smean)
54+
t.ok(mean > expected - smean)
55+
56+
t.end()
57+
58+
})
59+
}
5560

0 commit comments

Comments
 (0)