7

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

1
  • link broken, working fiddle in the comment from user unsynchronized Commented Oct 7, 2018 at 13:06

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

3 Comments

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?
Note that the current version of the sha1_stream.js has a bug and will give incorrect results for certain input lengths. To fix this replace h[8] += 512 - len % 512; with h[8] = (len + 576 >> 9) << 9; - it forgets to include the padding when rounding to a multiple of 512 bits. The non-streaming version doesn't have that bug.
here's a jsfiddle based on this answer:jsfiddle.net/ndJ2x/9/embedded/result source is here jsfiddle.net/ndJ2x/9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.