flocore/lib/hash.js
Ryan X. Charles 862235e57e initial commit
address, base58, base58check, hash all working with tests.

base58check code taken from bitcore.
2014-08-06 18:25:45 -07:00

43 lines
1.0 KiB
JavaScript

var hashjs = require('hash.js');
var sha512 = require('sha512');
var ripemd160 = require('ripemd160');
var Hash = module.exports;
Hash.sha256 = function(buf) {
if (!Buffer.isBuffer(buf))
throw new Error('sha256 hash must be of a buffer');
var hash = (new hashjs.sha256()).update(buf).digest();
return new Buffer(hash);
};
Hash.sha256sha256 = function(buf) {
try {
return Hash.sha256(Hash.sha256(buf));
} catch (e) {
throw new Error('sha256sha256 hash must be of a buffer');
}
};
Hash.ripemd160 = function(buf) {
if (!Buffer.isBuffer(buf))
throw new Error('ripemd160 hash must be of a buffer');
var hash = ripemd160(buf);
return new Buffer(hash);
};
Hash.sha256ripemd160 = function(buf) {
try {
return Hash.ripemd160(Hash.sha256(buf));
} catch (e) {
throw new Error('sha256ripemd160 hash must be of a buffer');
}
};
Hash.sha512 = function(buf) {
if (!Buffer.isBuffer(buf))
throw new Error('sha512 hash must be of a buffer');
var hash = sha512(buf);
return new Buffer(hash);
};