Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have tested your code, and it works as it should:</p> <pre><code>for i in $( seq 1 100 ); do openssl rand 9000 | head -c 9000 &gt; test YOUR=$( ./sha1 test | cut -b 12- ) SHA1=$( openssl dgst -sha1 test | cut -b 13- ) if [ "$YOUR" == "$SHA1" ]; then echo $YOUR OK else echo $YOUR $SHA1 FAIL fi done </code></pre> <p>Also works with lengths 8000, 9100, 10000.</p> <p>Tested your second code, this too appears to work correctly. This is the modified version I wrote to test on different files by supplying the name on command line:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;openssl/sha.h&gt; int main(int argc, char **argv) { unsigned char c[SHA_DIGEST_LENGTH]; int i; FILE *inFile = fopen (argv[1], "rb"); SHA_CTX shaContext; int bytes; unsigned char data[1024]; if (inFile == NULL) { printf ("%s can't be opened.\n", argv[1]); return 0; } SHA1_Init (&amp;shaContext); while ((bytes = fread (data, 1, 1024, inFile)) != 0) { printf("Read %d bytes\n", bytes); SHA1_Update (&amp;shaContext, data, bytes); } SHA1_Final (c,&amp;shaContext); for(i = 0; i &lt; SHA_DIGEST_LENGTH; i++) printf("%02x", c[i]); printf (" %s\n", argv[1]); fclose (inFile); return 0; } </code></pre> <p>There must be something in <em>how</em> the code is compiled that yields the wrong results.</p> <p>I checked with <code>gcc version 4.7.1 20120723 [gcc-4_7-branch revision 189773] (SUSE Linux)</code> on kernel 3.4.6 x86_64.</p> <p>I have modified the code above to print some information about its operation. I tested it with a file made up of 9000 zeroes (<code>dd if=/dev/zero of=/tmp/zeroes bs=1 count=9000</code>) and got:</p> <pre><code>Read 1024 bytes Read 1024 bytes Read 1024 bytes Read 1024 bytes Read 1024 bytes Read 1024 bytes Read 1024 bytes Read 1024 bytes Read 808 bytes 980eb7886d6d6ce206b9ce5371042d32c96d2714 /tmp/zeroes </code></pre> <p>If block sizes do match, then we'll try printing the sum of all bytes (which ought to be always zero), or check the buffer one byte at a time.</p> <p>If you have the same SHA1 as me, we'll try with a sequence of 9000 0x80's.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload