From ac21b987c8ec12e4a00318b7945f9a3f72baf50b Mon Sep 17 00:00:00 2001 From: Marek Date: Mon, 10 Nov 2014 14:32:56 +0100 Subject: [PATCH] =?UTF-8?q?Dodano=20metod=C4=99=20obliczaj=C4=85c=C4=85=20?= =?UTF-8?q?hash=20plik=C3=B3w.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- md5.cpp | 52 +++++++++++++++++++++++++++++++++++++--------------- md5.h | 10 ++++------ 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/md5.cpp b/md5.cpp index 7539af6..57bbe37 100644 --- a/md5.cpp +++ b/md5.cpp @@ -158,12 +158,15 @@ void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size){ unsigned long used, free; saved_lo = ctx->lo; - if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) ctx->hi++; + if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) + ctx->hi++; ctx->hi += size >> 29; + used = saved_lo & 0x3f; if (used){ free = 64 - used; + if (size < free) { memcpy(&ctx->buffer[used], data, size); return; @@ -237,6 +240,14 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx){ using namespace std; +/* Return Calculated raw result(always little-endian), the size is always 16 */ +void md5bin(const void* dat, size_t len, unsigned char out[16]) { + MD5_CTX c; + MD5_Init(&c); + MD5_Update(&c, dat, len); + MD5_Final(out, &c); +} + static char hb2hex(unsigned char hb) { hb = hb & 0xF; return hb < 10 ? '0' + hb : hb - 10 + 'a'; @@ -244,20 +255,29 @@ static char hb2hex(unsigned char hb) { string md5file(const char* filename){ std::FILE* file = std::fopen(filename, "rb"); + string res = md5file(file); + std::fclose(file); + return res; +} + +string md5file(std::FILE* file){ + + MD5_CTX c; + MD5_Init(&c); - std::fseek(file, 0L, SEEK_END); - long int len = std::ftell(file); - std::fseek(file, 0L, SEEK_SET); + char buff[BUFSIZ]; + unsigned char out[16]; + size_t len = 0; + while( ( len = std::fread(buff ,sizeof(char), BUFSIZ, file) ) > 0) { + MD5_Update(&c, buff, len); + } + MD5_Final(out, &c); string res; - unsigned char out[16]; - md5bin(dat, len, out); - for(size_t i = 0; i < 16; ++ i) { + for(size_t i = 0; i < 16; ++ i) { res.push_back(hb2hex(out[i] >> 4)); res.push_back(hb2hex(out[i])); } - - std::fclose(file); return res; } @@ -272,14 +292,12 @@ string md5sum(const void* dat, size_t len) { return res; } -void md5bin(const void* dat, size_t len, unsigned char out[16]) { - MD5_CTX c; - MD5_Init(&c); - MD5_Update(&c, dat, len); - MD5_Final(out, &c); +std::string md5sum(std::string dat){ + return md5sum(dat.c_str(), dat.length()); } -string md5sum6(const void* dat, size_t len) { +/* Generate shorter md5sum by something like base62 instead of base16 or base10. 0~61 are represented by 0-9a-zA-Z */ +string md5sum6(const void* dat, size_t len){ static const char* tbl = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; string res; unsigned char out[16]; @@ -288,4 +306,8 @@ string md5sum6(const void* dat, size_t len) { res.push_back(tbl[out[i] % 62]); } return res; +} + +std::string md5sum6(std::string dat){ + return md5sum6(dat.c_str(), dat.length() ); } \ No newline at end of file diff --git a/md5.h b/md5.h index 8b6eeed..6f88110 100644 --- a/md5.h +++ b/md5.h @@ -3,13 +3,11 @@ #include -/* Same as 'md5sum' command, returned size is always 32 */ +std::string md5sum(std::string dat); std::string md5sum(const void* dat, size_t len); - -/* Return Calculated raw result(always little-endian), the size is always 16 */ -void md5bin(const void* dat, size_t len, unsigned char out[16]); - -/* Generate shorter md5sum by something like base62 instead of base16 or base10. 0~61 are represented by 0-9a-zA-Z */ +std::string md5file(const char* filename); +std::string md5file(std::FILE* file); +std::string md5sum6(std::string dat); std::string md5sum6(const void* dat, size_t len); #endif // end of MD5_H \ No newline at end of file