Browse Source

convert: use Buffer internally and remove unused functions

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
3b988a0872
  1. 31
      src/convert.js

31
src/convert.js

@ -2,29 +2,28 @@ 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 bufferToWords(buffer) { function bufferToWordArray(buffer) {
assert(Buffer.isBuffer(buffer), 'Expected Buffer, got' + buffer) assert(Buffer.isBuffer(buffer), 'Expected Buffer, got', buffer)
var words = [] var words = []
for (var i = 0, b = 0; i < buffer.length; i++, b += 8) { for (var i = 0, b = 0; i < buffer.length; i++, b += 8) {
words[b >>> 5] |= buffer[i] << (24 - b % 32) words[b >>> 5] |= buffer[i] << (24 - b % 32)
} }
return words
}
function wordsToBytes(words) {
var bytes = []
for (var b = 0; b < words.length * 32; b += 8) {
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF)
}
return bytes
}
function bufferToWordArray(buffer) { return new WordArray.init(words, buffer.length)
return new WordArray.init(bufferToWords(buffer), buffer.length)
} }
function wordArrayToBuffer(wordArray) { function wordArrayToBuffer(wordArray) {
return new Buffer(wordsToBytes(wordArray.words)) assert(Array.isArray(wordArray.words), 'Expected WordArray, got' + wordArray)
var words = wordArray.words
var buffer = new Buffer(words.length * 4)
words.forEach(function(value, i) {
buffer.writeInt32BE(value & -1, i * 4)
})
return buffer
} }
function reverseEndian(hex) { function reverseEndian(hex) {
@ -35,8 +34,6 @@ function reverseEndian(hex) {
} }
module.exports = { module.exports = {
bufferToWords: bufferToWords,
wordsToBytes: wordsToBytes,
bufferToWordArray: bufferToWordArray, bufferToWordArray: bufferToWordArray,
wordArrayToBuffer: wordArrayToBuffer, wordArrayToBuffer: wordArrayToBuffer,
reverseEndian: reverseEndian reverseEndian: reverseEndian

Loading…
Cancel
Save