Skip to content

Commit

Permalink
replace jsbn's rng with module secure-random
Browse files Browse the repository at this point in the history
This uses window.crypto.getRandomValues on browser
and crypto.randomBytes on node
  • Loading branch information
weilu committed Mar 20, 2014
1 parent ff62596 commit b7861e4
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 141 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -40,6 +40,7 @@
"compile": "./node_modules/.bin/browserify ./src/index.js -s Bitcoin | ./node_modules/.bin/uglifyjs > bitcoinjs-min.js"
},
"dependencies": {
"crypto-js": "3.1.2-2"
"crypto-js": "3.1.2-2",
"secure-random": "^0.2.0"
}
}
3 changes: 1 addition & 2 deletions src/ecdsa.js
@@ -1,12 +1,11 @@
var sec = require('./jsbn/sec');
var SecureRandom = require('./jsbn/rng');
var rng = require('secure-random');
var BigInteger = require('./jsbn/jsbn');
var convert = require('./convert')
var HmacSHA256 = require('crypto-js/hmac-sha256');

var ECPointFp = require('./jsbn/ec').ECPointFp;

var rng = new SecureRandom();
var ecparams = sec("secp256k1");
var P_OVER_FOUR = null;

Expand Down
6 changes: 3 additions & 3 deletions src/jsbn/jsbn.js
Expand Up @@ -672,9 +672,9 @@ function bnpFromNumber(a,b,c) {
}
else {
// new BigInteger(int,RNG)
var x = new Array(), t = a&7;
x.length = (a>>3)+1;
b.nextBytes(x);
var t = a&7;
var length = (a>>3)+1;
var x = b(length, {array: true});
if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
self.fromString(x,256);
}
Expand Down
39 changes: 0 additions & 39 deletions src/jsbn/prng4.js

This file was deleted.

85 changes: 0 additions & 85 deletions src/jsbn/rng.js

This file was deleted.

8 changes: 2 additions & 6 deletions src/wallet.js
Expand Up @@ -7,8 +7,7 @@ var Transaction = require('./transaction').Transaction;
var TransactionIn = require('./transaction').TransactionIn;
var TransactionOut = require('./transaction').TransactionOut;
var HDNode = require('./hdwallet.js')
var SecureRandom = require('./jsbn/rng');
var rng = new SecureRandom();
var rng = require('secure-random');

var Wallet = function (seed, options) {
if (!(this instanceof Wallet)) { return new Wallet(seed, options); }
Expand All @@ -32,10 +31,7 @@ var Wallet = function (seed, options) {

// Make a new master key
this.newMasterKey = function(seed, network) {
if (!seed) {
var seed= new Array(32);
rng.nextBytes(seed);
}
if (!seed) seed= rng(32, { array: true })
masterkey = new HDNode(seed, network);

// HD first-level child derivation method should be private
Expand Down
9 changes: 9 additions & 0 deletions test/jsbn.js
Expand Up @@ -2,6 +2,7 @@
var assert = require('assert');
var BigInteger = require('../src/jsbn/jsbn.js')
var bytesToHex = require('../src/convert.js').bytesToHex;
var secureRandom = require('secure-random');

describe('BigInteger', function() {
describe('toByteArraySigned', function() {
Expand All @@ -25,4 +26,12 @@ describe('BigInteger', function() {
assert.equal(hex(-62300), '0x80f35c');
})
})

describe('with RNG passed into constructor as the 2nd argument', function(){
it('returns a BigInteger with the limit of the specified length', function(){
var bitLength = 256
var i = new BigInteger(bitLength, secureRandom)
assert(i.bitLength() <= 256)
})
})
})
6 changes: 1 addition & 5 deletions test/misc.js
@@ -1,13 +1,9 @@
/* global it */
var assert = require('assert');
var bitcoinjs = require('../');
var sec = require('../src/jsbn/sec');
var BigInteger = require('../src/jsbn/jsbn.js');
var SHA256 = require('crypto-js/sha256');

var SecureRandom = require('../src/jsbn/rng');
var rng = new SecureRandom();

var rng = require('secure-random');
var ecparams = sec('secp256k1');
var ECPointFp = bitcoinjs.ECPointFp;
var convert = require('../src/convert');
Expand Down

0 comments on commit b7861e4

Please sign in to comment.