导航:
论坛 -> 发布代码
斑竹:liumazi,ruralboy
作者:
2024/3/1 19:14:16
标题:
加入我的收藏
楼主:
以前曾收集过一个用内嵌汇编的计算MD5的代码,速度比较快,今天重温了一下,发现现在的编绎器进步了,不用内嵌汇编,用C代码速度跟用汇编的差不多,所以又修整了一下,发出来供大家使用。 //========== #include <vcl.h> /*这段汇编码对于现代编绎器的C代码已无性能优势 #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 #define a esi #define b edi #define c edx #define d ebx #define tmp1 eax #define tmp2 ecx #define x(i) [x+4*i] #define FF(a, b, c, d, x, s, ac) \ __asm mov tmp1,b \ __asm and tmp1,c \ __asm mov tmp2,b \ __asm not tmp2 \ __asm and tmp2,d \ __asm or tmp2,tmp1 \ __asm lea a,[tmp2+a+ac] \ __asm add a,x \ __asm rol a,s \ __asm add a,b #define GG(a, b, c, d, x, s, ac) \ __asm mov tmp1,b \ __asm and tmp1,d \ __asm mov tmp2,d \ __asm not tmp2 \ __asm and tmp2,c \ __asm or tmp2,tmp1 \ __asm lea a,[tmp2+a+ac] \ __asm add a,x \ __asm rol a,s \ __asm add a,b #define HH(a,b,c, d, x, s, ac) \ __asm mov tmp2,b \ __asm xor tmp2,c \ __asm xor tmp2,d \ __asm lea a,[tmp2+a+ac] \ __asm add a,x \ __asm rol a,s \ __asm add a,b #define II(a, b, c, d, x, s, ac) \ __asm mov tmp2,d \ __asm not tmp2 \ __asm or tmp2,b \ __asm xor tmp2,c \ __asm lea a,[tmp2+a+ac] \ __asm add a,x \ __asm rol a,s \ __asm add a,b static void MD5Transform (unsigned int *state, unsigned char *block) { DWORD x[16]; __asm { mov tmp1, DWORD PTR [state] mov a, DWORD PTR [tmp1] mov b, DWORD PTR [tmp1+4] mov c, DWORD PTR [tmp1+8] mov d, DWORD PTR [tmp1+12] push esi push edi xor ecx, ecx mov esi, DWORD PTR [block] lea edi, [x] ROLL: mov eax, DWORD PTR [esi+ecx] mov DWORD PTR [edi+ecx], eax add ecx, 4 cmp ecx, 64 jb ROLL pop edi pop esi } // Round 1 FF (a, b, c, d, x(0), S11, 0xd76aa478); FF (d, a, b, c, x(1), S12, 0xe8c7b756); FF (c, d, a, b, x(2), S13, 0x242070db); FF (b, c, d, a, x(3), S14, 0xc1bdceee); FF (a, b, c, d, x(4), S11, 0xf57c0faf); FF (d, a, b, c, x(5), S12, 0x4787c62a); FF (c, d, a, b, x(6), S13, 0xa8304613); FF (b, c, d, a, x(7), S14, 0xfd469501); FF (a, b, c, d, x(8), S11, 0x698098d8); FF (d, a, b, c, x(9), S12, 0x8b44f7af); FF (c, d, a, b, x(10), S13, 0xffff5bb1); FF (b, c, d, a, x(11), S14, 0x895cd7be); FF (a, b, c, d, x(12), S11, 0x6b901122); FF (d, a, b, c, x(13), S12, 0xfd987193); FF (c, d, a, b, x(14), S13, 0xa679438e); FF (b, c, d, a, x(15), S14, 0x49b40821); // Round 2 GG (a, b, c, d, x(1), S21, 0xf61e2562); GG (d, a, b, c, x(6), S22, 0xc040b340); GG (c, d, a, b, x(11), S23, 0x265e5a51); GG (b, c, d, a, x(0), S24, 0xe9b6c7aa); GG (a, b, c, d, x(5), S21, 0xd62f105d); GG (d, a, b, c, x(10), S22, 0x2441453); GG (c, d, a, b, x(15), S23, 0xd8a1e681); GG (b, c, d, a, x(4), S24, 0xe7d3fbc8); GG (a, b, c, d, x(9), S21, 0x21e1cde6); GG (d, a, b, c, x(14), S22, 0xc33707d6); GG (c, d, a, b, x(3), S23, 0xf4d50d87); GG (b, c, d, a, x(8), S24, 0x455a14ed); GG (a, b, c, d, x(13), S21, 0xa9e3e905); GG (d, a, b, c, x(2), S22, 0xfcefa3f8); GG (c, d, a, b, x(7), S23, 0x676f02d9); GG (b, c, d, a, x(12), S24, 0x8d2a4c8a); // Round 3 HH (a, b, c, d, x(5), S31, 0xfffa3942); HH (d, a, b, c, x(8), S32, 0x8771f681); HH (c, d, a, b, x(11), S33, 0x6d9d6122); HH (b, c, d, a, x(14), S34, 0xfde5380c); HH (a, b, c, d, x(1), S31, 0xa4beea44); HH (d, a, b, c, x(4), S32, 0x4bdecfa9); HH (c, d, a, b, x(7), S33, 0xf6bb4b60); HH (b, c, d, a, x(10), S34, 0xbebfbc70); HH (a, b, c, d, x(13), S31, 0x289b7ec6); HH (d, a, b, c, x(0), S32, 0xeaa127fa); HH (c, d, a, b, x(3), S33, 0xd4ef3085); HH (b, c, d, a, x(6), S34, 0x4881d05); HH (a, b, c, d, x(9), S31, 0xd9d4d039); HH (d, a, b, c, x(12), S32, 0xe6db99e5); HH (c, d, a, b, x(15), S33, 0x1fa27cf8); HH (b, c, d, a, x(2), S34, 0xc4ac5665); // Round 4 II (a, b, c, d, x(0), S41, 0xf4292244); II (d, a, b, c, x(7), S42, 0x432aff97); II (c, d, a, b, x(14), S43, 0xab9423a7); II (b, c, d, a, x(5), S44, 0xfc93a039); II (a, b, c, d, x(12), S41, 0x655b59c3); II (d, a, b, c, x(3), S42, 0x8f0ccc92); II (c, d, a, b, x(10), S43, 0xffeff47d); II (b, c, d, a, x(1), S44, 0x85845dd1); II (a, b, c, d, x(8), S41, 0x6fa87e4f); II (d, a, b, c, x(15), S42, 0xfe2ce6e0); II (c, d, a, b, x(6), S43, 0xa3014314); II (b, c, d, a, x(13), S44, 0x4e0811a1); II (a, b, c, d, x(4), S41, 0xf7537e82); II (d, a, b, c, x(11), S42, 0xbd3af235); II (c, d, a, b, x(2), S43, 0x2ad7d2bb); II (b, c, d, a, x(9), S44, 0xeb86d391); __asm { mov tmp1, DWORD PTR [state] add DWORD PTR [tmp1], a add DWORD PTR [tmp1+4], b add DWORD PTR [tmp1+8], c add DWORD PTR [tmp1+12], d } }*/ //以下是C++源码,兼容性比上面的汇编码好,性能也一样 #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 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) #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))) #define FF(a, b, c, d, x, s, ac) { \ (a) += F ((b), (c), (d)) + (x) + (unsigned int)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define GG(a, b, c, d, x, s, ac) { \ (a) += G ((b), (c), (d)) + (x) + (unsigned int)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define HH(a, b, c, d, x, s, ac) { \ (a) += H ((b), (c), (d)) + (x) + (unsigned int)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #define II(a, b, c, d, x, s, ac) { \ (a) += I ((b), (c), (d)) + (x) + (unsigned int)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } static void MD5Transform (unsigned int state[], unsigned char block[]){ unsigned int a = state[0], b = state[1], c = state[2], d = state[3]; unsigned int x[16]; for (unsigned int i = 0, j = 0; j < 64; i++, j += 4) x[i] = block[j] | (block[j+1] << 8) | (block[j+2] << 16) | (block[j+3] << 24); // 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; } static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; struct MD5_CTX { unsigned int state[4]; //encypted message context. state[0]是32位的无符号整数,注意内存字节顺序 unsigned long long count; char str[33] = {0}; unsigned char buffer[64]; }; static void MD5Init (MD5_CTX &context){ context.count = 0; context.state[0] = 0x67452301; context.state[1] = 0xefcdab89; context.state[2] = 0x98badcfe; context.state[3] = 0x10325476; } static void MD5Update (MD5_CTX &context, unsigned char *in, unsigned int inLen){ unsigned int i, index, partLen ; index = (context.count >> 3) & 0x3Full ; //此前已读入的数据字节数模除64,得出不足64字节的部分,此部分应在此前存入buffer context.count += (inLen << 3) ; //加本次新增的数据比特位数(字节数*8) partLen = 64 - index; //64字节buffer剩余空间 if (inLen < partLen) i = 0 ; else { CopyMemory (&context.buffer[index], in, partLen) ; //先装满buffer,最后一次Final会确保装满 MD5Transform (context.state, context.buffer) ; //装满后运算之 index = 0 ; //索引复位 for (i = partLen; i + 63 < inLen; i += 64) MD5Transform (context.state, in + i) ; //如果后续字节满64,则继续运算之 } CopyMemory (&context.buffer[index], in + i, inLen - i); //不足64字节的部分读入buffer,待MD5Final时运算 } static void MD5Final (MD5_CTX &context){ unsigned int index, padLen ; unsigned long long bits = context.count ; index = (context.count >> 3) & 0x3Full ; padLen = (index < 56) ? (56 - index) : (120 - index) ; MD5Update (context, PADDING, padLen); //填充一个2进制的1和若干个0使其长度模512与448同余 MD5Update (context, (unsigned char*)&bits, 8); //数据的真实长度以64bit表示附加在填充结果后面 unsigned char* p = (unsigned char*)context.state ; //将数值解码为字符串 for (int i = 0; i < 16; i++) { unsigned char j ; j = p[i] & 0xF ; if (j < 10) context.str[i*2+1] = j + '0' ; else context.str[i*2+1] = j - 10 + 'a' ; //需要大写MD5码将此处的a改为A即可 j = p[i] >> 4 ; if (j < 10) context.str[i*2] = j + '0' ; else context.str[i*2] = j - 10 + 'a' ; } } //以下是用于C++builder的封装,不用可以连同文件首行的 #include <vcl.h>一起删去 UnicodeString MD5_String (const UnicodeString &s){ int ok = 0; MD5_CTX context; try { MD5Init (context); MD5Update (context, (unsigned char*)(s.data()), s.Length()*2); MD5Final (context); ok = 1; } catch(...) { ok = 0; } if (ok == 0) return L"" ; //如果出错则返回空字符串 else return context.str ; } //========== UnicodeString MD5_File(const UnicodeString &filename){ if (!FileExists(filename)) return L"" ; int len, ok = 0; FILE *fp = NULL; MD5_CTX context; unsigned char* buf = NULL; fp = _wfopen (filename.c_str(), L"rb") ; if (NULL == fp) return L"" ; try { try { buf = new unsigned char[40960]; //每次读入文件的字节数,这个数字可以根据需要更改 MD5Init (context); while (0 != (len = fread (buf, 1, 40960, fp))) MD5Update (context, buf, len); MD5Final (context); ok = 1; } catch (...) { ok = 0 ; } } __finally { delete[] buf ; fclose (fp) ; } if (ok == 0) return L"" ; //如果出错则返回空字符串 else return context.str ; } //========== 使用方法,直接将上述内容保存为.cpp文件,加入工程即可使用。
----------------------------------------------
-
作者:
2024/4/4 13:52:01
1楼:
//这是源自 OpenSSH 的版本,更好,可以适应64位平台 #include <string.h> #include <stdio.h> #include <io.h> struct MD5_CTX { unsigned int lo, hi; unsigned int abcd[4]; unsigned char buffer[64]; unsigned int block[16]; unsigned char str[33]; //MD5字符串表达 } ctx; void MD5_Init(MD5_CTX *ctx) { memset(ctx, 0, sizeof(MD5_CTX)); ctx->abcd[0] = 0x67452301; ctx->abcd[1] = 0xefcdab89; ctx->abcd[2] = 0x98badcfe; ctx->abcd[3] = 0x10325476; } /* * The basic MD5 functions. * * F and G are optimized compared to their RFC 1321 definitions for * architectures that lack an AND-NOT instruction, just like in Colin Plumb's * implementation. */ #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) #define H(x, y, z) (((x) ^ (y)) ^ (z)) #define H2(x, y, z) ((x) ^ ((y) ^ (z))) #define I(x, y, z) ((y) ^ ((x) | ~(z))) /* * The MD5 transformation for all four rounds. */ #define STEP(f, a, b, c, d, x, t, s) \ (a) += f((b), (c), (d)) + (x) + (t); \ (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ (a) += (b); /* * SET reads 4 input bytes in little-endian byte order and stores them * in a properly aligned word in host byte order. * * The check for little-endian architectures that tolerate unaligned * memory accesses is just an optimization. Nothing will break if it * doesn't work. */ #if defined(__i386__) || defined(__x86_64__) || defined(__vax__) #define SET(n) \ (*(unsigned int *)&ptr[(n) * 4]) #define GET(n) \ SET(n) #else #define SET(n) \ (ctx->block[(n)] = \ (unsigned int)ptr[(n) * 4] | \ ((unsigned int)ptr[(n) * 4 + 1] << 8) | \ ((unsigned int)ptr[(n) * 4 + 2] << 16) | \ ((unsigned int)ptr[(n) * 4 + 3] << 24)) #define GET(n) \ (ctx->block[(n)]) #endif /* * This processes one or more 64-byte data blocks, but does NOT update * the bit counters. There are no alignment requirements. */ static const void* body(MD5_CTX *ctx, const void *data, unsigned long size) { const unsigned char *ptr; unsigned int a, b, c, d; unsigned int saved_a, saved_b, saved_c, saved_d; ptr = (const unsigned char *)data; a = ctx->abcd[0]; b = ctx->abcd[1]; c = ctx->abcd[2]; d = ctx->abcd[3]; do { saved_a = a; saved_b = b; saved_c = c; saved_d = d; /* Round 1 */ STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) STEP(F, c, d, a, b, SET(2), 0x242070db, 17) STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) /* Round 2 */ STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) STEP(G, d, a, b, c, GET(10), 0x02441453, 9) STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) /* Round 3 */ STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11) STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23) STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11) STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23) STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11) STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23) STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11) STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23) /* Round 4 */ STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) a += saved_a; b += saved_b; c += saved_c; d += saved_d; ptr += 64; } while (size -= 64); ctx->abcd[0] = a; ctx->abcd[1] = b; ctx->abcd[2] = c; ctx->abcd[3] = d; return ptr; } void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size) { unsigned int saved_lo; unsigned long used, available; saved_lo = ctx->lo; if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) ctx->hi++; ctx->hi += size >> 29; used = saved_lo & 0x3f; if (used) { available = 64 - used; if (size < available) { memcpy(&ctx->buffer[used], data, size); return; } memcpy(&ctx->buffer[used], data, available); data = (const unsigned char *)data + available; size -= available; body(ctx, ctx->buffer, 64); } if (size >= 64) { data = body(ctx, data, size & ~(unsigned long)0x3f); size &= 0x3f; } memcpy(ctx->buffer, data, size); } void MD5_Final(MD5_CTX *ctx) { unsigned long used, available; used = ctx->lo & 0x3f; ctx->buffer[used++] = 0x80; available = 64 - used; if (available < 8) { memset(&ctx->buffer[used], 0, available); body(ctx, ctx->buffer, 64); used = 0; available = 64; } memset(&ctx->buffer[used], 0, available - 8); ctx->lo <<= 3; ctx->buffer[56] = ctx->lo; ctx->buffer[57] = ctx->lo >> 8; ctx->buffer[58] = ctx->lo >> 16; ctx->buffer[59] = ctx->lo >> 24; ctx->buffer[60] = ctx->hi; ctx->buffer[61] = ctx->hi >> 8; ctx->buffer[62] = ctx->hi >> 16; ctx->buffer[63] = ctx->hi >> 24; body(ctx, ctx->buffer, 64); unsigned char* p = (unsigned char*)ctx->abcd ; //将数值解码为字符串 for (int i = 0; i < 16; i++) { unsigned char h = p[i] >> 4 ; //无符号右移4位,即除以16 unsigned char l = p[i] & 15 ; //无符号模除16 unsigned char upper = 87 ; //'A'=65,'a'=97,减10即为55:87,大写为55,小写为87 ctx->str[i*2] = h + (h < 10 ? 48 : upper) ; //字符串的高低位呈现与内存数值的高低位相反 ctx->str[i*2+1] = l + (l < 10 ? 48 : upper) ; } } //========== char* MD5_String (unsigned char* in , unsigned long len, int str){ //HASH空字符串为d41d8cd98f00b204e9800998ecf8427e char* bp = (str == 0 ? (char*)ctx.abcd : (char*)ctx.str); //选择返回对象 try { MD5_Init (&ctx); MD5_Update (&ctx, in, len); MD5_Final (&ctx); } catch (...){ MD5_Init (&ctx); } return bp ; } char* MD5_File(const wchar_t* filename, int str){ MD5_Init (&ctx); char* bp = (str == 0 ? (char*)ctx.abcd : (char*)ctx.str); //选择返回对象 if (_waccess(filename , 0)) return bp ; FILE *fp = _wfopen (filename, L"rb") ; if (fp == NULL) return bp ; unsigned int len = 0 ; unsigned char* buf = NULL; try { buf = new unsigned char[65536]; //每次读入文件的字节数,这个数字可以根据需要更改 while (0 != (len = fread (buf, 1, 65536, fp))) MD5_Update (&ctx, buf, len); MD5_Final (&ctx); } catch (...) { MD5_Init (&ctx); } delete[] buf ; fclose (fp) ; return bp ; }
----------------------------------------------
-
作者:
2024/4/4 14:03:31
2楼:
楼主,水平有限,请问如何在delphi里面使用?
----------------------------------------------
阳光总在
作者:
2024/4/7 11:54:57
3楼:
感谢分享,收藏备用
----------------------------------------------
-
作者:
2024/4/7 14:26:56
4楼:
Great!
----------------------------------------------
-
作者:
2024/4/12 0:52:35
5楼:
MD5改良版本: 1、OpenSSH原版的字节计数用两个32位整数,看起来比较复杂,不清晰,这可能是为了适应某些平台不支持64位整数计算。改良为unsigned __int64更直观。 2、OpenSSH的块长度用unsigned long,在这一些平台上不支持64位,改良为 unsigned __int64 3、上次的修改中对used, available 用了 unsigned long,这是没读懂原版代码,实则这两个变量用int 甚至char就够了。 //========== #include <string.h> #include <stdio.h> struct MD5_CTX { unsigned __int64 count; //读取并计算的字节计数 unsigned int abcd[4]; unsigned char buf[64]; unsigned int block[16]; unsigned char str[33]; //MD5字符串表达 } md5ctx; void MD5_Init(MD5_CTX *md5ctx) { memset(md5ctx, 0, sizeof(MD5_CTX)); md5ctx->abcd[0] = 0x67452301; md5ctx->abcd[1] = 0xefcdab89; md5ctx->abcd[2] = 0x98badcfe; md5ctx->abcd[3] = 0x10325476; } /* * The basic MD5 functions. * * F and G are optimized compared to their RFC 1321 definitions for * architectures that lack an AND-NOT instruction, just like in Colin Plumb's * implementation. */ #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) #define H(x, y, z) (((x) ^ (y)) ^ (z)) #define H2(x, y, z) ((x) ^ ((y) ^ (z))) #define I(x, y, z) ((y) ^ ((x) | ~(z))) /* * The MD5 transformation for all four rounds. */ #define STEP(f, a, b, c, d, x, t, s) \ (a) += f((b), (c), (d)) + (x) + (t); \ (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ (a) += (b); /* * SET reads 4 input bytes in little-endian byte order and stores them * in a properly aligned word in host byte order. * * The check for little-endian architectures that tolerate unaligned * memory accesses is just an optimization. Nothing will break if it * doesn't work. */ #if defined(__i386__) || defined(__x86_64__) || defined(__vax__) #define SET(n) \ (*(unsigned int *)&ptr[(n) * 4]) #define GET(n) \ SET(n) #else #define SET(n) \ (md5ctx->block[(n)] = \ (unsigned int)ptr[(n) * 4] | \ ((unsigned int)ptr[(n) * 4 + 1] << 8) | \ ((unsigned int)ptr[(n) * 4 + 2] << 16) | \ ((unsigned int)ptr[(n) * 4 + 3] << 24)) #define GET(n) \ (md5ctx->block[(n)]) #endif /* * This processes one or more 64-byte data blocks, but does NOT update * the bit counters. There are no alignment requirements. */ static const void* body(MD5_CTX *md5ctx, const void *data, unsigned __int64 size) { const unsigned char *ptr; unsigned int a, b, c, d; unsigned int saved_a, saved_b, saved_c, saved_d; ptr = (const unsigned char *)data; a = md5ctx->abcd[0]; b = md5ctx->abcd[1]; c = md5ctx->abcd[2]; d = md5ctx->abcd[3]; do { saved_a = a; saved_b = b; saved_c = c; saved_d = d; /* Round 1 */ STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) STEP(F, c, d, a, b, SET(2), 0x242070db, 17) STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) /* Round 2 */ STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) STEP(G, d, a, b, c, GET(10), 0x02441453, 9) STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) /* Round 3 */ STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11) STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23) STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11) STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23) STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11) STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23) STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11) STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23) /* Round 4 */ STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) a += saved_a; b += saved_b; c += saved_c; d += saved_d; ptr += 64; } while (size -= 64); //块长度必须是64字节的整数倍,否则这里会无限循环,此源码在调用时取整了64字节所以不存在问题 md5ctx->abcd[0] = a; md5ctx->abcd[1] = b; md5ctx->abcd[2] = c; md5ctx->abcd[3] = d; return ptr; } void MD5_Update(MD5_CTX *md5ctx, const void *data, unsigned __int64 size) { int used = md5ctx->count & 63 ; //已计算的字节数模除64得到不足64字节的余数 int remlen = 64 - used ; md5ctx->count += size ; if (used) { // 如果缓存区还有数据没处理完毕 if (size < remlen){ memcpy(&md5ctx->buf[used], data, size); return; } memcpy(&md5ctx->buf[used], data, remlen); data = (const unsigned char *)data + remlen; size -= remlen; body(md5ctx, md5ctx->buf, 64); } if (size >= 64) { data = body(md5ctx, data, size & 0xffffffffffC0ull); size &= 63; } if (size) memcpy(md5ctx->buf, data, size); //如果剩余字节大于0,则读入缓存区 } void MD5_Final(MD5_CTX *md5ctx) { int used = md5ctx->count & 63; //已计算的字节数模除64得到不足64字节的余数 md5ctx->buf[used++] = 0x80; int remlen = 64 - used ; if (remlen < 8) { memset(&md5ctx->buf[used], 0, remlen); body(md5ctx, md5ctx->buf, 64); used = 0; remlen = 64; } memset(&md5ctx->buf[used], 0, remlen - 8); md5ctx->buf[56] = md5ctx->count << 3; md5ctx->buf[57] = md5ctx->count >> 5; md5ctx->buf[58] = md5ctx->count >> 13; md5ctx->buf[59] = md5ctx->count >> 21; md5ctx->buf[60] = md5ctx->count >> 29; md5ctx->buf[61] = md5ctx->count >> 37; md5ctx->buf[62] = md5ctx->count >> 45; md5ctx->buf[63] = md5ctx->count >> 53; body(md5ctx, md5ctx->buf, 64); unsigned char* p = (unsigned char*)md5ctx->abcd ; unsigned char upper = 87 ; // 'A'=65,'a'=97,减10即为55:87,大写为55,小写为87 for (int i = 0; i < 16; i++) { //将数值解码为wchar_t字符串 unsigned char h = p[i] >> 4 ; //无符号右移4位,即除以16 unsigned char l = p[i] & 15 ; //无符号模除16 md5ctx->str[i*2] = h + (h < 10 ? 48 : upper) ; md5ctx->str[i*2+1] = l + (l < 10 ? 48 : upper) ; } } //========== char* MD5_String (unsigned char* in , unsigned __int64 len, bool str){ //HASH空字符串为d41d8cd98f00b204e9800998ecf8427e char* bp = (str ? (char*)md5ctx.str : (char*)md5ctx.abcd); //选择返回对象 MD5_Init (&md5ctx); MD5_Update (&md5ctx, in, len); MD5_Final (&md5ctx); return bp ; } char* MD5_File(const wchar_t* filename, bool str){ char* bp = (str ? (char*)md5ctx.str : (char*)md5ctx.abcd); //选择返回对象 FILE *fp = _wfopen (filename, L"rb") ; if (fp == NULL) return bp ; unsigned __int64 rlen = 0 ; int block = 65536; //每次读入文件的字节数,这个数字可以根据需要更改 unsigned char* buf = new unsigned char[block]; try { MD5_Init (&md5ctx); while (rlen = fread (buf, 1, block, fp)) MD5_Update (&md5ctx, buf, rlen); MD5_Final (&md5ctx); } catch (...) { MD5_Init (&md5ctx); } delete[] buf ; fclose (fp) ; return bp ; }
----------------------------------------------
-