Merge pull request #204 from dcousens/convclean

Convert Buffers, not Byte Arrays
This commit is contained in:
Wei Lu 2014-06-04 15:28:24 +08:00
commit 7f51832013
6 changed files with 64 additions and 64 deletions

View file

@ -2,42 +2,31 @@ var assert = require('assert')
var Crypto = require('crypto-js') var Crypto = require('crypto-js')
var WordArray = Crypto.lib.WordArray var WordArray = Crypto.lib.WordArray
function bytesToWords(bytes) { function bufferToWordArray(buffer) {
assert(Array.isArray(bytes) || Buffer.isBuffer(bytes), 'Input must be a byte array') assert(Buffer.isBuffer(buffer), 'Expected Buffer, got', buffer)
var words = [] var words = []
for (var i = 0, b = 0; i < bytes.length; i++, b += 8) { for (var i = 0, b = 0; i < buffer.length; i++, b += 8) {
words[b >>> 5] |= bytes[i] << (24 - b % 32) words[b >>> 5] |= buffer[i] << (24 - b % 32)
} }
return words
return new WordArray.init(words, buffer.length)
} }
function wordsToBytes(words) { function wordArrayToBuffer(wordArray) {
var bytes = [] assert(Array.isArray(wordArray.words), 'Expected WordArray, got' + wordArray)
for (var b = 0; b < words.length * 32; b += 8) {
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF)
}
return bytes
}
function bytesToWordArray(bytes) { var words = wordArray.words
return new WordArray.init(bytesToWords(bytes), bytes.length) var buffer = new Buffer(words.length * 4)
}
function wordArrayToBytes(wordArray) { words.forEach(function(value, i) {
return wordsToBytes(wordArray.words) buffer.writeInt32BE(value & -1, i * 4)
} })
function reverseEndian(hex) { return buffer
var buffer = new Buffer(hex, 'hex')
Array.prototype.reverse.call(buffer)
return buffer.toString('hex')
} }
module.exports = { module.exports = {
bytesToWords: bytesToWords, bufferToWordArray: bufferToWordArray,
wordsToBytes: wordsToBytes, wordArrayToBuffer: wordArrayToBuffer
bytesToWordArray: bytesToWordArray,
wordArrayToBytes: wordArrayToBytes,
reverseEndian: reverseEndian
} }

View file

@ -5,13 +5,12 @@ var crypto = require('crypto')
var convert = require('./convert') var convert = require('./convert')
function hash160(buffer) { function hash160(buffer) {
var step1 = sha256(buffer) var step1 = sha256(buffer)
var step2a = convert.bytesToWordArray(step1) var step2a = convert.bufferToWordArray(step1)
var step2b = CryptoJS.RIPEMD160(step2a) var step2b = CryptoJS.RIPEMD160(step2a)
return new Buffer(convert.wordArrayToBytes(step2b)) return convert.wordArrayToBuffer(step2b)
} }
function hash256(buffer) { function hash256(buffer) {
@ -35,12 +34,12 @@ function HmacSHA512(data, secret) {
assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data) assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data)
assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret) assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret)
var dataWords = convert.bytesToWordArray(data) var dataWords = convert.bufferToWordArray(data)
var secretWords = convert.bytesToWordArray(secret) var secretWords = convert.bufferToWordArray(secret)
var hash = CryptoJS.HmacSHA512(dataWords, secretWords) var hash = CryptoJS.HmacSHA512(dataWords, secretWords)
return new Buffer(convert.wordArrayToBytes(hash)) return convert.wordArrayToBuffer(hash)
} }
module.exports = { module.exports = {

View file

@ -29,7 +29,7 @@ describe('Address', function() {
}) })
fixtures.invalid.fromBase58Check.forEach(function(f) { fixtures.invalid.fromBase58Check.forEach(function(f) {
it('throws on ' + f.descpription, function() { it('throws on ' + f.description, function() {
assert.throws(function() { assert.throws(function() {
Address.fromBase58Check(f.base58check) Address.fromBase58Check(f.base58check)
}, new RegExp(f.exception)) }, new RegExp(f.exception))

View file

@ -1,40 +1,27 @@
var assert = require('assert') var assert = require('assert')
var convert = require('../src/convert') var convert = require('../src/convert')
var fixtures = require('./fixtures/convert')
describe('convert', function() { describe('convert', function() {
describe('byte array and word array conversions', function(){ describe('bufferToWordArray', function() {
var bytes, wordArray fixtures.valid.forEach(function(f) {
it('converts ' + f.hex + ' correctly', function() {
var buffer = new Buffer(f.hex, 'hex')
var result = convert.bufferToWordArray(buffer)
beforeEach(function(){ assert.deepEqual(result, f.wordArray)
bytes = [
98, 233, 7, 177, 92, 191, 39, 213, 66, 83,
153, 235, 246, 240, 251, 80, 235, 184, 143, 24
]
wordArray = {
words: [1659439025, 1556031445, 1112775147, -151979184, -340226280],
sigBytes: 20
}
})
describe('bytesToWords', function() {
it('works', function() {
assert.deepEqual(convert.bytesToWordArray(bytes), wordArray)
})
})
describe('bytesToWords', function() {
it('works', function() {
assert.deepEqual(convert.wordArrayToBytes(wordArray), bytes)
}) })
}) })
}) })
describe('reverseEndian', function() { describe('wordArrayToBuffer', function() {
it('works', function() { fixtures.valid.forEach(function(f) {
var bigEndian = "6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7" it('converts to ' + f.hex + ' correctly', function() {
var littleEdian = "c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a" var resultHex = convert.wordArrayToBuffer(f.wordArray).toString('hex')
assert.deepEqual(convert.reverseEndian(bigEndian), littleEdian)
assert.deepEqual(convert.reverseEndian(littleEdian), bigEndian) assert.deepEqual(resultHex, f.hex)
})
}) })
}) })
}) })

25
test/fixtures/convert.json vendored Normal file
View file

@ -0,0 +1,25 @@
{
"valid": [
{
"hex": "0000000000000000000000000000000000000000",
"wordArray": {
"words": [0, 0, 0, 0, 0],
"sigBytes": 20
}
},
{
"hex": "62e907b15cbf27d5425399ebf6f0fb50ebb88f18",
"wordArray": {
"words": [1659439025, 1556031445, 1112775147, -151979184, -340226280],
"sigBytes": 20
}
},
{
"hex": "ffffffffffffffffffffffffffffffffffffffff",
"wordArray": {
"words": [-1, -1, -1, -1, -1],
"sigBytes": 20
}
}
]
}

View file

@ -136,7 +136,7 @@ describe('HDNode', function() {
}) })
fixtures.invalid.fromBuffer.forEach(function(f) { fixtures.invalid.fromBuffer.forEach(function(f) {
it('throws on ' + f.string, function() { it('throws on ' + f.hex, function() {
assert.throws(function() { assert.throws(function() {
HDNode.fromHex(f.hex) HDNode.fromHex(f.hex)
}, new RegExp(f.exception)) }, new RegExp(f.exception))