briannyagol / crypto-js

Automatically exported from code.google.com/p/crypto-js
0 stars 0 forks source link

Support for computing hashes incrementally #24

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I have an application where I want to incrementally compute hashes of data, eg.

  SHA256(A)
  SHA256(A+B)
  SHA256(A+B+C)
  etc.

I don't want this to be an O(n^2) algorithm, so I was wondering if the API 
could support a lower-level object with an API like:

  var sha256 = new Crypto.SHA256()
  sha256.append(A)
  var digestA = sha256.digest();
  sha256.append(B)
  var digestB = sha256.digest();
  // etc.

It seems like the algorithm allows this and that this would be easy to 
implement.  What do you think?

Original issue reported on code.google.com by jhaber...@gmail.com on 26 Jan 2012 at 6:10

GoogleCodeExporter commented 8 years ago
It's interesting idea. The upcoming 3.0 release will indeed support 
incrementally appending the message. Though, it'll be tricky to continue 
appending after you've completed the digest. Completing the digest involves 
adding padding, then hashing the last padded block.

Original comment by Jeff.Mott.OR on 26 Jan 2012 at 7:23

GoogleCodeExporter commented 8 years ago
Cool.  It seems doable if you just make the final padding and hashing 
calculation local to the function that returns the digest instead of mutating 
the object (you could cache the digest if you were worried about the efficiency 
of repeated calls to digest()).

Is the code for your upcoming 3.0 release currently available in a public 
repository?  If so I'd be happy to take a stab at it and send a patch, if you 
like.

Original comment by haber...@google.com on 26 Jan 2012 at 8:00

GoogleCodeExporter commented 8 years ago
http://code.google.com/p/crypto-js/source/browse/branches/#branches%2F3.x%2Fsrc

Original comment by Jeff.Mott.OR on 26 Jan 2012 at 8:08

GoogleCodeExporter commented 8 years ago
I also have the need to compute MD5 checksums incrementally. This, combined 
with the new HTML5 File APIs would allow for a very robust upload mechanism. 
Any estimate on when this functionality (supposedly in version 3) will be 
available?

Thanks!
Evan

Original comment by e...@dnanexus.com on 16 Mar 2012 at 9:23

GoogleCodeExporter commented 8 years ago
Version 3 is very close. I'd expect it to be ready within the next month. I'm 
mostly focusing on the cipher components now, and the hashing components are 
largely set.

Here's a preview for you.

<!doctype html>

<script 
src="http://crypto-js.googlecode.com/svn-history/r437/branches/3.x/src/core.js">
</script>
<script 
src="http://crypto-js.googlecode.com/svn-history/r437/branches/3.x/src/sha256.js
"></script>
<script 
src="http://crypto-js.googlecode.com/svn-history/r437/branches/3.x/src/enc-base6
4.js"></script>

<script>

// Basic hashing
var hash = CryptoJS.SHA256('abc');

// Output encoding
alert('Hash encoded as latin-1 string: ' + hash.toString(CryptoJS.enc.Latin1));
alert('Hash encoded as base-64 string: ' + hash.toString(CryptoJS.enc.Base64));
alert('Hash encoded as hex string: ' + hash); // Default is hex

// Incremental hashing
var sha256 = CryptoJS.algo.SHA256.create();
sha256.update('a');
sha256.update('b');
sha256.update('c');
var hash = sha256.finalize();

alert('Incrementally computed hash: ' + hash);

// Incremental hashing, capturing intermediate hash values
var sha256 = CryptoJS.algo.SHA256.create();
var hashA = sha256.update('a').clone().finalize();
var hashAB = sha256.update('b').clone().finalize();
var hashABC = sha256.update('c').clone().finalize();

alert('Intermediate hash value "a": ' + hashA);
alert('Intermediate hash value "ab": ' + hashAB);
alert('Intermediate hash value "abc": ' + hashABC);

</script>

Original comment by Jeff.Mott.OR on 16 Mar 2012 at 10:27

GoogleCodeExporter commented 8 years ago
This feature now officially available with version 3.

Original comment by Jeff.Mott.OR on 6 May 2012 at 7:51

GoogleCodeExporter commented 8 years ago
Great! Thanks for the excellent work, Jeff.

Original comment by e...@dnanexus.com on 7 May 2012 at 7:50