random: require random from crypto.

This commit is contained in:
Christopher Jeffrey 2016-09-15 13:36:18 -07:00
parent 530fbd61dd
commit 9dd863d9f2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
9 changed files with 34 additions and 24 deletions

View File

@ -621,4 +621,14 @@ crypto.randomBytes = random.randomBytes;
* @returns {Number} * @returns {Number}
*/ */
crypto.randomRange = random.randomRange;
/**
* Generate a random uint32.
* Probably more cryptographically sound than
* `Math.random()`.
* @function
* @returns {Number}
*/
crypto.randomInt = random.randomInt; crypto.randomInt = random.randomInt;

View File

@ -10,7 +10,7 @@
/* jshint worker: true */ /* jshint worker: true */
var random, crypto, global; var randomBytes, crypto, global;
try { try {
crypto = require('crypto'); crypto = require('crypto');
@ -19,7 +19,7 @@ try {
} }
if (crypto) { if (crypto) {
random = function random(n) { randomBytes = function randomBytes(n) {
return crypto.randomBytes(n); return crypto.randomBytes(n);
}; };
} else { } else {
@ -34,7 +34,7 @@ if (crypto) {
crypto = global.crypto || global.msCrypto; crypto = global.crypto || global.msCrypto;
if (crypto && crypto.getRandomValues) { if (crypto && crypto.getRandomValues) {
random = function random(n) { randomBytes = function randomBytes(n) {
var data = new Uint8Array(n); var data = new Uint8Array(n);
crypto.getRandomValues(data); crypto.getRandomValues(data);
return new Buffer(data.buffer); return new Buffer(data.buffer);
@ -43,7 +43,7 @@ if (crypto) {
// Out of luck here. Use bad randomness for now. // Out of luck here. Use bad randomness for now.
// Possibly fall back to randy in the future: // Possibly fall back to randy in the future:
// https://github.com/deestan/randy // https://github.com/deestan/randy
random = function random(n) { randomBytes = function randomBytes(n) {
var data = new Buffer(n); var data = new Buffer(n);
var i; var i;
@ -55,8 +55,12 @@ if (crypto) {
} }
} }
function randomInt(min, max) { function randomInt() {
var num = random(4).readUInt32LE(0, true); return randomBytes(4).readUInt32LE(0, true);
}
function randomRange(min, max) {
var num = randomInt();
return Math.floor((num / 0x100000000) * (max - min) + min); return Math.floor((num / 0x100000000) * (max - min) + min);
} }
@ -64,8 +68,9 @@ function randomInt(min, max) {
* Expose * Expose
*/ */
exports = random; exports = randomBytes;
exports.randomBytes = random; exports.randomBytes = randomBytes;
exports.randomInt = randomInt; exports.randomInt = randomInt;
exports.randomRange = randomRange;
module.exports = random; module.exports = randomBytes;

View File

@ -10,7 +10,7 @@ var bn = require('bn.js');
var elliptic = require('elliptic'); var elliptic = require('elliptic');
var Signature = require('elliptic/lib/elliptic/ec/signature'); var Signature = require('elliptic/lib/elliptic/ec/signature');
var hmacDRBG = require('elliptic/lib/elliptic/hmac-drbg'); var hmacDRBG = require('elliptic/lib/elliptic/hmac-drbg');
var random = require('./random'); var crypto = require('./crypto');
var curve = elliptic.ec('secp256k1').curve; var curve = elliptic.ec('secp256k1').curve;
var sha256 = require('./crypto').sha256; var sha256 = require('./crypto').sha256;
@ -109,7 +109,7 @@ schnorr.sign = function sign(msg, key, hash, pubnonce) {
throw new Error('Bad private key.'); throw new Error('Bad private key.');
while (!sig) { while (!sig) {
k = new bn(random.randomBytes(32)); k = new bn(crypto.randomBytes(32));
sig = schnorr._sign(msg, prv, k, hash, pubnonce); sig = schnorr._sign(msg, prv, k, hash, pubnonce);
} }

View File

@ -9,7 +9,6 @@
var bcoin = require('../env'); var bcoin = require('../env');
var utils = require('../utils/utils'); var utils = require('../utils/utils');
var crypto = require('../crypto/crypto'); var crypto = require('../crypto/crypto');
var random = require('../crypto/random');
var assert = utils.assert; var assert = utils.assert;
var constants = bcoin.constants; var constants = bcoin.constants;
var BufferWriter = require('../utils/writer'); var BufferWriter = require('../utils/writer');
@ -169,7 +168,7 @@ Mnemonic.prototype.toKey = function toKey(passphrase, network) {
Mnemonic.prototype.getEntropy = function getEntropy() { Mnemonic.prototype.getEntropy = function getEntropy() {
if (!this.entropy) if (!this.entropy)
this.entropy = random.randomBytes(this.bits / 8); this.entropy = crypto.randomBytes(this.bits / 8);
assert(this.bits / 8 === this.entropy.length); assert(this.bits / 8 === this.entropy.length);

View File

@ -10,7 +10,6 @@ var bcoin = require('../env');
var utils = require('../utils/utils'); var utils = require('../utils/utils');
var crypto = require('../crypto/crypto'); var crypto = require('../crypto/crypto');
var ec = require('../crypto/ec'); var ec = require('../crypto/ec');
var random = require('../crypto/random');
var assert = utils.assert; var assert = utils.assert;
var constants = bcoin.constants; var constants = bcoin.constants;
var networks = bcoin.networks; var networks = bcoin.networks;
@ -580,7 +579,7 @@ HDPrivateKey.fromKey = function fromKey(key, entropy, network) {
HDPrivateKey.generate = function generate(network) { HDPrivateKey.generate = function generate(network) {
var key = ec.generatePrivateKey(); var key = ec.generatePrivateKey();
var entropy = random.randomBytes(32); var entropy = crypto.randomBytes(32);
return HDPrivateKey.fromKey(key, entropy, network); return HDPrivateKey.fromKey(key, entropy, network);
}; };

View File

@ -16,7 +16,6 @@ var http = require('./');
var HTTPBase = http.base; var HTTPBase = http.base;
var utils = require('../utils/utils'); var utils = require('../utils/utils');
var crypto = require('../crypto/crypto'); var crypto = require('../crypto/crypto');
var random = require('../crypto/random');
var assert = utils.assert; var assert = utils.assert;
var RPC; /*= require('./rpc'); - load lazily */ var RPC; /*= require('./rpc'); - load lazily */
@ -59,7 +58,7 @@ function HTTPServer(options) {
this.rpc = null; this.rpc = null;
if (!this.apiKey) if (!this.apiKey)
this.apiKey = utils.toBase58(random.randomBytes(20)); this.apiKey = utils.toBase58(crypto.randomBytes(20));
assert(typeof this.apiKey === 'string', 'API key must be a string.'); assert(typeof this.apiKey === 'string', 'API key must be a string.');
assert(this.apiKey.length <= 200, 'API key must be under 200 bytes.'); assert(this.apiKey.length <= 200, 'API key must be under 200 bytes.');

View File

@ -14,7 +14,7 @@ var utils = require('../utils/utils');
var assert = utils.assert; var assert = utils.assert;
var BufferWriter = require('../utils/writer'); var BufferWriter = require('../utils/writer');
var BufferReader = require('../utils/reader'); var BufferReader = require('../utils/reader');
var random = require('../crypto/random'); var crypto = require('../crypto/crypto');
var VerifyError = bcoin.errors.VerifyError; var VerifyError = bcoin.errors.VerifyError;
var VerifyResult = utils.VerifyResult; var VerifyResult = utils.VerifyResult;
@ -296,7 +296,7 @@ Mempool.prototype.limitOrphans = function limitOrphans() {
var i, hash; var i, hash;
while (this.totalOrphans > constants.mempool.MAX_ORPHAN_TX) { while (this.totalOrphans > constants.mempool.MAX_ORPHAN_TX) {
i = random.randomInt(0, orphans.length); i = crypto.randomRange(0, orphans.length);
hash = orphans[i]; hash = orphans[i];
orphans.splice(i, 1); orphans.splice(i, 1);

View File

@ -12,7 +12,6 @@ var EventEmitter = require('events').EventEmitter;
var bcoin = require('../env'); var bcoin = require('../env');
var utils = require('../utils/utils'); var utils = require('../utils/utils');
var crypto = require('../crypto/crypto'); var crypto = require('../crypto/crypto');
var random = require('../crypto/random');
var assert = utils.assert; var assert = utils.assert;
var constants = bcoin.constants; var constants = bcoin.constants;
@ -120,7 +119,7 @@ BIP150.prototype.reply = function reply(payload) {
throw new Error('Auth failure.'); throw new Error('Auth failure.');
if (!this.peerIdentity) if (!this.peerIdentity)
return random.randomBytes(32); return crypto.randomBytes(32);
sig = bcoin.ec.toDER(data); sig = bcoin.ec.toDER(data);
msg = this.hash(this.output.sid, type, this.peerIdentity); msg = this.hash(this.output.sid, type, this.peerIdentity);
@ -128,7 +127,7 @@ BIP150.prototype.reply = function reply(payload) {
result = bcoin.ec.verify(msg, sig, this.peerIdentity); result = bcoin.ec.verify(msg, sig, this.peerIdentity);
if (!result) if (!result)
return random.randomBytes(32); return crypto.randomBytes(32);
if (this.isAuthed()) { if (this.isAuthed()) {
this.auth = true; this.auth = true;

View File

@ -15,7 +15,6 @@ var crypto = require('../crypto/crypto');
var assert = utils.assert; var assert = utils.assert;
var BufferReader = require('../utils/reader'); var BufferReader = require('../utils/reader');
var BufferWriter = require('../utils/writer'); var BufferWriter = require('../utils/writer');
var random = require('../crypto/random');
var TXDB = require('./txdb'); var TXDB = require('./txdb');
var Path = require('./path'); var Path = require('./path');
@ -2381,7 +2380,7 @@ MasterKey.prototype.encrypt = function encrypt(passphrase, callback) {
return callback(); return callback();
data = this.key.toExtended(); data = this.key.toExtended();
iv = random.randomBytes(16); iv = crypto.randomBytes(16);
this.stop(); this.stop();