From 1fc0b204ac175e8b7b802545d8f37b6bd0e92641 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 17 Mar 2015 15:20:18 +1100 Subject: [PATCH 1/5] bip39: use randombytes package --- index.js | 3 ++- package.json | 1 + test/index.js | 12 ++++-------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 1600cab..e457624 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var assert = require('assert') var crypto = require('crypto') var pbkdf2 = require('pbkdf2-compat').pbkdf2Sync +var randomBytes = require('randombytes') var unorm = require('unorm') var DEFAULT_WORDLIST = require('./wordlists/en.json') @@ -69,7 +70,7 @@ function entropyToMnemonic(entropy, wordlist) { function generateMnemonic(strength, rng, wordlist) { strength = strength || 128 - rng = rng || crypto.randomBytes + rng = rng || randomBytes var hex = rng(strength / 8).toString('hex') return entropyToMnemonic(hex, wordlist) diff --git a/package.json b/package.json index 7b373eb..679cd16 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "license": "ISC", "dependencies": { "pbkdf2-compat": "^2.0.1", + "randombytes": "^2.0.1", "unorm": "^1.3.3" }, "devDependencies": { diff --git a/test/index.js b/test/index.js index 033f2a5..9cbff2c 100644 --- a/test/index.js +++ b/test/index.js @@ -1,7 +1,5 @@ var assert = require('assert') -var crypto = require('crypto') var BIP39 = require('../index.js') -var sinon = require('sinon') var wordlists = { english: require('../wordlists/en.json'), @@ -51,13 +49,11 @@ describe('BIP39', function() { describe('generateMnemonic', function() { vectors.english.forEach(function(v, i) { - it('works for tests vector ' + i, sinon.test(function() { - this.mock(crypto).expects('randomBytes') - .exactly(1) - .onCall(0).returns(new Buffer(v[0], 'hex')) + it('works for tests vector ' + i, function() { + function rng() { return new Buffer(v[0], 'hex') } - assert.equal(BIP39.generateMnemonic(), v[1]) - })) + assert.equal(BIP39.generateMnemonic(undefined, rng), v[1]) + }) }) it('can vary generated entropy bit length', function() { From ff287164d192b63764adb632c9cf256fb723c7ce Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 18 Mar 2015 19:23:26 +1100 Subject: [PATCH 2/5] index: use createHash/pbkdf2 directly --- index.js | 6 +++--- package.json | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index e457624..89006d9 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ var assert = require('assert') -var crypto = require('crypto') -var pbkdf2 = require('pbkdf2-compat').pbkdf2Sync +var createHash = require('create-hash') +var pbkdf2 = require('pbkdf2').pbkdf2Sync var randomBytes = require('randombytes') var unorm = require('unorm') @@ -87,7 +87,7 @@ function validateMnemonic(mnemonic, wordlist) { } function checksumBits(entropyBuffer) { - var hash = crypto.createHash('sha256').update(entropyBuffer).digest() + var hash = createHash('sha256').update(entropyBuffer).digest() // Calculated constants from BIP39 var ENT = entropyBuffer.length * 8 diff --git a/package.json b/package.json index 679cd16..feee8e1 100644 --- a/package.json +++ b/package.json @@ -21,13 +21,14 @@ }, "license": "ISC", "dependencies": { - "pbkdf2-compat": "^2.0.1", + "create-hash": "^1.1.0", + "pbkdf2": "^3.0.0", "randombytes": "^2.0.1", "unorm": "^1.3.3" }, "devDependencies": { - "browserify": "^8.0.3", - "mocha": "^2.1.0", + "browserify": "^9.0.0", + "mocha": "^2.2.0", "sinon": "^1.12.2" } } From 38d1ae21c2855500f03be11651cf6a64c5dcb1d2 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Wed, 18 Mar 2015 23:48:33 +0800 Subject: [PATCH 3/5] remove sinon as a dev dependency --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index feee8e1..fb5b11f 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ }, "devDependencies": { "browserify": "^9.0.0", - "mocha": "^2.2.0", - "sinon": "^1.12.2" + "mocha": "^2.2.0" } } From d99ec8630fb65b766b28f4465cc3177e192845cb Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Mar 2015 14:41:52 +1100 Subject: [PATCH 4/5] index: use utf8 explicitly --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 89006d9..c613cdb 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,10 @@ var unorm = require('unorm') var DEFAULT_WORDLIST = require('./wordlists/en.json') function mnemonicToSeed(mnemonic, password) { - return pbkdf2(mnemonic, salt(password), 2048, 64, 'sha512') + var mnemonicBuffer = new Buffer(mnemonic, 'utf8') + var saltBuffer = new Buffer(salt(password), 'utf8') + + return pbkdf2(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512') } function mnemonicToSeedHex(mnemonic, password) { From 1e2eb62cff2a52b50998afa195135c6ef034d6d7 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 8 Apr 2015 12:26:14 +1000 Subject: [PATCH 5/5] adds test for randombytes --- package.json | 3 ++- test/index.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index fb5b11f..1fc425f 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "devDependencies": { "browserify": "^9.0.0", - "mocha": "^2.2.0" + "mocha": "^2.2.0", + "mock-require": "^1.0.5" } } diff --git a/test/index.js b/test/index.js index 9cbff2c..b201882 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,10 @@ var assert = require('assert') +var mock = require('mock-require') + +mock('randombytes', function(size) { + return new Buffer('qwertyuiopasdfghjklzxcvbnm[];,./'.slice(0, size)) +}) + var BIP39 = require('../index.js') var wordlists = { @@ -63,6 +69,10 @@ describe('BIP39', function() { assert.equal(words.length, 9) }) + it('defaults to randombytes for the RNG', function() { + assert.equal(BIP39.generateMnemonic(32), 'imitate robot frequent') + }) + it('allows a custom RNG to be used', function() { var rng = function(size) { var buffer = new Buffer(size)