Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHA1 JavaScript Implementation for large string

I have the same problem as :Can SHA-1 algorithm be computed on a stream? With low memory footprint?

I'm looking for a JavaScript implementation which computed block by block of a very large string. The idea is to slice the string into 512 bits block and do it block by block.

Any hint?

[updated] Thanks sunetos's help, I write a litte html5 java script app: Generate SHA1 File Checksum Using HTML5 File API in Javascript

like image 924
Ke. Avatar asked Jul 08 '10 13:07

Ke.


1 Answers

I believe I came across one by Paul Johnston at http://pajhome.org.uk/crypt/md5/contrib/sha1_stream.js . It is listed on the page http://pajhome.org.uk/crypt/md5/scripts.html . I have not tested it myself, but I have used his non-streamable version that he modified for it.

UPDATE: Here is some example code (I verified it against a separate SHA1 known to be correct). Make sure you include the original sha1.js (found at http://pajhome.org.uk/crypt/md5/sha1.js) before the streamable sha1_stream.js.

<script src="sha1.js" type="text/javascript" charset="utf-8"></script>
<script src="sha1_stream.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript" charset="utf-8">

    var input = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';

    var blocksize = 512;
    var h = naked_sha1_head();
    for (var i = 0; i < input.length; i += blocksize) {
        var len = Math.min(blocksize, input.length - i);
        var block = input.substr(i, len);
        naked_sha1(str2binb(block), len*chrsz, h);
    }
    var result = binb2hex(naked_sha1_tail(h));

</script>
like image 114
sunetos Avatar answered Nov 01 '22 00:11

sunetos