random: require random from crypto.
This commit is contained in:
parent
530fbd61dd
commit
9dd863d9f2
@ -621,4 +621,14 @@ crypto.randomBytes = random.randomBytes;
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
crypto.randomRange = random.randomRange;
|
||||
|
||||
/**
|
||||
* Generate a random uint32.
|
||||
* Probably more cryptographically sound than
|
||||
* `Math.random()`.
|
||||
* @function
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
crypto.randomInt = random.randomInt;
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
/* jshint worker: true */
|
||||
|
||||
var random, crypto, global;
|
||||
var randomBytes, crypto, global;
|
||||
|
||||
try {
|
||||
crypto = require('crypto');
|
||||
@ -19,7 +19,7 @@ try {
|
||||
}
|
||||
|
||||
if (crypto) {
|
||||
random = function random(n) {
|
||||
randomBytes = function randomBytes(n) {
|
||||
return crypto.randomBytes(n);
|
||||
};
|
||||
} else {
|
||||
@ -34,7 +34,7 @@ if (crypto) {
|
||||
crypto = global.crypto || global.msCrypto;
|
||||
|
||||
if (crypto && crypto.getRandomValues) {
|
||||
random = function random(n) {
|
||||
randomBytes = function randomBytes(n) {
|
||||
var data = new Uint8Array(n);
|
||||
crypto.getRandomValues(data);
|
||||
return new Buffer(data.buffer);
|
||||
@ -43,7 +43,7 @@ if (crypto) {
|
||||
// Out of luck here. Use bad randomness for now.
|
||||
// Possibly fall back to randy in the future:
|
||||
// https://github.com/deestan/randy
|
||||
random = function random(n) {
|
||||
randomBytes = function randomBytes(n) {
|
||||
var data = new Buffer(n);
|
||||
var i;
|
||||
|
||||
@ -55,8 +55,12 @@ if (crypto) {
|
||||
}
|
||||
}
|
||||
|
||||
function randomInt(min, max) {
|
||||
var num = random(4).readUInt32LE(0, true);
|
||||
function randomInt() {
|
||||
return randomBytes(4).readUInt32LE(0, true);
|
||||
}
|
||||
|
||||
function randomRange(min, max) {
|
||||
var num = randomInt();
|
||||
return Math.floor((num / 0x100000000) * (max - min) + min);
|
||||
}
|
||||
|
||||
@ -64,8 +68,9 @@ function randomInt(min, max) {
|
||||
* Expose
|
||||
*/
|
||||
|
||||
exports = random;
|
||||
exports.randomBytes = random;
|
||||
exports = randomBytes;
|
||||
exports.randomBytes = randomBytes;
|
||||
exports.randomInt = randomInt;
|
||||
exports.randomRange = randomRange;
|
||||
|
||||
module.exports = random;
|
||||
module.exports = randomBytes;
|
||||
|
||||
@ -10,7 +10,7 @@ var bn = require('bn.js');
|
||||
var elliptic = require('elliptic');
|
||||
var Signature = require('elliptic/lib/elliptic/ec/signature');
|
||||
var hmacDRBG = require('elliptic/lib/elliptic/hmac-drbg');
|
||||
var random = require('./random');
|
||||
var crypto = require('./crypto');
|
||||
var curve = elliptic.ec('secp256k1').curve;
|
||||
var sha256 = require('./crypto').sha256;
|
||||
|
||||
@ -109,7 +109,7 @@ schnorr.sign = function sign(msg, key, hash, pubnonce) {
|
||||
throw new Error('Bad private key.');
|
||||
|
||||
while (!sig) {
|
||||
k = new bn(random.randomBytes(32));
|
||||
k = new bn(crypto.randomBytes(32));
|
||||
sig = schnorr._sign(msg, prv, k, hash, pubnonce);
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
var bcoin = require('../env');
|
||||
var utils = require('../utils/utils');
|
||||
var crypto = require('../crypto/crypto');
|
||||
var random = require('../crypto/random');
|
||||
var assert = utils.assert;
|
||||
var constants = bcoin.constants;
|
||||
var BufferWriter = require('../utils/writer');
|
||||
@ -169,7 +168,7 @@ Mnemonic.prototype.toKey = function toKey(passphrase, network) {
|
||||
|
||||
Mnemonic.prototype.getEntropy = function getEntropy() {
|
||||
if (!this.entropy)
|
||||
this.entropy = random.randomBytes(this.bits / 8);
|
||||
this.entropy = crypto.randomBytes(this.bits / 8);
|
||||
|
||||
assert(this.bits / 8 === this.entropy.length);
|
||||
|
||||
|
||||
@ -10,7 +10,6 @@ var bcoin = require('../env');
|
||||
var utils = require('../utils/utils');
|
||||
var crypto = require('../crypto/crypto');
|
||||
var ec = require('../crypto/ec');
|
||||
var random = require('../crypto/random');
|
||||
var assert = utils.assert;
|
||||
var constants = bcoin.constants;
|
||||
var networks = bcoin.networks;
|
||||
@ -580,7 +579,7 @@ HDPrivateKey.fromKey = function fromKey(key, entropy, network) {
|
||||
|
||||
HDPrivateKey.generate = function generate(network) {
|
||||
var key = ec.generatePrivateKey();
|
||||
var entropy = random.randomBytes(32);
|
||||
var entropy = crypto.randomBytes(32);
|
||||
return HDPrivateKey.fromKey(key, entropy, network);
|
||||
};
|
||||
|
||||
|
||||
@ -16,7 +16,6 @@ var http = require('./');
|
||||
var HTTPBase = http.base;
|
||||
var utils = require('../utils/utils');
|
||||
var crypto = require('../crypto/crypto');
|
||||
var random = require('../crypto/random');
|
||||
var assert = utils.assert;
|
||||
var RPC; /*= require('./rpc'); - load lazily */
|
||||
|
||||
@ -59,7 +58,7 @@ function HTTPServer(options) {
|
||||
this.rpc = null;
|
||||
|
||||
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(this.apiKey.length <= 200, 'API key must be under 200 bytes.');
|
||||
|
||||
@ -14,7 +14,7 @@ var utils = require('../utils/utils');
|
||||
var assert = utils.assert;
|
||||
var BufferWriter = require('../utils/writer');
|
||||
var BufferReader = require('../utils/reader');
|
||||
var random = require('../crypto/random');
|
||||
var crypto = require('../crypto/crypto');
|
||||
var VerifyError = bcoin.errors.VerifyError;
|
||||
var VerifyResult = utils.VerifyResult;
|
||||
|
||||
@ -296,7 +296,7 @@ Mempool.prototype.limitOrphans = function limitOrphans() {
|
||||
var i, hash;
|
||||
|
||||
while (this.totalOrphans > constants.mempool.MAX_ORPHAN_TX) {
|
||||
i = random.randomInt(0, orphans.length);
|
||||
i = crypto.randomRange(0, orphans.length);
|
||||
hash = orphans[i];
|
||||
orphans.splice(i, 1);
|
||||
|
||||
|
||||
@ -12,7 +12,6 @@ var EventEmitter = require('events').EventEmitter;
|
||||
var bcoin = require('../env');
|
||||
var utils = require('../utils/utils');
|
||||
var crypto = require('../crypto/crypto');
|
||||
var random = require('../crypto/random');
|
||||
var assert = utils.assert;
|
||||
var constants = bcoin.constants;
|
||||
|
||||
@ -120,7 +119,7 @@ BIP150.prototype.reply = function reply(payload) {
|
||||
throw new Error('Auth failure.');
|
||||
|
||||
if (!this.peerIdentity)
|
||||
return random.randomBytes(32);
|
||||
return crypto.randomBytes(32);
|
||||
|
||||
sig = bcoin.ec.toDER(data);
|
||||
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);
|
||||
|
||||
if (!result)
|
||||
return random.randomBytes(32);
|
||||
return crypto.randomBytes(32);
|
||||
|
||||
if (this.isAuthed()) {
|
||||
this.auth = true;
|
||||
|
||||
@ -15,7 +15,6 @@ var crypto = require('../crypto/crypto');
|
||||
var assert = utils.assert;
|
||||
var BufferReader = require('../utils/reader');
|
||||
var BufferWriter = require('../utils/writer');
|
||||
var random = require('../crypto/random');
|
||||
var TXDB = require('./txdb');
|
||||
var Path = require('./path');
|
||||
|
||||
@ -2381,7 +2380,7 @@ MasterKey.prototype.encrypt = function encrypt(passphrase, callback) {
|
||||
return callback();
|
||||
|
||||
data = this.key.toExtended();
|
||||
iv = random.randomBytes(16);
|
||||
iv = crypto.randomBytes(16);
|
||||
|
||||
this.stop();
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user