标签:
首先是MD5
#define SECTION 4 #define GROUPSIZE 16 #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 LOOP_LEFT_MOVE(x,y) (x<<y | x>>(32-y)) #define FF(a,b,c,d,mj,s,ti) LOOP_LEFT_MOVE(a + F(b, c, d) + mj + ti,s)+b #define GG(a,b,c,d,mj,s,ti) LOOP_LEFT_MOVE(a + G(b, c, d) + mj + ti,s)+b #define HH(a,b,c,d,mj,s,ti) LOOP_LEFT_MOVE(a + H(b, c, d) + mj + ti,s)+b #define II(a,b,c,d,mj,s,ti) LOOP_LEFT_MOVE(a + I(b, c, d) + mj + ti,s)+b void GetFileDirectory(TCHAR File[MAX_PATH], OUT TCHAR RDirectory[MAX_PATH]); void GetFileNameWithoutExtension(TCHAR File[MAX_PATH], OUT TCHAR filename[MAX_PATH]);
// MD5.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" #include "Header.h" DWORD A = 0X67452301; DWORD B = 0XEFCDAB89; DWORD C = 0X98BADCFE; DWORD D = 0X10325476; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //------------------------------ DWORD groups = 0; //----------------------------- TCHAR szBuffer[MAX_PATH] = { 0 }; OPENFILENAME ofn = { 0 }; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = NULL; ofn.lpstrFilter = _T("TXT文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0");//要选择的文件后缀 ofn.lpstrInitialDir = _T("D:\\");//默认的文件路径 ofn.lpstrFile = szBuffer;//存放文件的缓冲区 ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer); ofn.nFilterIndex = 0; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;//标志如果是多选要加上OFN_ALLOWMULTISELECT BOOL bSel = GetOpenFileName(&ofn); HANDLE hFile = CreateFile(szBuffer, GENERIC_ALL, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0); int i = GetLastError(); DWORD FileSizeHigh; DWORD FileSizeLow = GetFileSize(hFile, &FileSizeHigh); PBYTE pFile = (PBYTE)malloc((FileSizeLow / 64 + 2) * 64); DWORD fileRTemp; ReadFile(hFile, pFile, FileSizeLow,&fileRTemp, 0); if (FileSizeHigh == 0) { PBYTE pFileEnd = pFile + FileSizeLow; if (FileSizeLow % 64 != 56) { if (FileSizeLow % 64 < 56) { memset(pFileEnd, 0, 56 - FileSizeLow % 64); *pFileEnd = *pFileEnd | 0x80; pFileEnd += 56 - FileSizeLow % 64; groups = FileSizeLow / 64 + 1; } else { memset(pFileEnd, 0, 64 - FileSizeLow % 64 + 56); *pFileEnd = *pFileEnd | 0x80; pFileEnd += 64 - FileSizeLow % 64 + 56; groups = FileSizeLow / 64 + 2; } } else { groups = FileSizeLow / 64 + 1; } PDWORD64 temp = (PDWORD64)pFileEnd; *temp = FileSizeLow * 8; } else { } DWORD index = 0; for (; index < groups; index++) { DWORD a = A, b = B, c = C, d = D; DWORD M[16]; memcpy(M, pFile + index*GROUPSIZE,64); a = FF(a, b, c, d, M[0], 7, 0xd76aa478); b = FF(d, a, b, c, M[1], 12, 0xe8c7b756); c = FF(c, d, a, b, M[2], 17, 0x242070db); d = FF(b, c, d, a, M[3], 22, 0xc1bdceee); a = FF(a, b, c, d, M[4], 7, 0xf57c0faf); b = FF(d, a, b, c, M[5], 12, 0x4787c62a); c = FF(c, d, a, b, M[6], 17, 0xa8304613); d = FF(b, c, d, a, M[7], 22, 0xfd469501); a = FF(a, b, c, d, M[8], 7, 0x698098d8); b = FF(d, a, b, c, M[9], 12, 0x8b44f7af); c = FF(c, d, a, b, M[10], 17, 0xffff5bb1); d = FF(b, c, d, a, M[11], 22, 0x895cd7be); a = FF(a, b, c, d, M[12], 7, 0x6b901122); b = FF(d, a, b, c, M[13], 12, 0xfd987193); c = FF(c, d, a, b, M[14], 17, 0xa679438e); d = FF(b, c, d, a, M[15], 22, 0x49b40821); a = GG(a, b, c, d, M[1], 5, 0xf61e2562); b = GG(d, a, b, c, M[6], 9, 0xc040b340); c = GG(c, d, a, b, M[11], 14, 0x265e5a51); d = GG(b, c, d, a, M[0], 20, 0xe9b6c7aa); a = GG(a, b, c, d, M[5], 5, 0xd62f105d); b = GG(d, a, b, c, M[10], 9, 0x02441453); c = GG(c, d, a, b, M[15], 14, 0xd8a1e681); d = GG(b, c, d, a, M[4], 20, 0xe7d3fbc8); a = GG(a, b, c, d, M[9], 5, 0x21e1cde6); b = GG(d, a, b, c, M[14], 9, 0xc33707d6); c = GG(c, d, a, b, M[3], 14, 0xf4d50d87); d = GG(b, c, d, a, M[8], 20, 0x455a14ed); a = GG(a, b, c, d, M[13], 5, 0xa9e3e905); b = GG(d, a, b, c, M[2], 9, 0xfcefa3f8); c = GG(c, d, a, b, M[7], 14, 0x676f02d9); d = GG(b, c, d, a, M[12], 20, 0x8d2a4c8a); a = HH(a, b, c, d, M[5], 4, 0xfffa3942); b = HH(d, a, b, c, M[8], 11, 0x8771f681); c = HH(c, d, a, b, M[11], 16, 0x6d9d6122); d = HH(b, c, d, a, M[14], 23, 0xfde5380c); a = HH(a, b, c, d, M[1], 4, 0xa4beea44); b = HH(d, a, b, c, M[4], 11, 0x4bdecfa9); c = HH(c, d, a, b, M[7], 16, 0xf6bb4b60); d = HH(b, c, d, a, M[10], 23, 0xbebfbc70); a = HH(a, b, c, d, M[13], 4, 0x289b7ec6); b = HH(d, a, b, c, M[0], 11, 0xeaa127fa); c = HH(c, d, a, b, M[3], 16, 0xd4ef3085); d = HH(b, c, d, a, M[6], 23, 0x04881d05); a = HH(a, b, c, d, M[9], 4, 0xd9d4d039); b = HH(d, a, b, c, M[12], 11, 0xe6db99e5); c = HH(c, d, a, b, M[15], 16, 0x1fa27cf8); d = HH(b, c, d, a, M[2], 23, 0xc4ac5665); a = II(a, b, c, d, M[0], 6, 0xf4292244); b = II(d, a, b, c, M[7], 10, 0x432aff97); c = II(c, d, a, b, M[14], 15, 0xab9423a7); d = II(b, c, d, a, M[5], 21, 0xfc93a039); a = II(a, b, c, d, M[12], 6, 0x655b59c3); b = II(d, a, b, c, M[3], 10, 0x8f0ccc92); c = II(c, d, a, b, M[10], 15, 0xffeff47d); d = II(b, c, d, a, M[1], 21, 0x85845dd1); a = II(a, b, c, d, M[8], 6, 0x6fa87e4f); b = II(d, a, b, c, M[15], 10, 0xfe2ce6e0); c = II(c, d, a, b, M[6], 15, 0xa3014314); d = II(b, c, d, a, M[13], 21, 0x4e0811a1); a = II(a, b, c, d, M[4], 6, 0xf7537e82); b = II(d, a, b, c, M[11], 10, 0xbd3af235); c = II(c, d, a, b, M[2], 15, 0x2ad7d2bb); d = II(b, c, d, a, M[9], 21, 0xeb86d391); A += a; B += b; C += c; D += d; } BYTE result[16]; memcpy(result, &A, 4); memcpy(result+4, &B, 4); memcpy(result+8, &C, 4); memcpy(result+12, &D, 4); TCHAR RDirectory[MAX_PATH] = { 0 }; TCHAR FileNameWithOutExt[MAX_PATH] = { 0 }; GetFileDirectory(szBuffer, RDirectory); GetFileNameWithoutExtension(szBuffer, FileNameWithOutExt); wcscat_s(RDirectory, FileNameWithOutExt); wcscat_s(RDirectory, L"-md5.txt"); HANDLE hReFile = CreateFile(RDirectory, GENERIC_ALL, FILE_SHARE_READ, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hReFile, result, 16, &fileRTemp, 0); free(pFile); CloseHandle(hFile); CloseHandle(hReFile); return 0; } void GetFileDirectory(TCHAR File[MAX_PATH], OUT TCHAR RDirectory[MAX_PATH]) { int index = 0; for (int i = 0; i < MAX_PATH; i++) { if (File[i] == TEXT(‘\\‘)) { index = i; continue; } if (File[i] == 0) { break; } } memcpy(RDirectory, File, (index + 1) * 2); } void GetFileNameWithoutExtension(TCHAR File[MAX_PATH], OUT TCHAR filename[MAX_PATH]) { int dindex = 0; int pindex = 0; for (int i = 0; i < MAX_PATH; i++) { if (File[i] == TEXT(‘\\‘)) { dindex = i; continue; } if (File[i] == TEXT(‘.‘)) { pindex = i; continue; } if (File[i] == 0) { break; } } memcpy(filename, File + dindex + 1, (pindex - dindex - 1) * 2); }
SHA1的分组和填充与MD5是一样的
#define RESULT_BYTES 20 #define FT(B,C,D) ((B & C) | ((~B) & D)) #define GT(B,C,D) (B^C^D) #define HT(B,C,D) ((B&C) | (B&D) | (C&D)) #define IT(B,C,D) (B^C^D) #define LEFT_RORATE(x,y) ((x<<y) | x>>(32-y)) #define GET_BYTE_FROM_QWORD(x,y) ((x & (0xffffffffffffffff << (8-y)*8) & (0xffffffffffffffff >>(y-1)*8)) >> (8-y)*8)
// MD5.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "windows.h" #include "Header.h" DWORD It = 0x5A827999, Jt = 0x6ED9EBA1, Kt = 0x8F1BBCDC, Lt = 0xCA62C1D6; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //------------------------------ DWORD groups = 0; //----------------------------- TCHAR szBuffer[MAX_PATH] = { 0 }; OPENFILENAME ofn = { 0 }; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = NULL; ofn.lpstrFilter = _T("TXT文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0");//要选择的文件后缀 ofn.lpstrInitialDir = _T("D:\\");//默认的文件路径 ofn.lpstrFile = szBuffer;//存放文件的缓冲区 ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer); ofn.nFilterIndex = 0; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;//标志如果是多选要加上OFN_ALLOWMULTISELECT BOOL bSel = GetOpenFileName(&ofn); HANDLE hFile = CreateFile(szBuffer, GENERIC_ALL, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0); DWORD FileSizeHigh; DWORD FileSizeLow = GetFileSize(hFile, &FileSizeHigh); PBYTE pFile; DWORD fileRTemp; if (FileSizeHigh == 0) { pFile = (PBYTE)malloc((FileSizeLow / 64 + 2) * 64); ReadFile(hFile, pFile, FileSizeLow, &fileRTemp, 0); PBYTE pFileEnd = pFile + FileSizeLow; if (FileSizeLow % 64 != 56) { if (FileSizeLow % 64 < 56) { memset(pFileEnd, 0, 56 - FileSizeLow % 64); *pFileEnd = *pFileEnd | 0x80; pFileEnd += 56 - FileSizeLow % 64; groups = FileSizeLow / 64 + 1; } else { memset(pFileEnd, 0, 64 - FileSizeLow % 64 + 56); *pFileEnd = *pFileEnd | 0x80; pFileEnd += 64 - FileSizeLow % 64 + 56; groups = FileSizeLow / 64 + 2; } } else { groups = FileSizeLow / 64 + 1; } DWORD64 temp = (DWORD64)FileSizeLow * 8; for (int i = 0; i < 8; i++) { *(pFileEnd++) = GET_BYTE_FROM_QWORD(temp, i + 1); } } else { MessageBox(NULL, L"big file not support now", L"", 0); exit(0); } DWORD A[5] = { 0 }; DWORD H[5] = { 0 }; DWORD W[80] = { 0 }; H[0] = 0x67452301; H[1] = 0xEFCDAB89; H[2] = 0x98BADCFE; H[3] = 0x10325476; H[4] = 0xC3D2E1F0; PBYTE ptempFile = pFile; for (DWORD index = 0; index < groups; index++) { memcpy(W, ptempFile, 16); for (int i = 16; i <= 79; i++) { W[i] = LEFT_RORATE(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1); } memcpy(A, H, 5); DWORD temptemp; for (int i = 0; i < 80; i++) { if (i >= 0 && i <= 19) { temptemp = LEFT_RORATE(A[0], 5) + FT(A[1], A[2], A[3]) + A[4] + 0x5A827999 + W[i]; } else if (i >= 20 && i << 39) { temptemp = LEFT_RORATE(A[0], 5) + GT(A[1], A[2], A[3]) + A[4] + 0x6ED9EBA1 + W[i]; } else if (i >= 40 && i <= 59) { temptemp = LEFT_RORATE(A[0], 5) + HT(A[1], A[2], A[3]) + A[4] + 0x8F1BBCDC + W[i]; } else if (i >= 60 && i <= 79) { temptemp = LEFT_RORATE(A[0], 5) + IT(A[1], A[2], A[3]) + A[4] + 0xCA62C1D6 + W[i]; } A[4] = A[3]; A[3] = A[2]; A[2] = LEFT_RORATE(A[1], 30); A[1] = A[0]; A[0] = temptemp; } H[0] += A[0]; H[1] += A[1]; H[2] += A[2]; H[3] += A[3]; H[4] += A[4]; } DWORD result[RESULT_BYTES] = { 0 }; for (int i = 0; i < RESULT_BYTES; i++) { memcpy(result + i, H + i, 4); } //-------------------------------- TCHAR RDirectory[MAX_PATH] = { 0 }; TCHAR FileNameWithOutExt[MAX_PATH] = { 0 }; GetFileDirectory(szBuffer, RDirectory); GetFileNameWithoutExtension(szBuffer, FileNameWithOutExt); wcscat_s(RDirectory, FileNameWithOutExt); wcscat_s(RDirectory, L"-sha1.txt"); HANDLE hReFile = CreateFile(RDirectory, GENERIC_ALL, FILE_SHARE_READ, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0); WriteFile(hReFile, result, RESULT_BYTES, &fileRTemp, 0); free(pFile); CloseHandle(hFile); CloseHandle(hReFile); return 0; } void GetFileDirectory(TCHAR File[MAX_PATH], OUT TCHAR RDirectory[MAX_PATH]) { int index = 0; for (int i = 0; i < MAX_PATH; i++) { if (File[i] == TEXT(‘\\‘)) { index = i; continue; } if (File[i] == 0) { break; } } memcpy(RDirectory, File, (index + 1) * 2); } void GetFileNameWithoutExtension(TCHAR File[MAX_PATH], OUT TCHAR filename[MAX_PATH]) { int dindex = 0; int pindex = 0; for (int i = 0; i < MAX_PATH; i++) { if (File[i] == TEXT(‘\\‘)) { dindex = i; continue; } if (File[i] == TEXT(‘.‘)) { pindex = i; continue; } if (File[i] == 0) { break; } } memcpy(filename, File + dindex + 1, (pindex - dindex - 1) * 2); }
标签:
原文地址:http://www.cnblogs.com/distanceblog/p/5467549.html