Skip to content

Commit

Permalink
fixes ripe160 add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Mar 12, 2014
1 parent 6af1b1d commit ab183c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
15 changes: 14 additions & 1 deletion browser/bitcoinjs-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,20 @@ f+(((m|~n)^p)+c[2]):64>b?f+((m&p|n&~p)+c[3]):f+((m^(n|~p))+c[4]),f|=0,f=f<<k[b]|
e[a>>>5]|=128<<24-a%32;e[(a+64>>>9<<4)+14]=(b<<8|b>>>24)&16711935|(b<<24|b>>>8)&4278255360;g.sigBytes=4*(e.length+1);this._process();g=this._hash;e=g.words;for(b=0;5>b;b++)a=e[b],e[b]=(a<<8|a>>>24)&16711935|(a<<24|a>>>8)&4278255360;return g},clone:function(){var e=l.clone.call(this);e._hash=this._hash.clone();return e}});j.RIPEMD160=l._createHelper(k);j.HmacRIPEMD160=l._createHmacHelper(k)})(Math);


module.exports.RIPEMD160 = CryptoJS.RIPEMD160;
module.exports.ripemd160 = function(bytes) {
if (!Buffer.isBuffer(bytes)) {
throw new Error('arg should be a buffer');
}
var w = new CryptoJS.lib.WordArray.init(Crypto.util.bytesToWords(bytes), bytes.length);
var wordArray = CryptoJS.RIPEMD160(w);
var words = wordArray.words;
var answer = [];
for (var b = 0; b < words.length * 32; b += 8) {
answer.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
}
return answer;
};

Bitcoin = {};


Expand Down
20 changes: 15 additions & 5 deletions examples/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
div.innerHTML += s + '<br />';
};

print('<hr> <h1>Address</h1>' );
var addrStrings = [
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
"1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx",
Expand All @@ -38,8 +39,7 @@
}

});
print('<hr>');

print('<hr> <h1>KeyModule</h1>' );
/*
Using bitcore root module
*/
Expand All @@ -50,11 +50,11 @@
print ('Private:' + bitcore.buffertools.toHex(k.private));
print ('Public:' + bitcore.buffertools.toHex(k.public));

print('<hr>');
print('<hr> <h1>PeerManager</h1>' );

var p = new bitcore.PeerManager();

print('<hr>');
print('<hr> <h1>Util</h1>' );
var coinUtil = bitcore.util;

var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071'
Expand All @@ -64,10 +64,20 @@
pubKeyHash = coinUtil.sha256ripe160(pk);
print(bitcore.buffertools.toHex(pubKeyHash));


var Buffer = bitcore.Buffer;

pubKeyHash = coinUtil.ripe160(new bitcore.Buffer('hola'));
print(bitcore.buffertools.toHex(pubKeyHash));

print('<hr>');
var bu = new Buffer('a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d', 'hex');
print(bitcore.buffertools.toHex(bu));

var pubKeyHash2 = coinUtil.ripe160(bu);
print(bitcore.buffertools.toHex(pubKeyHash2));


print('<hr><h1>WalletKey </h1>');
var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks;

Expand Down
12 changes: 11 additions & 1 deletion test/test.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ describe('util', function() {
});
});
});
describe('#ripe160', function() {
var pk = 'a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d';
it('should work for ' + pk, function() {
var pubKeyHash = coinUtil.ripe160(new Buffer(pk,'hex'));
var pkh = buffertools.toHex(pubKeyHash);
pkh.should.equal('d166a41f27fd4b158f70314e5eee8998bf3d97d5');
});
});


describe('#sha256', function() {
var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071'
it('should work for ' + pk, function() {
Expand All @@ -61,7 +71,7 @@ describe('util', function() {
];
ripemdData.forEach(function(datum) {
it('should work for ' + datum[0], function() {
var r = coinUtil.ripe160(datum[0]);
var r = coinUtil.ripe160( new bitcore.Buffer(datum[0]));
buffertools.toHex(r).should.equal(datum[1]);
});
it('should work for Buffer ' + datum[0], function() {
Expand Down
9 changes: 5 additions & 4 deletions util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ var sha256 = exports.sha256 = function (data) {
};

var ripe160 = exports.ripe160 = function (data) {
if (!Buffer.isBuffer(data)) {
throw new Error('arg should be a buffer');
}

if (!process.versions) {
var RIPEMD160 = bjs.RIPEMD160;
var WordArray = bjs.WordArray;
data = data.toString();
var result = RIPEMD160(data) + '';
var result = bjs.ripemd160(data);
return new Buffer(result, 'hex');
}
return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary');
Expand Down

0 comments on commit ab183c0

Please sign in to comment.