This commit is contained in:
Kagami Hiiragi 2014-12-26 20:17:01 +03:00
parent 68f1217911
commit 1866c94de9
4 changed files with 39 additions and 1 deletions

View File

@ -1,6 +1,5 @@
/**
* Browser Bitmessage crypto implementation.
*
* Documentation: <http://www.w3.org/TR/WebCryptoAPI/>
* Browsers support: <http://caniuse.com/#feat=cryptography>
* Blink implementation details: <https://sites.google.com/a/chromium.org/dev/blink/webcrypto>
@ -34,3 +33,9 @@ exports.ripemd160 = function(buf) {
// so use module.
return Promise.resolve(ripemd160(buf));
};
exports.randomBytes = function(size) {
var arr = new Uint8Array(size);
window.crypto.getRandomValues(arr);
return new Buffer(arr);
};

View File

@ -25,3 +25,12 @@ exports.ripemd160 = function(buf) {
hash.update(buf);
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);
};

View File

@ -7,6 +7,7 @@
"use strict";
var cryptoPlatform = require("./crypto-platform");
Object.keys(cryptoPlatform).forEach(function(key) {
exports[key] = cryptoPlatform[key];
});

23
test.js
View File

@ -56,6 +56,29 @@ describe("Crypto", function() {
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() {