views:

66

answers:

1

Hi All,

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

+2  A: 

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>
sunetos
Thanks sunetos.I guess this is what I want. But there is no document nor example to show how to use it. Can you help me?
Ke
Just updated it to show a sample of how to use it.
sunetos