diff --git a/lib/crypto.browser.js b/lib/crypto.browser.js index dc78860..7415e1a 100644 --- a/lib/crypto.browser.js +++ b/lib/crypto.browser.js @@ -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. diff --git a/lib/crypto.js b/lib/crypto.js index c341da4..c8b5154 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -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); diff --git a/test.js b/test.js index e5aefca..39e01b5 100644 --- a/test.js +++ b/test.js @@ -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");