PRNG
This commit is contained in:
parent
68f1217911
commit
1866c94de9
|
@ -1,6 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* Browser Bitmessage crypto implementation.
|
* Browser Bitmessage crypto implementation.
|
||||||
*
|
|
||||||
* Documentation: <http://www.w3.org/TR/WebCryptoAPI/>
|
* Documentation: <http://www.w3.org/TR/WebCryptoAPI/>
|
||||||
* Browsers support: <http://caniuse.com/#feat=cryptography>
|
* Browsers support: <http://caniuse.com/#feat=cryptography>
|
||||||
* Blink implementation details: <https://sites.google.com/a/chromium.org/dev/blink/webcrypto>
|
* Blink implementation details: <https://sites.google.com/a/chromium.org/dev/blink/webcrypto>
|
||||||
|
@ -34,3 +33,9 @@ exports.ripemd160 = function(buf) {
|
||||||
// so use module.
|
// so use module.
|
||||||
return Promise.resolve(ripemd160(buf));
|
return Promise.resolve(ripemd160(buf));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.randomBytes = function(size) {
|
||||||
|
var arr = new Uint8Array(size);
|
||||||
|
window.crypto.getRandomValues(arr);
|
||||||
|
return new Buffer(arr);
|
||||||
|
};
|
||||||
|
|
|
@ -25,3 +25,12 @@ exports.ripemd160 = function(buf) {
|
||||||
hash.update(buf);
|
hash.update(buf);
|
||||||
return Promise.resolve(hash.digest());
|
return Promise.resolve(hash.digest());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate cryptographically strong pseudo-random data.
|
||||||
|
* @param {number} size - Number of bytes
|
||||||
|
* @return {Buffer} Buffer with random data.
|
||||||
|
*/
|
||||||
|
exports.randomBytes = function(size) {
|
||||||
|
return crypto.randomBytes(size);
|
||||||
|
};
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var cryptoPlatform = require("./crypto-platform");
|
var cryptoPlatform = require("./crypto-platform");
|
||||||
|
|
||||||
Object.keys(cryptoPlatform).forEach(function(key) {
|
Object.keys(cryptoPlatform).forEach(function(key) {
|
||||||
exports[key] = cryptoPlatform[key];
|
exports[key] = cryptoPlatform[key];
|
||||||
});
|
});
|
||||||
|
|
23
test.js
23
test.js
|
@ -56,6 +56,29 @@ describe("Crypto", function() {
|
||||||
expect(res.toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107");
|
expect(res.toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should implement cryptographically secure PRNG", function() {
|
||||||
|
var size = 100;
|
||||||
|
var rnd = bmcrypto.randomBytes(size);
|
||||||
|
expect(Buffer.isBuffer(rnd)).to.be.true;
|
||||||
|
expect(rnd.length).to.equal(size);
|
||||||
|
// Very simple statistical test.
|
||||||
|
var bytes = {};
|
||||||
|
var sum = 0;
|
||||||
|
var value;
|
||||||
|
for (var i = 0; i < size; i++) {
|
||||||
|
value = rnd[i];
|
||||||
|
sum += value;
|
||||||
|
if (!bytes[value]) {
|
||||||
|
bytes[value] = 0;
|
||||||
|
}
|
||||||
|
bytes[value]++;
|
||||||
|
expect(bytes[value]).to.be.below(5);
|
||||||
|
}
|
||||||
|
// Ideal sum = (255 / 2) * size = 12750
|
||||||
|
expect(sum).to.be.above(10000);
|
||||||
|
expect(sum).to.be.below(15000);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Address", function() {
|
describe("Address", function() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user