|
|
@ -2,29 +2,28 @@ var assert = require('assert') |
|
|
|
var Crypto = require('crypto-js') |
|
|
|
var WordArray = Crypto.lib.WordArray |
|
|
|
|
|
|
|
function bufferToWords(buffer) { |
|
|
|
assert(Buffer.isBuffer(buffer), 'Expected Buffer, got' + buffer) |
|
|
|
function bufferToWordArray(buffer) { |
|
|
|
assert(Buffer.isBuffer(buffer), 'Expected Buffer, got', buffer) |
|
|
|
|
|
|
|
var words = [] |
|
|
|
for (var i = 0, b = 0; i < buffer.length; i++, b += 8) { |
|
|
|
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(bufferToWords(buffer), buffer.length) |
|
|
|
return new WordArray.init(words, buffer.length) |
|
|
|
} |
|
|
|
|
|
|
|
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) { |
|
|
@ -35,8 +34,6 @@ function reverseEndian(hex) { |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
bufferToWords: bufferToWords, |
|
|
|
wordsToBytes: wordsToBytes, |
|
|
|
bufferToWordArray: bufferToWordArray, |
|
|
|
wordArrayToBuffer: wordArrayToBuffer, |
|
|
|
reverseEndian: reverseEndian |
|
|
|