From fc7e9350cc44dedd18b086bdbe69f42db4078a17 Mon Sep 17 00:00:00 2001 From: lomna-dev Date: Mon, 24 Apr 2023 12:01:37 +0530 Subject: [PATCH] Convert to single header Converged to a single header file --- dummyFile | 2 +- example.c | 10 +++- sha1.c | 168 ---------------------------------------------------- sha1.h | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+), 170 deletions(-) delete mode 100644 sha1.c diff --git a/dummyFile b/dummyFile index 5e1c309..557db03 100644 --- a/dummyFile +++ b/dummyFile @@ -1 +1 @@ -Hello World \ No newline at end of file +Hello World diff --git a/example.c b/example.c index 6fdf04f..dd6466d 100644 --- a/example.c +++ b/example.c @@ -1,7 +1,15 @@ #include "sha1.h" +#include 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); + char buffer[41]; + sprintf(buffer, "%x%x%x%x%x", r.h0, r.h1, r.h2, r.h3, r.h4); + + int i = 0; + while(buffer[i] != '\0') + printf("%c", buffer[i++]); + printf("\n"); + // Expected : 1d135491d20198c382098d477009a910d7e9ca22 return 0; } diff --git a/sha1.c b/sha1.c deleted file mode 100644 index 73d019f..0000000 --- a/sha1.c +++ /dev/null @@ -1,168 +0,0 @@ -#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 index 9bc1b3d..1c9f95a 100644 --- a/sha1.h +++ b/sha1.h @@ -1,4 +1,8 @@ +#ifndef _SHA1_H +#define _SHA1_H + #include +#include #include typedef struct SHA1_Result{ @@ -10,3 +14,173 @@ typedef struct SHA1_Result{ } SHA1_Result; SHA1_Result sha1file(char *filename); + +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; + /////////////////////// ****************************************** //////////////////////////////////////////// + } + } + + fclose(file); + + // 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; +} + +#endif