Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » sci.math.* » sci.math.independent

Topic: SHA1 in C
Replies: 2   Last Post: Jun 9, 2014 3:43 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
David Bernier

Posts: 3,276
Registered: 12/13/04
Re: SHA1 in C
Posted: Jun 9, 2014 2:57 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 06/09/2014 02:33 AM, David Bernier wrote:
> I read about sha1 secure hash and the C language here:
>
> Stack Overflow, Answer:
>
> http://stackoverflow.com/questions/3467097/correctly-getting-sha-1-for-files-using-openssl
>
>
> I want to read a 8192-byte block from a file, then compute the sha1sum
> digest and print it to a file, then read another 8192-byte block from a
> file and compute its sha1sum, and so on.
>
> This with a file of a camera recording tube-noise on a TV, say
> 1 gigabyte.
>
> One refererence gives the prototype for SHA1_Update :
>
> http://linux.die.net/man/3/sha1_update
>
> We find:
>
> int SHA1_Update(SHA_CTX *c, const void *data, unsigned long len);
>
>
> ---
>
>
> What does "const" in const void *data signify?
>
> I hear that "const" implies assignment cannot be done at run-time ...
>
> Now, we have a "const void pointer" ...
>
> I have unsigned char data in a buffer of length 8192.
>
> So, I can change what's in the buffer, but not change the pointer, i.e.
> &buffer[0] ?
>
> Anyway, this works with
> test10 containing 8192 '0', then 8192 '1', ... then 8192 '9' for a total
> of 81,920 characters:
>
> [david@localhost ZIPfiles]$ gcc -lcrypto -o tst5_sha1.out tst5_sha1.c
>
>
> #include <openssl/sha.h>
> #include <stdio.h>
>
> int main(void)
> {
> FILE *f;
> unsigned char buf[8192];
> unsigned char pow2[8];
> SHA_CTX sc;
> int i;
> int j;
> int k;
> int bits[8];
> int hex0, hex1;
> unsigned char hexes[16];
>
> hexes[0] = '0';
> hexes[1] = '1';
> hexes[2] = '2';
> hexes[3] = '3';
> hexes[4] = '4';
> hexes[5] = '5';
> hexes[6] = '6';
> hexes[7] = '7';
> hexes[8] = '8';
> hexes[9] = '9';
> hexes[10] = 'a';
> hexes[11] = 'b';
> hexes[12] = 'c';
> hexes[13] = 'd';
> hexes[14] = 'e';
> hexes[15] = 'f';
>
>
>
>
>
>
>
> FILE *h0;
> FILE *h1;
> FILE *h2;
> FILE *h3;
> FILE *h4;
> FILE *h5;
> FILE *h6;
> FILE *h7;
> FILE *h8;
> FILE *h9;
>
>
> pow2[0] = (unsigned char) 1;
> pow2[1] = (unsigned char) 2;
> pow2[2] = (unsigned char) 4;
> pow2[3] = (unsigned char) 8;
> pow2[4] = (unsigned char) 16;
> pow2[5] = (unsigned char) 32;
> pow2[6] = (unsigned char) 64;
> pow2[7] = (unsigned char) 128;
>
>
>
>
>
>
>
>
>
>
>
> unsigned char out[20];
>
>
>
> f = fopen("/home/david/ZIPfiles/test10", "rb");
>
> h0 = fopen("/home/david/ZIPfiles/hash0", "wb");
>
> h1 = fopen("/home/david/ZIPfiles/hash1", "wb");
>
> h2 = fopen("/home/david/ZIPfiles/hash2", "wb");
>
> h3 = fopen("/home/david/ZIPfiles/hash3", "wb");
>
> h4 = fopen("/home/david/ZIPfiles/hash4", "wb");
>
> h5 = fopen("/home/david/ZIPfiles/hash5", "wb");
>
> h6 = fopen("/home/david/ZIPfiles/hash6", "wb");
>
> h7 = fopen("/home/david/ZIPfiles/hash7", "wb");
>
> h8 = fopen("/home/david/ZIPfiles/hash8", "wb");
>
> h9 = fopen("/home/david/ZIPfiles/hash9", "wb");
>
>
>
>
>
>
>
> for(j=0; j<10; j++)
> {
>
> SHA1_Init(&sc);
>
> size_t len;
>
> len = fread(buf, 1, sizeof buf, f);
>
> SHA1_Update(&sc, buf, len);
>
>
> SHA1_Final(out, &sc);
>
> if(j==0)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h0, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h0);
> }
>
>
> if(j==1)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h1, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h1);
> }
>
>
> if(j==2)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h2, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h2);
> }
>
>
> if(j==3)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h3, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h3);
> }
>
>
> if(j==4)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h4, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h4);
> }
>
>
>
> if(j==5)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h5, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h5);
> }
>
>
> if(j==6)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h6, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h6);
> }
>
>
> if(j==7)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h7, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h7);
> }
>
>
>
> if(j==8)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h8, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h8);
> }
>
>
> if(j==9)
> {
> for(i=0; i<20; i++)
> {
> for(k=0 ; k<8; k++)
> {
> bits[7-k] = (int) ((out[i]&pow2[k])>>k);
> }
>
> hex0 = 8*bits[0] + 4*bits[1] + 2*bits[2] + bits[3];
> hex1 = 8*bits[4] + 4*bits[5] + 2*bits[6] + bits[7];
>
> fprintf(h9, "%c%c", hexes[hex0], hexes[hex1]);
> }
>
> fclose(h9);
> }
>
>
>
>
>
>
>
>
> }
>
> fclose(f);
>
> return 0;
> }
>
>
>



First, sha1sum of 8192 '0's in a file, ... 8192 '9's in a file:

[david@localhost ZIPfiles]$ sha1sum in0
bd5fdf6bf5aa7db12d8cb6a4ee066adad41dc0d6 in0
[david@localhost ZIPfiles]$ sha1sum in1
9e42a6f5039b8bf058333aa78878ca5575994735 in1
[david@localhost ZIPfiles]$ sha1sum in2
7f60091fed1434b6085266256f0f5de6728531bc in2
[david@localhost ZIPfiles]$ sha1sum in3
36ac704cdca4c32c51970346a2285d8150b79106 in3
[david@localhost ZIPfiles]$ sha1sum in4
a1b4a1e6092952ea4afbd37109ef21f86a98980b in4
[david@localhost ZIPfiles]$ sha1sum in5
a7133dcae23a9bd6ee1b3f6e3ae27cb1286e7052 in5
[david@localhost ZIPfiles]$ sha1sum in6
f41edb99d562f9f118cd03f522c9adf86692aa1c in6
[david@localhost ZIPfiles]$ sha1sum in7
20b0119609c6ec80e995a9fe327d461a0cc83567 in7
[david@localhost ZIPfiles]$ sha1sum in8
846e364ee02074ec6e3013408794fc03bee79313 in8
[david@localhost ZIPfiles]$ sha1sum in9
e4e5a221c274f17692a3adb47bb0be5666cdbe3e in9

-----------------------------------------------------

Now, hashing a file of 81920 characters in blocks of 8192 (should be same):



bd5fdf6bf5aa7db12d8cb6a4ee066adad41dc0d6
9e42a6f5039b8bf058333aa78878ca5575994735
7f60091fed1434b6085266256f0f5de6728531bc
36ac704cdca4c32c51970346a2285d8150b79106
a1b4a1e6092952ea4afbd37109ef21f86a98980b
a7133dcae23a9bd6ee1b3f6e3ae27cb1286e7052
f41edb99d562f9f118cd03f522c9adf86692aa1c
20b0119609c6ec80e995a9fe327d461a0cc83567
846e364ee02074ec6e3013408794fc03bee79313
e4e5a221c274f17692a3adb47bb0be5666cdbe3e



--
http://meditationatae.wordpress.com/



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.