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
SHA1 in C
Posted: Jun 9, 2014 2:33 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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;
}




regards,

David Bernier






--
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.