From 478bf5761265dfea3f7e6af2f30b4e4c599a5602 Mon Sep 17 00:00:00 2001 From: Marek Date: Mon, 10 Nov 2014 01:46:45 +0100 Subject: [PATCH] First commit --- md5.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/md5.cpp b/md5.cpp index ac682ec..7539af6 100644 --- a/md5.cpp +++ b/md5.cpp @@ -158,15 +158,12 @@ 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; @@ -245,6 +242,25 @@ static char hb2hex(unsigned char hb) { return hb < 10 ? '0' + hb : hb - 10 + 'a'; } +string md5file(const char* filename){ + std::FILE* file = std::fopen(filename, "rb"); + + std::fseek(file, 0L, SEEK_END); + long int len = std::ftell(file); + std::fseek(file, 0L, SEEK_SET); + + string res; + unsigned char out[16]; + md5bin(dat, len, out); + 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; +} + string md5sum(const void* dat, size_t len) { string res; unsigned char out[16];