From 19304cf56ad1db0635af393c575b37c0c4ad9ad0 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 14 Dec 2014 17:55:13 +0100 Subject: [PATCH] Add sha1_calculate (takes a filename path) --- hash.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- hash.h | 4 +--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/hash.c b/hash.c index a0af63994c..abb2ae8098 100644 --- a/hash.c +++ b/hash.c @@ -56,6 +56,7 @@ #include #include +#include #include "hash.h" #include #include @@ -277,7 +278,7 @@ uint32_t crc32_calculate(const uint8_t *data, size_t length) /* Define the circular shift macro */ #define SHA1CircularShift(bits,word) ((((word) << (bits)) & 0xFFFFFFFF) | ((word) >> (32-(bits)))) -void SHA1Reset(SHA1Context *context) +static void SHA1Reset(SHA1Context *context) { context->Length_Low = 0; context->Length_High = 0; @@ -424,7 +425,7 @@ static void SHA1PadMessage(SHA1Context *context) SHA1ProcessMessageBlock(context); } -int SHA1Result(SHA1Context *context) +static int SHA1Result(SHA1Context *context) { if (context->Corrupted) return 0; @@ -438,7 +439,7 @@ int SHA1Result(SHA1Context *context) return 1; } -void SHA1Input( SHA1Context *context, +static void SHA1Input( SHA1Context *context, const unsigned char *message_array, unsigned length) { @@ -474,3 +475,42 @@ void SHA1Input( SHA1Context *context, message_array++; } } + +int sha1_calculate(const char *path, char *result) +{ + unsigned char buff[4096]; + SHA1Context sha; + int rv = 1; + int fd = open(path, O_RDONLY); + + if (fd < 0) + goto error; + + SHA1Reset(&sha); + + do + { + rv = read(fd, buff, 4096); + if (rv < 0) + goto error; + + SHA1Input(&sha, buff, rv); + }while(rv); + + if (!SHA1Result(&sha)) + goto error; + + sprintf(result, "%08X%08X%08X%08X%08X", + sha.Message_Digest[0], + sha.Message_Digest[1], + sha.Message_Digest[2], + sha.Message_Digest[3], sha.Message_Digest[4]); + + close(fd); + return 0; + +error: + if (fd >= 0) + close(fd); + return -1; +} diff --git a/hash.h b/hash.h index 8924b89bb5..51a59ab315 100644 --- a/hash.h +++ b/hash.h @@ -85,9 +85,7 @@ typedef struct SHA1Context int Corrupted; /* Is the message digest corruped? */ } SHA1Context; -void SHA1Reset(SHA1Context *); -int SHA1Result(SHA1Context *); -void SHA1Input( SHA1Context *, const unsigned char *, unsigned); +int sha1_calculate(const char *path, char *result); #endif