SHA-256 crypto (for WIF)

This commit is contained in:
Kagami Hiiragi 2014-12-19 15:34:33 +03:00
parent 6e4cb56a27
commit 2e9f064e7e
3 changed files with 18 additions and 0 deletions

View File

@ -25,6 +25,12 @@ exports.sha512 = function(buf) {
});
};
exports.sha256 = function(buf) {
return subtle.digest({name: "SHA-256"}, buf).then(function(arr) {
return new Buffer(new Uint8Array(arr));
});
};
exports.ripemd160 = function(buf) {
// XXX(Kagami): No support in browsers via Web Crypto API currently,
// so use module.

View File

@ -14,6 +14,12 @@ exports.sha512 = function(buf) {
return Promise.resolve(hash.digest());
};
exports.sha256 = function(buf) {
var hash = crypto.createHash("sha256");
hash.update(buf);
return Promise.resolve(hash.digest());
};
exports.ripemd160 = function(buf) {
var hash = crypto.createHash("ripemd160");
hash.update(buf);

View File

@ -47,6 +47,12 @@ describe("Crypto", function() {
});
});
it("should implement SHA-256 hash", function() {
return bmcrypto.sha256(Buffer("test")).then(function(res) {
expect(res.toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");
});
});
it("should implement RIPEMD-160 hash", function() {
return bmcrypto.ripemd160(Buffer("test")).then(function(res) {
expect(res.toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107");