Bitcoin Forum

Other => Off-topic => Topic started by: Hyena on April 09, 2016, 10:43:33 AM



Title: RIPEMD-160 progressive hashing?
Post by: Hyena on April 09, 2016, 10:43:33 AM
This may or may not be a suitable question for this subforum but since Bitcoin utilizes RIPEMD-160 then perhaps there are people here who know the technical essence of this hashing algorithm.

I would like to know whether it is possible to calculate a RIPEMD-160 hash of a byte string on the go. For example, I need to calculate the hash of some binary data with an unknown length. I am receiving that data chunk-by-chunk and every chunk could be the last one. However, I cannot keep all of the data in the memory at once.

How to calculate the RIPEMD-160 hash of a byte string in such conditions? Eventually I would have to do it in JavaScript. Perhaps it is really easy? For example the current hash would be the hash of the concatenation of the last hash and the current 20 chunk of 20 bytes of data (just guessing).


Title: Re: RIPEMD-160 progressive hashing?
Post by: hhanh00 on April 09, 2016, 11:27:25 AM
You can use crypto-js.


Title: Re: RIPEMD-160 progressive hashing?
Post by: Hyena on April 09, 2016, 11:59:37 AM
You can use crypto-js.

Yes, I am actually already using it. However, RIPEMD-160 and other hashing functions typically require the whole data to be hashed as an argument. In my case I can only provide 20 bytes of data at a time.


Title: Re: RIPEMD-160 progressive hashing?
Post by: hhanh00 on April 09, 2016, 12:29:57 PM
Does it? Extract from their doc:

Code:
Progressive Hashing

var sha256 = CryptoJS.algo.SHA256.create();
sha256.update("Message Part 1");
sha256.update("Message Part 2");
sha256.update("Message Part 3");
var hash = sha256.finalize();



Title: Re: RIPEMD-160 progressive hashing?
Post by: Hyena on April 09, 2016, 12:31:28 PM
Does it? Extract from their doc:

Code:
Progressive Hashing

var sha256 = CryptoJS.algo.SHA256.create();
sha256.update("Message Part 1");
sha256.update("Message Part 2");
sha256.update("Message Part 3");
var hash = sha256.finalize();



holy shit, it looks exactly what I have been looking for. I need to test this out now, thank you  very much!

edit:
hopefully finalize function is not computationally very expensive, so the hash is gradually calculated during the calls to the update function. I have yet to verify if it's really the case.
looking at their documentation I am pretty confident that it is the case

edit 2:
So I have included ripemd160.js from here (https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/crypto-js/CryptoJS%20v3.1.2.zip) in my project and the following code actually is exactly what I needed.
Code:
    var ripemd160 = CryptoJS.algo.RIPEMD160.create(); 
    ripemd160.update("Message Part 1");
    ripemd160.update("Message Part 2");
    ripemd160.update("Message Part 3");
    var hash = ripemd160.finalize();
    alert(hash);