Browse Source

Dodano metodę obliczającą hash plików.

legacy
Marek 10 years ago
parent
commit
ac21b987c8
  1. 48
      md5.cpp
  2. 10
      md5.h

48
md5.cpp

@ -158,12 +158,15 @@ void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size){
unsigned long used, free; unsigned long used, free;
saved_lo = ctx->lo; 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; ctx->hi += size >> 29;
used = saved_lo & 0x3f; used = saved_lo & 0x3f;
if (used){ if (used){
free = 64 - used; free = 64 - used;
if (size < free) { if (size < free) {
memcpy(&ctx->buffer[used], data, size); memcpy(&ctx->buffer[used], data, size);
return; return;
@ -237,6 +240,14 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx){
using namespace std; 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) { static char hb2hex(unsigned char hb) {
hb = hb & 0xF; hb = hb & 0xF;
return hb < 10 ? '0' + hb : hb - 10 + 'a'; return hb < 10 ? '0' + hb : hb - 10 + 'a';
@ -244,20 +255,29 @@ static char hb2hex(unsigned char hb) {
string md5file(const char* filename){ string md5file(const char* filename){
std::FILE* file = std::fopen(filename, "rb"); std::FILE* file = std::fopen(filename, "rb");
string res = md5file(file);
std::fclose(file);
return res;
}
std::fseek(file, 0L, SEEK_END); string md5file(std::FILE* file){
long int len = std::ftell(file);
std::fseek(file, 0L, SEEK_SET);
string res; MD5_CTX c;
MD5_Init(&c);
char buff[BUFSIZ];
unsigned char out[16]; unsigned char out[16];
md5bin(dat, len, out); 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;
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] >> 4));
res.push_back(hb2hex(out[i])); res.push_back(hb2hex(out[i]));
} }
std::fclose(file);
return res; return res;
} }
@ -272,13 +292,11 @@ string md5sum(const void* dat, size_t len) {
return res; return res;
} }
void md5bin(const void* dat, size_t len, unsigned char out[16]) { std::string md5sum(std::string dat){
MD5_CTX c; return md5sum(dat.c_str(), dat.length());
MD5_Init(&c);
MD5_Update(&c, dat, len);
MD5_Final(out, &c);
} }
/* 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){ string md5sum6(const void* dat, size_t len){
static const char* tbl = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; static const char* tbl = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string res; string res;
@ -289,3 +307,7 @@ string md5sum6(const void* dat, size_t len) {
} }
return res; return res;
} }
std::string md5sum6(std::string dat){
return md5sum6(dat.c_str(), dat.length() );
}

10
md5.h

@ -3,13 +3,11 @@
#include <string> #include <string>
/* Same as 'md5sum' command, returned size is always 32 */ std::string md5sum(std::string dat);
std::string md5sum(const void* dat, size_t len); std::string md5sum(const void* dat, size_t len);
std::string md5file(const char* filename);
/* Return Calculated raw result(always little-endian), the size is always 16 */ std::string md5file(std::FILE* file);
void md5bin(const void* dat, size_t len, unsigned char out[16]); std::string md5sum6(std::string dat);
/* Generate shorter md5sum by something like base62 instead of base16 or base10. 0~61 are represented by 0-9a-zA-Z */
std::string md5sum6(const void* dat, size_t len); std::string md5sum6(const void* dat, size_t len);
#endif // end of MD5_H #endif // end of MD5_H
Loading…
Cancel
Save