diff --git a/dummyFile b/dummyFile new file mode 100644 index 0000000..5e1c309 --- /dev/null +++ b/dummyFile @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/example.c b/example.c new file mode 100644 index 0000000..6fdf04f --- /dev/null +++ b/example.c @@ -0,0 +1,7 @@ +#include "sha1.h" + +int main(){ + SHA1_Result r = sha1file("dummyFile"); + printf("\nFinal result %x%x%x%x%x", r.h0, r.h1, r.h2, r.h3, r.h4); + return 0; +} diff --git a/sha1.c b/sha1.c new file mode 100644 index 0000000..73d019f --- /dev/null +++ b/sha1.c @@ -0,0 +1,168 @@ +#include "sha1.h" +#include + +uint32_t leftRotate(uint32_t n, unsigned int d) +{ + return (n << d) | (n >> (32 - d)); +} + +SHA1_Result sha1file(char *filename){ + FILE* file; + + SHA1_Result r; + uint32_t number_of_chuncks = 0; + uint32_t size_in_bytes = 0; + uint64_t size_in_bits = 0; + + uint32_t h0 = 1742612993; + uint32_t h1 = 3137593996; + uint32_t h2 = 18805212; + uint32_t h3 = 2459167202; + uint32_t h4 = 3455001513; + + file = fopen(filename,"r"); + if(errno != 0){ + fprintf(stderr, "\nErrno : %d\n", errno); + perror(filename); + return r; + } + + size_t bytes_read; + char buffer[64]; + + while(1){ + bytes_read = fread(buffer, 1, 64, file); + number_of_chuncks++; + if(bytes_read != 64){ + // * We need to convert to 512 bit * // + // printf("\nWe read %d bytes", bytes_read); + size_in_bytes += bytes_read; // the final size is done here + size_in_bits = size_in_bytes * 8; + + //padding + buffer[bytes_read] = 0x80; + bytes_read += 1; + + for(int i = bytes_read; i <= 55; i++) + buffer[i] = 0x00; + + //adding size to end + for(int i = 63; i >= 56; i--){ + buffer[i] = size_in_bits % 256; + size_in_bits /= 256; + } + + /////////////////////// ****************************************** //////////////////////////////////////////// + //continue how we do for 512 bit chunck + uint32_t w[80]; + for(int i = 0; i < 16; i++) + w[i] = buffer[i] + buffer[i+1] * 256 + buffer[i+2] * 65536 + buffer[i+3] * 16777216; + for(int i = 16; i < 80; i++) + w[i] = leftRotate( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i - 16] , 1 ); + + uint32_t a = h0; + uint32_t b = h1; + uint32_t c = h2; + uint32_t d = h3; + uint32_t e = h4; + + uint32_t f; + uint32_t k; + + // The 80 iterations + for(int i = 0; i < 80; i++){ + if(i >= 0 && i <= 19){ + f = (b & c) ^ (( ~ b ) & d); + k = 1518500249; + }else if ( i >= 20 && i <= 39){ + f = b ^ c ^ d; + k = 1859775393; + }else if (i >= 40 && i <= 59){ + f = (b & c) ^ (b & d) ^ (c & d); + k = 2400959708; + }else if (i >= 60 && i <= 79){ + f = b ^ c ^ d; + k = 3395469782; + } + + uint32_t temp = leftRotate(a,5) + f + e + k + w[i]; + e = d; + d = c; + c = leftRotate(b, 30); + b = a; + a = temp; + } + + // Add this chunk's hash result + h0 += a; + h1 += b; + h2 += c; + h3 += d; + h4 += e; + /////////////////////// ****************************************** //////////////////////////////////////////// + break; + } + else{ + // * We got a 512 bit chunk * // + // printf("\nWe read 64 bytes"); + size_in_bytes += 64; + + /////////////////////// ****************************************** //////////////////////////////////////////// + uint32_t w[80]; + for(int i = 0; i < 16; i++) + w[i] = buffer[i] + buffer[i+1] * 256 + buffer[i+2] * 65536 + buffer[i+3] * 16777216; + for(int i = 16; i < 80; i++) + w[i] = leftRotate( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i - 16] , 1 ); + + uint32_t a = h0; + uint32_t b = h1; + uint32_t c = h2; + uint32_t d = h3; + uint32_t e = h4; + + uint32_t f; + uint32_t k; + + // The 80 iterations + for(int i = 0; i < 80; i++){ + if(i >= 0 && i <= 19){ + f = (b & c) ^ (( ~ b ) & d); + k = 1518500249; + }else if ( i >= 20 && i <= 39){ + f = b ^ c ^ d; + k = 1859775393; + }else if (i >= 40 && i <= 59){ + f = (b & c) ^ (b & d) ^ (c & d); + k = 2400959708; + }else if (i >= 60 && i <= 79){ + f = b ^ c ^ d; + k = 3395469782; + } + + uint32_t temp = leftRotate(a,5) + f + e + k + w[i]; + e = d; + d = c; + c = leftRotate(b, 30); + b = a; + a = temp; + } + + // Add this chunk's hash result + h0 += a; + h1 += b; + h2 += c; + h3 += d; + h4 += e; + /////////////////////// ****************************************** //////////////////////////////////////////// + } + } + + // printf("\nFinal result %x%x%x%x%x", h0, h1, h2, h3, h4); + r.h0 = h0; + r.h1 = h1; + r.h2 = h2; + r.h3 = h3; + r.h4 = h4; + + return r; +} diff --git a/sha1.h b/sha1.h new file mode 100644 index 0000000..9bc1b3d --- /dev/null +++ b/sha1.h @@ -0,0 +1,12 @@ +#include +#include + +typedef struct SHA1_Result{ + uint32_t h0; + uint32_t h1; + uint32_t h2; + uint32_t h3; + uint32_t h4; +} SHA1_Result; + +SHA1_Result sha1file(char *filename);