From 55afeb3eaf56068edef1d7883a6c045ea0551174 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 8 Jul 2015 22:15:20 -0400 Subject: [PATCH] Use Node.js crypto ripemd160 hash if available. --- lib/crypto/hash.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/crypto/hash.js b/lib/crypto/hash.js index 0661a46..f6a625a 100644 --- a/lib/crypto/hash.js +++ b/lib/crypto/hash.js @@ -29,10 +29,19 @@ Hash.sha256sha256 = function(buf) { Hash.ripemd160 = function(buf) { $.checkArgument(BufferUtil.isBuffer(buf)); - var hash = (new hashjs.ripemd160()).update(buf).digest(); - return new Buffer(hash); + return crypto.createHash('ripemd160').update(buf).digest(); }; +// Node.js crypto ripemd160 hashes are not supported in a browser +// We'll replace with a (slower) version that does. +if (global.window) { + Hash.ripemd160 = function(buf) { + $.checkArgument(BufferUtil.isBuffer(buf)); + var hash = (new hashjs.ripemd160()).update(buf).digest(); + return new Buffer(hash); + }; +} + Hash.sha256ripemd160 = function(buf) { $.checkArgument(BufferUtil.isBuffer(buf)); return Hash.ripemd160(Hash.sha256(buf));