Skip to content

Commit 7f51832

Browse files
committed
Merge pull request #204 from dcousens/convclean
Convert Buffers, not Byte Arrays
2 parents 63e6cf9 + 943621f commit 7f51832

File tree

6 files changed

+63
-63
lines changed

6 files changed

+63
-63
lines changed

src/convert.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,31 @@ var assert = require('assert')
22
var Crypto = require('crypto-js')
33
var WordArray = Crypto.lib.WordArray
44

5-
function bytesToWords(bytes) {
6-
assert(Array.isArray(bytes) || Buffer.isBuffer(bytes), 'Input must be a byte array')
5+
function bufferToWordArray(buffer) {
6+
assert(Buffer.isBuffer(buffer), 'Expected Buffer, got', buffer)
7+
78
var words = []
8-
for (var i = 0, b = 0; i < bytes.length; i++, b += 8) {
9-
words[b >>> 5] |= bytes[i] << (24 - b % 32)
9+
for (var i = 0, b = 0; i < buffer.length; i++, b += 8) {
10+
words[b >>> 5] |= buffer[i] << (24 - b % 32)
1011
}
11-
return words
12-
}
1312

14-
function wordsToBytes(words) {
15-
var bytes = []
16-
for (var b = 0; b < words.length * 32; b += 8) {
17-
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF)
18-
}
19-
return bytes
13+
return new WordArray.init(words, buffer.length)
2014
}
2115

22-
function bytesToWordArray(bytes) {
23-
return new WordArray.init(bytesToWords(bytes), bytes.length)
24-
}
16+
function wordArrayToBuffer(wordArray) {
17+
assert(Array.isArray(wordArray.words), 'Expected WordArray, got' + wordArray)
2518

26-
function wordArrayToBytes(wordArray) {
27-
return wordsToBytes(wordArray.words)
28-
}
19+
var words = wordArray.words
20+
var buffer = new Buffer(words.length * 4)
2921

30-
function reverseEndian(hex) {
31-
var buffer = new Buffer(hex, 'hex')
32-
Array.prototype.reverse.call(buffer)
22+
words.forEach(function(value, i) {
23+
buffer.writeInt32BE(value & -1, i * 4)
24+
})
3325

34-
return buffer.toString('hex')
26+
return buffer
3527
}
3628

3729
module.exports = {
38-
bytesToWords: bytesToWords,
39-
wordsToBytes: wordsToBytes,
40-
bytesToWordArray: bytesToWordArray,
41-
wordArrayToBytes: wordArrayToBytes,
42-
reverseEndian: reverseEndian
30+
bufferToWordArray: bufferToWordArray,
31+
wordArrayToBuffer: wordArrayToBuffer
4332
}

src/crypto.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ var crypto = require('crypto')
55
var convert = require('./convert')
66

77
function hash160(buffer) {
8-
98
var step1 = sha256(buffer)
109

11-
var step2a = convert.bytesToWordArray(step1)
10+
var step2a = convert.bufferToWordArray(step1)
1211
var step2b = CryptoJS.RIPEMD160(step2a)
1312

14-
return new Buffer(convert.wordArrayToBytes(step2b))
13+
return convert.wordArrayToBuffer(step2b)
1514
}
1615

1716
function hash256(buffer) {
@@ -35,12 +34,12 @@ function HmacSHA512(data, secret) {
3534
assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data)
3635
assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret)
3736

38-
var dataWords = convert.bytesToWordArray(data)
39-
var secretWords = convert.bytesToWordArray(secret)
37+
var dataWords = convert.bufferToWordArray(data)
38+
var secretWords = convert.bufferToWordArray(secret)
4039

4140
var hash = CryptoJS.HmacSHA512(dataWords, secretWords)
4241

43-
return new Buffer(convert.wordArrayToBytes(hash))
42+
return convert.wordArrayToBuffer(hash)
4443
}
4544

4645
module.exports = {

test/address.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Address', function() {
2929
})
3030

3131
fixtures.invalid.fromBase58Check.forEach(function(f) {
32-
it('throws on ' + f.descpription, function() {
32+
it('throws on ' + f.description, function() {
3333
assert.throws(function() {
3434
Address.fromBase58Check(f.base58check)
3535
}, new RegExp(f.exception))

test/convert.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
11
var assert = require('assert')
22
var convert = require('../src/convert')
33

4-
describe('convert', function() {
5-
describe('byte array and word array conversions', function(){
6-
var bytes, wordArray
7-
8-
beforeEach(function(){
9-
bytes = [
10-
98, 233, 7, 177, 92, 191, 39, 213, 66, 83,
11-
153, 235, 246, 240, 251, 80, 235, 184, 143, 24
12-
]
13-
wordArray = {
14-
words: [1659439025, 1556031445, 1112775147, -151979184, -340226280],
15-
sigBytes: 20
16-
}
17-
})
4+
var fixtures = require('./fixtures/convert')
185

19-
describe('bytesToWords', function() {
20-
it('works', function() {
21-
assert.deepEqual(convert.bytesToWordArray(bytes), wordArray)
22-
})
23-
})
6+
describe('convert', function() {
7+
describe('bufferToWordArray', function() {
8+
fixtures.valid.forEach(function(f) {
9+
it('converts ' + f.hex + ' correctly', function() {
10+
var buffer = new Buffer(f.hex, 'hex')
11+
var result = convert.bufferToWordArray(buffer)
2412

25-
describe('bytesToWords', function() {
26-
it('works', function() {
27-
assert.deepEqual(convert.wordArrayToBytes(wordArray), bytes)
13+
assert.deepEqual(result, f.wordArray)
2814
})
2915
})
3016
})
3117

32-
describe('reverseEndian', function() {
33-
it('works', function() {
34-
var bigEndian = "6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7"
35-
var littleEdian = "c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a"
36-
assert.deepEqual(convert.reverseEndian(bigEndian), littleEdian)
37-
assert.deepEqual(convert.reverseEndian(littleEdian), bigEndian)
18+
describe('wordArrayToBuffer', function() {
19+
fixtures.valid.forEach(function(f) {
20+
it('converts to ' + f.hex + ' correctly', function() {
21+
var resultHex = convert.wordArrayToBuffer(f.wordArray).toString('hex')
22+
23+
assert.deepEqual(resultHex, f.hex)
24+
})
3825
})
3926
})
4027
})

test/fixtures/convert.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"valid": [
3+
{
4+
"hex": "0000000000000000000000000000000000000000",
5+
"wordArray": {
6+
"words": [0, 0, 0, 0, 0],
7+
"sigBytes": 20
8+
}
9+
},
10+
{
11+
"hex": "62e907b15cbf27d5425399ebf6f0fb50ebb88f18",
12+
"wordArray": {
13+
"words": [1659439025, 1556031445, 1112775147, -151979184, -340226280],
14+
"sigBytes": 20
15+
}
16+
},
17+
{
18+
"hex": "ffffffffffffffffffffffffffffffffffffffff",
19+
"wordArray": {
20+
"words": [-1, -1, -1, -1, -1],
21+
"sigBytes": 20
22+
}
23+
}
24+
]
25+
}

test/hdnode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('HDNode', function() {
136136
})
137137

138138
fixtures.invalid.fromBuffer.forEach(function(f) {
139-
it('throws on ' + f.string, function() {
139+
it('throws on ' + f.hex, function() {
140140
assert.throws(function() {
141141
HDNode.fromHex(f.hex)
142142
}, new RegExp(f.exception))

0 commit comments

Comments
 (0)