2014-03-08 13:02:40 +08:00
|
|
|
var Crypto = require('crypto-js');
|
|
|
|
var RIPEMD160 = Crypto.RIPEMD160;
|
|
|
|
var SHA256 = Crypto.SHA256;
|
|
|
|
var HMAC= Crypto.algo.HMAC;
|
|
|
|
var WordArray = Crypto.lib.WordArray;
|
2012-08-17 09:14:07 +02:00
|
|
|
|
2014-01-11 13:57:05 +07:00
|
|
|
/**
|
|
|
|
* Create a byte array representing a number with the given length
|
|
|
|
*/
|
|
|
|
exports.numToBytes = function(num, bytes) {
|
2013-10-08 06:34:15 -04:00
|
|
|
if (bytes === undefined) bytes = 8;
|
2014-01-11 13:57:05 +07:00
|
|
|
if (bytes === 0) return [];
|
|
|
|
return [num % 256].concat(module.exports.numToBytes(Math.floor(num / 256), bytes - 1));
|
|
|
|
}
|
2013-10-08 06:34:15 -04:00
|
|
|
|
2014-01-11 13:57:05 +07:00
|
|
|
/**
|
|
|
|
* Convert a byte array to the number that it represents
|
|
|
|
*/
|
|
|
|
exports.bytesToNum = function(bytes) {
|
|
|
|
if (bytes.length === 0) return 0;
|
|
|
|
return bytes[0] + 256 * module.exports.bytesToNum(bytes.slice(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn an integer into a "var_int".
|
|
|
|
*
|
|
|
|
* "var_int" is a variable length integer used by Bitcoin's binary format.
|
|
|
|
*
|
|
|
|
* Returns a byte array.
|
|
|
|
*/
|
|
|
|
exports.numToVarInt = function(num) {
|
2013-10-07 08:21:00 -04:00
|
|
|
if (num < 253) return [num];
|
2014-01-11 13:57:05 +07:00
|
|
|
if (num < 65536) return [253].concat(exports.numToBytes(num, 2));
|
|
|
|
if (num < 4294967296) return [254].concat(exports.numToBytes(num, 4));
|
|
|
|
return [253].concat(exports.numToBytes(num, 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.bytesToWords = function (bytes) {
|
|
|
|
var words = [];
|
|
|
|
for (var i = 0, b = 0; i < bytes.length; i++, b += 8) {
|
|
|
|
words[b >>> 5] |= bytes[i] << (24 - b % 32);
|
|
|
|
}
|
|
|
|
return words;
|
|
|
|
}
|
2012-01-11 10:41:52 +01:00
|
|
|
|
2014-01-11 13:57:05 +07:00
|
|
|
exports.wordsToBytes = function (words) {
|
|
|
|
var bytes = [];
|
|
|
|
for (var b = 0; b < words.length * 32; b += 8) {
|
|
|
|
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
|
|
|
|
}
|
|
|
|
return bytes;
|
2014-03-08 13:02:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.bytesToWordArray = function (bytes) {
|
|
|
|
return new WordArray.init(exports.bytesToWords(bytes), bytes.length)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.wordArrayToBytes = function (wordArray) {
|
|
|
|
return exports.wordsToBytes(wordArray.words)
|
|
|
|
}
|
2013-11-18 23:47:56 -05:00
|
|
|
|
2014-01-11 13:57:05 +07:00
|
|
|
/**
|
|
|
|
* Calculate RIPEMD160(SHA256(data)).
|
|
|
|
*
|
|
|
|
* Takes an arbitrary byte array as inputs and returns the hash as a byte
|
|
|
|
* array.
|
|
|
|
*/
|
|
|
|
exports.sha256ripe160 = function (data) {
|
2014-03-08 13:02:40 +08:00
|
|
|
var wordArray = RIPEMD160(SHA256(exports.bytesToWordArray(data)))
|
|
|
|
return exports.wordArrayToBytes(wordArray)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exports.HmacFromBytesToBytes = function (hasher, message, key) {
|
|
|
|
var hmac = HMAC.create(hasher, exports.bytesToWordArray(key))
|
|
|
|
hmac.update(exports.bytesToWordArray(message))
|
|
|
|
return exports.wordArrayToBytes(hmac.finalize())
|
2014-01-11 13:57:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.error = function(msg) {
|
2013-10-21 14:00:31 -04:00
|
|
|
throw new Error(msg);
|
2014-01-11 13:57:05 +07:00
|
|
|
}
|