递归遍历文件夹,对比文件md5
首先,需要引用 md5 的相关代码,参考这篇文章,防止链接内容被删除,这里再记录一次:
md5.h
#ifndef md5_h #define md5_h #include <string> #include <fstream> /* type define */ typedef unsigned char byte; typedef unsigned int uint32; using std::string; using std::ifstream; /* md5 declaration. */ class md5 { public: md5(); md5(const void* input, size_t length); md5(const string& str); md5(ifstream& in); void update(const void* input, size_t length); void update(const string& str); void update(ifstream& in); const byte* digest(); string tostring(); void reset(); private: void update(const byte* input, size_t length); void final(); void transform(const byte block[64]); void encode(const uint32* input, byte* output, size_t length); void decode(const byte* input, uint32* output, size_t length); string bytestohexstring(const byte* input, size_t length); /* class uncopyable */ md5(const md5&); md5& operator=(const md5&); private: uint32 _state[4]; /* state (abcd) */ uint32 _count[2]; /* number of bits, modulo 2^64 (low-order word first) */ byte _buffer[64]; /* input buffer */ byte _digest[16]; /* message digest */ bool _finished; /* calculate finished ? */ static const byte padding[64]; /* padding for calculate */ static const char hex[16]; enum { buffer_size = 1024 }; }; #endif /*md5_h*/
md5.cpp
#include "md5.h" using namespace std; /* constants for md5transform routine. */ #define s11 7 #define s12 12 #define s13 17 #define s14 22 #define s21 5 #define s22 9 #define s23 14 #define s24 20 #define s31 4 #define s32 11 #define s33 16 #define s34 23 #define s41 6 #define s42 10 #define s43 15 #define s44 21 /* f, g, h and i are basic md5 functions. */ #define f(x, y, z) (((x) & (y)) | ((~x) & (z))) #define g(x, y, z) (((x) & (z)) | ((y) & (~z))) #define h(x, y, z) ((x) ^ (y) ^ (z)) #define i(x, y, z) ((y) ^ ((x) | (~z))) /* rotate_left rotates x left n bits. */ #define rotate_left(x, n) (((x) << (n)) | ((x) >> (32-(n)))) /* ff, gg, hh, and ii transformations for rounds 1, 2, 3, and 4. rotation is separate from addition to prevent recomputation. */ #define ff(a, b, c, d, x, s, ac) { (a) += f ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } #define gg(a, b, c, d, x, s, ac) { (a) += g ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } #define hh(a, b, c, d, x, s, ac) { (a) += h ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } #define ii(a, b, c, d, x, s, ac) { (a) += i ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } const byte md5::padding[64] = { 0x80 }; const char md5::hex[16] = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ }; /* default construct. */ md5::md5() { reset(); } /* construct a md5 object with a input buffer. */ md5::md5(const void* input, size_t length) { reset(); update(input, length); } /* construct a md5 object with a string. */ md5::md5(const string& str) { reset(); update(str); } /* construct a md5 object with a file. */ md5::md5(ifstream& in) { reset(); update(in); } /* return the message-digest */ const byte* md5::digest() { if (!_finished) { _finished = true; final(); } return _digest; } /* reset the calculate state */ void md5::reset() { _finished = false; /* reset number of bits. */ _count[0] = _count[1] = 0; /* load magic initialization constants. */ _state[0] = 0x67452301; _state[1] = 0xefcdab89; _state[2] = 0x98badcfe; _state[3] = 0x10325476; } /* updating the context with a input buffer. */ void md5::update(const void* input, size_t length) { update((const byte*)input, length); } /* updating the context with a string. */ void md5::update(const string& str) { update((const byte*)str.c_str(), str.length()); } /* updating the context with a file. */ void md5::update(ifstream& in) { if (!in) { return; } std::streamsize length; char buffer[buffer_size]; while (!in.eof()) { in.read(buffer, buffer_size); length = in.gcount(); if (length > 0) { update(buffer, length); } } in.close(); } /* md5 block update operation. continues an md5 message-digest operation, processing another message block, and updating the context. */ void md5::update(const byte* input, size_t length) { uint32 i, index, partlen; _finished = false; /* compute number of bytes mod 64 */ index = (uint32)((_count[0] >> 3) & 0x3f); /* update number of bits */ if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) { ++_count[1]; } _count[1] += ((uint32)length >> 29); partlen = 64 - index; /* transform as many times as possible. */ if (length >= partlen) { memcpy(&_buffer[index], input, partlen); transform(_buffer); for (i = partlen; i + 63 < length; i += 64) { transform(&input[i]); } index = 0; } else { i = 0; } /* buffer remaining input */ memcpy(&_buffer[index], &input[i], length - i); } /* md5 finalization. ends an md5 message-_digest operation, writing the the message _digest and zeroizing the context. */ void md5::final() { byte bits[8]; uint32 oldstate[4]; uint32 oldcount[2]; uint32 index, padlen; /* save current state and count. */ memcpy(oldstate, _state, 16); memcpy(oldcount, _count, 8); /* save number of bits */ encode(_count, bits, 8); /* pad out to 56 mod 64. */ index = (uint32)((_count[0] >> 3) & 0x3f); padlen = (index < 56) ? (56 - index) : (120 - index); update(padding, padlen); /* append length (before padding) */ update(bits, 8); /* store state in digest */ encode(_state, _digest, 16); /* restore current state and count. */ memcpy(_state, oldstate, 16); memcpy(_count, oldcount, 8); } /* md5 basic transformation. transforms _state based on block. */ void md5::transform(const byte block[64]) { uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16]; decode(block, x, 64); /* round 1 */ ff (a, b, c, d, x[ 0], s11, 0xd76aa478); /* 1 */ ff (d, a, b, c, x[ 1], s12, 0xe8c7b756); /* 2 */ ff (c, d, a, b, x[ 2], s13, 0x242070db); /* 3 */ ff (b, c, d, a, x[ 3], s14, 0xc1bdceee); /* 4 */ ff (a, b, c, d, x[ 4], s11, 0xf57c0faf); /* 5 */ ff (d, a, b, c, x[ 5], s12, 0x4787c62a); /* 6 */ ff (c, d, a, b, x[ 6], s13, 0xa8304613); /* 7 */ ff (b, c, d, a, x[ 7], s14, 0xfd469501); /* 8 */ ff (a, b, c, d, x[ 8], s11, 0x698098d8); /* 9 */ ff (d, a, b, c, x[ 9], s12, 0x8b44f7af); /* 10 */ ff (c, d, a, b, x[10], s13, 0xffff5bb1); /* 11 */ ff (b, c, d, a, x[11], s14, 0x895cd7be); /* 12 */ ff (a, b, c, d, x[12], s11, 0x6b901122); /* 13 */ ff (d, a, b, c, x[13], s12, 0xfd987193); /* 14 */ ff (c, d, a, b, x[14], s13, 0xa679438e); /* 15 */ ff (b, c, d, a, x[15], s14, 0x49b40821); /* 16 */ /* round 2 */ gg (a, b, c, d, x[ 1], s21, 0xf61e2562); /* 17 */ gg (d, a, b, c, x[ 6], s22, 0xc040b340); /* 18 */ gg (c, d, a, b, x[11], s23, 0x265e5a51); /* 19 */ gg (b, c, d, a, x[ 0], s24, 0xe9b6c7aa); /* 20 */ gg (a, b, c, d, x[ 5], s21, 0xd62f105d); /* 21 */ gg (d, a, b, c, x[10], s22, 0x2441453); /* 22 */ gg (c, d, a, b, x[15], s23, 0xd8a1e681); /* 23 */ gg (b, c, d, a, x[ 4], s24, 0xe7d3fbc8); /* 24 */ gg (a, b, c, d, x[ 9], s21, 0x21e1cde6); /* 25 */ gg (d, a, b, c, x[14], s22, 0xc33707d6); /* 26 */ gg (c, d, a, b, x[ 3], s23, 0xf4d50d87); /* 27 */ gg (b, c, d, a, x[ 8], s24, 0x455a14ed); /* 28 */ gg (a, b, c, d, x[13], s21, 0xa9e3e905); /* 29 */ gg (d, a, b, c, x[ 2], s22, 0xfcefa3f8); /* 30 */ gg (c, d, a, b, x[ 7], s23, 0x676f02d9); /* 31 */ gg (b, c, d, a, x[12], s24, 0x8d2a4c8a); /* 32 */ /* round 3 */ hh (a, b, c, d, x[ 5], s31, 0xfffa3942); /* 33 */ hh (d, a, b, c, x[ 8], s32, 0x8771f681); /* 34 */ hh (c, d, a, b, x[11], s33, 0x6d9d6122); /* 35 */ hh (b, c, d, a, x[14], s34, 0xfde5380c); /* 36 */ hh (a, b, c, d, x[ 1], s31, 0xa4beea44); /* 37 */ hh (d, a, b, c, x[ 4], s32, 0x4bdecfa9); /* 38 */ hh (c, d, a, b, x[ 7], s33, 0xf6bb4b60); /* 39 */ hh (b, c, d, a, x[10], s34, 0xbebfbc70); /* 40 */ hh (a, b, c, d, x[13], s31, 0x289b7ec6); /* 41 */ hh (d, a, b, c, x[ 0], s32, 0xeaa127fa); /* 42 */ hh (c, d, a, b, x[ 3], s33, 0xd4ef3085); /* 43 */ hh (b, c, d, a, x[ 6], s34, 0x4881d05); /* 44 */ hh (a, b, c, d, x[ 9], s31, 0xd9d4d039); /* 45 */ hh (d, a, b, c, x[12], s32, 0xe6db99e5); /* 46 */ hh (c, d, a, b, x[15], s33, 0x1fa27cf8); /* 47 */ hh (b, c, d, a, x[ 2], s34, 0xc4ac5665); /* 48 */ /* round 4 */ ii (a, b, c, d, x[ 0], s41, 0xf4292244); /* 49 */ ii (d, a, b, c, x[ 7], s42, 0x432aff97); /* 50 */ ii (c, d, a, b, x[14], s43, 0xab9423a7); /* 51 */ ii (b, c, d, a, x[ 5], s44, 0xfc93a039); /* 52 */ ii (a, b, c, d, x[12], s41, 0x655b59c3); /* 53 */ ii (d, a, b, c, x[ 3], s42, 0x8f0ccc92); /* 54 */ ii (c, d, a, b, x[10], s43, 0xffeff47d); /* 55 */ ii (b, c, d, a, x[ 1], s44, 0x85845dd1); /* 56 */ ii (a, b, c, d, x[ 8], s41, 0x6fa87e4f); /* 57 */ ii (d, a, b, c, x[15], s42, 0xfe2ce6e0); /* 58 */ ii (c, d, a, b, x[ 6], s43, 0xa3014314); /* 59 */ ii (b, c, d, a, x[13], s44, 0x4e0811a1); /* 60 */ ii (a, b, c, d, x[ 4], s41, 0xf7537e82); /* 61 */ ii (d, a, b, c, x[11], s42, 0xbd3af235); /* 62 */ ii (c, d, a, b, x[ 2], s43, 0x2ad7d2bb); /* 63 */ ii (b, c, d, a, x[ 9], s44, 0xeb86d391); /* 64 */ _state[0] += a; _state[1] += b; _state[2] += c; _state[3] += d; } /* encodes input (ulong) into output (byte). assumes length is a multiple of 4. */ void md5::encode(const uint32* input, byte* output, size_t length) { for (size_t i = 0, j = 0; j < length; ++i, j += 4) { output[j]= (byte)(input[i] & 0xff); output[j + 1] = (byte)((input[i] >> 8) & 0xff); output[j + 2] = (byte)((input[i] >> 16) & 0xff); output[j + 3] = (byte)((input[i] >> 24) & 0xff); } } /* decodes input (byte) into output (ulong). assumes length is a multiple of 4. */ void md5::decode(const byte* input, uint32* output, size_t length) { for (size_t i = 0, j = 0; j < length; ++i, j += 4) { output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) | (((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24); } } /* convert byte array to hex string. */ string md5::bytestohexstring(const byte* input, size_t length) { string str; str.reserve(length << 1); for (size_t i = 0; i < length; ++i) { int t = input[i]; int a = t / 16; int b = t % 16; str.append(1, hex[a]); str.append(1, hex[b]); } return str; } /* convert digest to string value */ string md5::tostring() { return bytestohexstring(digest(), 16); }
调用例子:
#include "md5.h" #include <iostream> using namespace std; void printmd5(const string& str, md5& md5) { cout << "md5("" << str << "") = " << md5.tostring() << endl; } int main() { md5 md5; md5.update(""); printmd5("", md5); md5.update("a"); printmd5("a", md5); md5.update("bc"); printmd5("abc", md5); md5.update("defghijklmnopqrstuvwxyz"); printmd5("abcdefghijklmnopqrstuvwxyz", md5); md5.reset(); md5.update("message digest"); printmd5("message digest", md5); md5.reset(); md5.update(ifstream("d:\test.txt")); printmd5("d:\test.txt", md5); return 0; }
配置好了以后开始写我们的递归遍历函数:
/* 遍历目录下所有文件,对比 md5 path:文件夹路径(末尾不要有‘‘) format:要筛选的文件后缀名 str_md5:md5 字符串 isfound:是否匹配到与 str_md5 相等的 md5 值 */ void findallfile_md5(const char * path,const char * format,string str_md5,bool &isfound) { // 路径末尾追加 ‘*.*‘ char newpath[200]; strcpy(newpath, path); strcat(newpath, "\*.*"); // 找到目录下的第一个文件 #include <io.h> _finddata_t finddata; /* 文件信息结构体 struct _finddata_t{ unsigned attrib; // 文件属性 time_t time_create; // 创建时的时间戳 time_t time_access; // 最后一次被访问时的时间戳 time_t time_write; // 最后一次被修改时的时间戳 _fsize_t size; // 文件字节大小 char name[_max_fname]; // 文件名 }; */ long handle = _findfirst(newpath, &finddata); if (handle == -1){return;} // 遍历文件和文件夹 while (_findnext(handle, &finddata) == 0){ // 文件夹 if (finddata.attrib & _a_subdir){ // 文件夹名不能有敏感字符 ‘.‘、‘..‘ if (strcmp(finddata.name, ".") == 0 || strcmp(finddata.name, "..") == 0){continue;} // 进入这个文件夹继续遍历 strcpy(newpath, path); strcat(newpath, "\"); strcat(newpath, finddata.name); findallfile_md5(newpath,format,str_md5,isfound); } // 文件 else{ // 判断是不是指定后缀的文件 if(strstr( finddata.name,format)){ // 输出(用来测试) //cout << "finddata.size = " << finddata.size << endl; //cout << "finddata.name = " << finddata.name << endl; //cout << "path = " << path << endl; // 取文件全路径 string str_fullpath = path; str_fullpath+="\"; str_fullpath+=finddata.name; // 取文件 md5,判断是否匹配特征 md5 md5; md5.reset(); md5.update(ifstream(str_fullpath)); if(md5.tostring() == str_md5){isfound = true;} } } } // 关闭搜索句柄 _findclose(handle); }
调用例子(遍历%temp% 下的文件)
// 获取 %temp% 目录 tchar lptemppathbuffer[max_path]; gettemppath(max_path,lptemppathbuffer); // 删除末尾 ‘‘ string str_temppath = lptemppathbuffer; str_temppath = str_temppath.substr(0,str_temppath.length()-1); // 遍历目录下所有 exe 文件,匹配 md5 bool isfound = false; findallfile_md5(stringtocharp(str_temppath),".exe","52f5ce92c6f72c7e193b560bf4e76330",isfound); if(isfound){cout << "找到了!" << endl;;}
知识点扩展:
c++计算md5
#include "md5.h" using namespace std; /* constants for md5transform routine. */ #define s11 7 #define s12 12 #define s13 17 #define s14 22 #define s21 5 #define s22 9 #define s23 14 #define s24 20 #define s31 4 #define s32 11 #define s33 16 #define s34 23 #define s41 6 #define s42 10 #define s43 15 #define s44 21 /* f, g, h and i are basic md5 functions. */ #define f(x, y, z) (((x) & (y)) | ((~x) & (z))) #define g(x, y, z) (((x) & (z)) | ((y) & (~z))) #define h(x, y, z) ((x) ^ (y) ^ (z)) #define i(x, y, z) ((y) ^ ((x) | (~z))) /* rotate_left rotates x left n bits. */ #define rotate_left(x, n) (((x) << (n)) | ((x) >> (32-(n)))) /* ff, gg, hh, and ii transformations for rounds 1, 2, 3, and 4. rotation is separate from addition to prevent recomputation. */ #define ff(a, b, c, d, x, s, ac) { (a) += f ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } #define gg(a, b, c, d, x, s, ac) { (a) += g ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } #define hh(a, b, c, d, x, s, ac) { (a) += h ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } #define ii(a, b, c, d, x, s, ac) { (a) += i ((b), (c), (d)) + (x) + ac; (a) = rotate_left ((a), (s)); (a) += (b); } const byte md5::padding[64] = { 0x80 }; const char md5::hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /* default construct. */ md5::md5() { reset(); } /* construct a md5 object with a input buffer. */ md5::md5(const void* input, size_t length) { reset(); update(input, length); } /* construct a md5 object with a string. */ md5::md5(const string& str) { reset(); update(str); } /* construct a md5 object with a file. */ md5::md5(ifstream& in) { reset(); update(in); } /* return the message-digest */ const byte* md5::digest() { if (!_finished) { _finished = true; final(); } return _digest; } /* reset the calculate state */ void md5::reset() { _finished = false; /* reset number of bits. */ _count[0] = _count[1] = 0; /* load magic initialization constants. */ _state[0] = 0x67452301; _state[1] = 0xefcdab89; _state[2] = 0x98badcfe; _state[3] = 0x10325476; } /* updating the context with a input buffer. */ void md5::update(const void* input, size_t length) { update((const byte*)input, length); } /* updating the context with a string. */ void md5::update(const string& str) { update((const byte*)str.c_str(), str.length()); } /* updating the context with a file. */ void md5::update(ifstream& in) { if (!in) { return; } std::streamsize length; char buffer[buffer_size]; while (!in.eof()) { in.read(buffer, buffer_size); length = in.gcount(); if (length > 0) { update(buffer, length); } } in.close(); } /* md5 block update operation. continues an md5 message-digest operation, processing another message block, and updating the context. */ void md5::update(const byte* input, size_t length) { uint32 i, index, partlen; _finished = false; /* compute number of bytes mod 64 */ index = (uint32)((_count[0] >> 3) & 0x3f); /* update number of bits */ if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) { ++_count[1]; } _count[1] += ((uint32)length >> 29); partlen = 64 - index; /* transform as many times as possible. */ if (length >= partlen) { memcpy(&_buffer[index], input, partlen); transform(_buffer); for (i = partlen; i + 63 < length; i += 64) { transform(&input[i]); } index = 0; } else { i = 0; } /* buffer remaining input */ memcpy(&_buffer[index], &input[i], length - i); } /* md5 finalization. ends an md5 message-_digest operation, writing the the message _digest and zeroizing the context. */ void md5::final() { byte bits[8]; uint32 oldstate[4]; uint32 oldcount[2]; uint32 index, padlen; /* save current state and count. */ memcpy(oldstate, _state, 16); memcpy(oldcount, _count, 8); /* save number of bits */ encode(_count, bits, 8); /* pad out to 56 mod 64. */ index = (uint32)((_count[0] >> 3) & 0x3f); padlen = (index < 56) ? (56 - index) : (120 - index); update(padding, padlen); /* append length (before padding) */ update(bits, 8); /* store state in digest */ encode(_state, _digest, 16); /* restore current state and count. */ memcpy(_state, oldstate, 16); memcpy(_count, oldcount, 8); } /* md5 basic transformation. transforms _state based on block. */ void md5::transform(const byte block[64]) { uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16]; decode(block, x, 64); /* round 1 */ ff(a, b, c, d, x[0], s11, 0xd76aa478); /* 1 */ ff(d, a, b, c, x[1], s12, 0xe8c7b756); /* 2 */ ff(c, d, a, b, x[2], s13, 0x242070db); /* 3 */ ff(b, c, d, a, x[3], s14, 0xc1bdceee); /* 4 */ ff(a, b, c, d, x[4], s11, 0xf57c0faf); /* 5 */ ff(d, a, b, c, x[5], s12, 0x4787c62a); /* 6 */ ff(c, d, a, b, x[6], s13, 0xa8304613); /* 7 */ ff(b, c, d, a, x[7], s14, 0xfd469501); /* 8 */ ff(a, b, c, d, x[8], s11, 0x698098d8); /* 9 */ ff(d, a, b, c, x[9], s12, 0x8b44f7af); /* 10 */ ff(c, d, a, b, x[10], s13, 0xffff5bb1); /* 11 */ ff(b, c, d, a, x[11], s14, 0x895cd7be); /* 12 */ ff(a, b, c, d, x[12], s11, 0x6b901122); /* 13 */ ff(d, a, b, c, x[13], s12, 0xfd987193); /* 14 */ ff(c, d, a, b, x[14], s13, 0xa679438e); /* 15 */ ff(b, c, d, a, x[15], s14, 0x49b40821); /* 16 */ /* round 2 */ gg(a, b, c, d, x[1], s21, 0xf61e2562); /* 17 */ gg(d, a, b, c, x[6], s22, 0xc040b340); /* 18 */ gg(c, d, a, b, x[11], s23, 0x265e5a51); /* 19 */ gg(b, c, d, a, x[0], s24, 0xe9b6c7aa); /* 20 */ gg(a, b, c, d, x[5], s21, 0xd62f105d); /* 21 */ gg(d, a, b, c, x[10], s22, 0x2441453); /* 22 */ gg(c, d, a, b, x[15], s23, 0xd8a1e681); /* 23 */ gg(b, c, d, a, x[4], s24, 0xe7d3fbc8); /* 24 */ gg(a, b, c, d, x[9], s21, 0x21e1cde6); /* 25 */ gg(d, a, b, c, x[14], s22, 0xc33707d6); /* 26 */ gg(c, d, a, b, x[3], s23, 0xf4d50d87); /* 27 */ gg(b, c, d, a, x[8], s24, 0x455a14ed); /* 28 */ gg(a, b, c, d, x[13], s21, 0xa9e3e905); /* 29 */ gg(d, a, b, c, x[2], s22, 0xfcefa3f8); /* 30 */ gg(c, d, a, b, x[7], s23, 0x676f02d9); /* 31 */ gg(b, c, d, a, x[12], s24, 0x8d2a4c8a); /* 32 */ /* round 3 */ hh(a, b, c, d, x[5], s31, 0xfffa3942); /* 33 */ hh(d, a, b, c, x[8], s32, 0x8771f681); /* 34 */ hh(c, d, a, b, x[11], s33, 0x6d9d6122); /* 35 */ hh(b, c, d, a, x[14], s34, 0xfde5380c); /* 36 */ hh(a, b, c, d, x[1], s31, 0xa4beea44); /* 37 */ hh(d, a, b, c, x[4], s32, 0x4bdecfa9); /* 38 */ hh(c, d, a, b, x[7], s33, 0xf6bb4b60); /* 39 */ hh(b, c, d, a, x[10], s34, 0xbebfbc70); /* 40 */ hh(a, b, c, d, x[13], s31, 0x289b7ec6); /* 41 */ hh(d, a, b, c, x[0], s32, 0xeaa127fa); /* 42 */ hh(c, d, a, b, x[3], s33, 0xd4ef3085); /* 43 */ hh(b, c, d, a, x[6], s34, 0x4881d05); /* 44 */ hh(a, b, c, d, x[9], s31, 0xd9d4d039); /* 45 */ hh(d, a, b, c, x[12], s32, 0xe6db99e5); /* 46 */ hh(c, d, a, b, x[15], s33, 0x1fa27cf8); /* 47 */ hh(b, c, d, a, x[2], s34, 0xc4ac5665); /* 48 */ /* round 4 */ ii(a, b, c, d, x[0], s41, 0xf4292244); /* 49 */ ii(d, a, b, c, x[7], s42, 0x432aff97); /* 50 */ ii(c, d, a, b, x[14], s43, 0xab9423a7); /* 51 */ ii(b, c, d, a, x[5], s44, 0xfc93a039); /* 52 */ ii(a, b, c, d, x[12], s41, 0x655b59c3); /* 53 */ ii(d, a, b, c, x[3], s42, 0x8f0ccc92); /* 54 */ ii(c, d, a, b, x[10], s43, 0xffeff47d); /* 55 */ ii(b, c, d, a, x[1], s44, 0x85845dd1); /* 56 */ ii(a, b, c, d, x[8], s41, 0x6fa87e4f); /* 57 */ ii(d, a, b, c, x[15], s42, 0xfe2ce6e0); /* 58 */ ii(c, d, a, b, x[6], s43, 0xa3014314); /* 59 */ ii(b, c, d, a, x[13], s44, 0x4e0811a1); /* 60 */ ii(a, b, c, d, x[4], s41, 0xf7537e82); /* 61 */ ii(d, a, b, c, x[11], s42, 0xbd3af235); /* 62 */ ii(c, d, a, b, x[2], s43, 0x2ad7d2bb); /* 63 */ ii(b, c, d, a, x[9], s44, 0xeb86d391); /* 64 */ _state[0] += a; _state[1] += b; _state[2] += c; _state[3] += d; } /* encodes input (ulong) into output (byte). assumes length is a multiple of 4. */ void md5::encode(const uint32* input, byte* output, size_t length) { for (size_t i = 0, j = 0; j < length; ++i, j += 4) { output[j] = (byte)(input[i] & 0xff); output[j + 1] = (byte)((input[i] >> 8) & 0xff); output[j + 2] = (byte)((input[i] >> 16) & 0xff); output[j + 3] = (byte)((input[i] >> 24) & 0xff); } } /* decodes input (byte) into output (ulong). assumes length is a multiple of 4. */ void md5::decode(const byte* input, uint32* output, size_t length) { for (size_t i = 0, j = 0; j < length; ++i, j += 4) { output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) | (((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24); } } /* convert byte array to hex string. */ string md5::bytestohexstring(const byte* input, size_t length) { string str; str.reserve(length << 1); for (size_t i = 0; i < length; ++i) { int t = input[i]; int a = t / 16; int b = t % 16; str.append(1, hex[a]); str.append(1, hex[b]); } return str; } /* convert digest to string value */ string md5::tostring() { return bytestohexstring(digest(), 16); }
到此这篇关于c++ 递归遍历文件并计算md5的实例代码的文章就介绍到这了,更多相关c++ 递归遍历文件并计算md5内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!
需要了解更多c/c++开发分享C++ 递归遍历文件并计算MD5的实例代码,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/672439.html