C++ MD5加密
2019-06-07
太久没写C++感觉自己变菜了,好吧这个也不是自己写的
先看目录结构
MD5.H
#pragma once #include <stdlib.h> #include <string> #include <iostream> #include <stdio.h> #include <fstream> typedef unsigned char byte; typedef unsigned long int UNIT; using namespace std; class MD5 { public: MD5(); MD5(const string &str); MD5(const void* input, size_t length); MD5(ifstream &ifs); void update(const void* input, size_t length); void update(const string &str); void update(ifstream &ifs); string toString(); const byte* digest(); void reset(); ~MD5(void); private: void update(const byte*input, size_t length); void final(); void transform(const byte block[64]); void encode(const UNIT *input, byte *output, size_t length); void decode(const byte *input, UNIT *output, size_t length); string bytesToHexString(const byte *input, size_t length); private: UNIT _state[4]; UNIT _count[2]; byte _buffer[64]; byte _digest[16]; bool _finished; static const byte PADDING[64]; static const char HEX[16]; static const size_t BUFFER_SIZE = 1024; };
MD5.cpp
#include "MD5.h" /* F, G, H and I are basic MD5 functions. */ inline UNIT F(UNIT x, UNIT y, UNIT z) { return (UNIT)(((x)& (y)) | ((~x) & (z))); } inline UNIT G(UNIT x, UNIT y, UNIT z) { return (UNIT)(((x)& (z)) | ((y)& (~z))); } inline UNIT H(UNIT x, UNIT y, UNIT z) { return (UNIT)((x) ^ (y) ^ (z)); } inline UNIT I(UNIT x, UNIT y, UNIT z) { return (UNIT)((y) ^ ((x) | (~z))); } /* ROTATE_LEFT rotates x left n bits. */ inline UNIT ROTATE_LEFT(UNIT x, UNIT n) { return UNIT(((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. */ void FF(UNIT &a, UNIT &b, UNIT &c, UNIT &d, UNIT x, UNIT s, UNIT ac){ a += F((b), (c), (d)) + (x)+ac; a = ROTATE_LEFT((a), (s)); a += b; } void GG(UNIT &a, UNIT &b, UNIT &c, UNIT &d, UNIT x, UNIT s, UNIT ac) { (a) += G((b), (c), (d)) + (x)+ac; (a) = ROTATE_LEFT((a), (s)); (a) += (b); } void HH(UNIT &a, UNIT &b, UNIT &c, UNIT &d, UNIT x, UNIT s, UNIT ac) { (a) += H((b), (c), (d)) + (x)+ac; (a) = ROTATE_LEFT((a), (s)); (a) += (b); } void II(UNIT &a, UNIT &b, UNIT &c, UNIT &d, UNIT x, UNIT s, UNIT ac) { (a) += I((b), (c), (d)) + (x)+ac; (a) = ROTATE_LEFT((a), (s)); (a) += (b); } const int s[16] = { 7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21 }; 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' }; MD5::MD5() { reset(); } MD5::MD5(const string &str){ reset(); update(str); } MD5::MD5(const void* input, size_t length){ reset(); update(input, length); } MD5::MD5(ifstream &ifs){ reset(); update(ifs); } void MD5::reset(){ _state[0] = 0x67452301; _state[1] = 0xefcdab89; _state[2] = 0x98badcfe; _state[3] = 0x10325476; _finished = false; _count[0] = _count[1] = 0; } const byte* MD5::digest(){ if (!_finished){ _finished = true; final(); } return _digest; } void MD5::final(){ byte bits[8]; UNIT oldState[4]; UNIT oldCount[2]; UNIT index, padLen; memcpy(oldState, _state, 16); memcpy(oldCount, _count, 8); encode(_count, bits, 8); index = (UNIT)((_count[0] >> 3)&(0x3f)); padLen = (index < 56) ? (56 - index) : (120 - index); update(PADDING, padLen); update(bits, 8); encode(_state, _digest, 16); } void MD5::update(const void *input, size_t length){ update((const byte*)input, length); } void MD5::update(const string &str){ update((const byte*)str.c_str(), str.length()); } void MD5::update(ifstream &ifs){ if (!ifs){ return; } streamsize length; char buffer[BUFFER_SIZE]; while (!ifs.eof()){ ifs.read(buffer, BUFFER_SIZE); length = ifs.gcount(); if (length >0){ update(buffer, length); } } ifs.close(); } void MD5::update(const byte* input, size_t length){ UNIT index, partLen, i; _finished = false; //Compute number of bytes mod 64 index = (UNIT)((_count[0] >> 3) & 0x3f); if ((_count[0] += ((UNIT)length << 3))<((UNIT)length << 3)){ _count[1]++; } _count[1] += ((UNIT)length >> 29); partLen = 64 - index; 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; memcpy(&_buffer[index], &input[i], length - i); } void MD5::transform(const byte block[64]){ UNIT a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16]; decode(block, x, 64); int i = 0; FF(a, b, c, d, x[0], s[i++], 0xd76aa478); /* 1 */ FF(d, a, b, c, x[1], s[i++], 0xe8c7b756); /* 2 */ FF(c, d, a, b, x[2], s[i++], 0x242070db); /* 3 */ FF(b, c, d, a, x[3], s[i++], 0xc1bdceee); /* 4 */ i -= 4; FF(a, b, c, d, x[4], s[i++], 0xf57c0faf); /* 5 */ FF(d, a, b, c, x[5], s[i++], 0x4787c62a); /* 6 */ FF(c, d, a, b, x[6], s[i++], 0xa8304613); /* 7 */ FF(b, c, d, a, x[7], s[i++], 0xfd469501); /* 8 */ i -= 4; FF(a, b, c, d, x[8], s[i++], 0x698098d8); /* 9 */ FF(d, a, b, c, x[9], s[i++], 0x8b44f7af); /* 10 */ FF(c, d, a, b, x[10], s[i++], 0xffff5bb1); /* 11 */ FF(b, c, d, a, x[11], s[i++], 0x895cd7be); /* 12 */ i -= 4; FF(a, b, c, d, x[12], s[i++], 0x6b901122); /* 13 */ FF(d, a, b, c, x[13], s[i++], 0xfd987193); /* 14 */ FF(c, d, a, b, x[14], s[i++], 0xa679438e); /* 15 */ FF(b, c, d, a, x[15], s[i++], 0x49b40821); /* 16 */ /* Round 2 */ GG(a, b, c, d, x[1], s[i++], 0xf61e2562); /* 17 */ GG(d, a, b, c, x[6], s[i++], 0xc040b340); /* 18 */ GG(c, d, a, b, x[11], s[i++], 0x265e5a51); /* 19 */ GG(b, c, d, a, x[0], s[i++], 0xe9b6c7aa); /* 20 */ i -= 4; GG(a, b, c, d, x[5], s[i++], 0xd62f105d); /* 21 */ GG(d, a, b, c, x[10], s[i++], 0x2441453); /* 22 */ GG(c, d, a, b, x[15], s[i++], 0xd8a1e681); /* 23 */ GG(b, c, d, a, x[4], s[i++], 0xe7d3fbc8); /* 24 */ i -= 4; GG(a, b, c, d, x[9], s[i++], 0x21e1cde6); /* 25 */ GG(d, a, b, c, x[14], s[i++], 0xc33707d6); /* 26 */ GG(c, d, a, b, x[3], s[i++], 0xf4d50d87); /* 27 */ GG(b, c, d, a, x[8], s[i++], 0x455a14ed); /* 28 */ i -= 4; GG(a, b, c, d, x[13], s[i++], 0xa9e3e905); /* 29 */ GG(d, a, b, c, x[2], s[i++], 0xfcefa3f8); /* 30 */ GG(c, d, a, b, x[7], s[i++], 0x676f02d9); /* 31 */ GG(b, c, d, a, x[12], s[i++], 0x8d2a4c8a); /* 32 */ /* Round 3 */ HH(a, b, c, d, x[5], s[i++], 0xfffa3942); /* 33 */ HH(d, a, b, c, x[8], s[i++], 0x8771f681); /* 34 */ HH(c, d, a, b, x[11], s[i++], 0x6d9d6122); /* 35 */ HH(b, c, d, a, x[14], s[i++], 0xfde5380c); /* 36 */ i -= 4; HH(a, b, c, d, x[1], s[i++], 0xa4beea44); /* 37 */ HH(d, a, b, c, x[4], s[i++], 0x4bdecfa9); /* 38 */ HH(c, d, a, b, x[7], s[i++], 0xf6bb4b60); /* 39 */ HH(b, c, d, a, x[10], s[i++], 0xbebfbc70); /* 40 */ i -= 4; HH(a, b, c, d, x[13], s[i++], 0x289b7ec6); /* 41 */ HH(d, a, b, c, x[0], s[i++], 0xeaa127fa); /* 42 */ HH(c, d, a, b, x[3], s[i++], 0xd4ef3085); /* 43 */ HH(b, c, d, a, x[6], s[i++], 0x4881d05); /* 44 */ i -= 4; HH(a, b, c, d, x[9], s[i++], 0xd9d4d039); /* 45 */ HH(d, a, b, c, x[12], s[i++], 0xe6db99e5); /* 46 */ HH(c, d, a, b, x[15], s[i++], 0x1fa27cf8); /* 47 */ HH(b, c, d, a, x[2], s[i++], 0xc4ac5665); /* 48 */ /* Round 4 */ II(a, b, c, d, x[0], s[i++], 0xf4292244); /* 49 */ II(d, a, b, c, x[7], s[i++], 0x432aff97); /* 50 */ II(c, d, a, b, x[14], s[i++], 0xab9423a7); /* 51 */ II(b, c, d, a, x[5], s[i++], 0xfc93a039); /* 52 */ i -= 4; II(a, b, c, d, x[12], s[i++], 0x655b59c3); /* 53 */ II(d, a, b, c, x[3], s[i++], 0x8f0ccc92); /* 54 */ II(c, d, a, b, x[10], s[i++], 0xffeff47d); /* 55 */ II(b, c, d, a, x[1], s[i++], 0x85845dd1); /* 56 */ i -= 4; II(a, b, c, d, x[8], s[i++], 0x6fa87e4f); /* 57 */ II(d, a, b, c, x[15], s[i++], 0xfe2ce6e0); /* 58 */ II(c, d, a, b, x[6], s[i++], 0xa3014314); /* 59 */ II(b, c, d, a, x[13], s[i++], 0x4e0811a1); /* 60 */ i -= 4; II(a, b, c, d, x[4], s[i++], 0xf7537e82); /* 61 */ II(d, a, b, c, x[11], s[i++], 0xbd3af235); /* 62 */ II(c, d, a, b, x[2], s[i++], 0x2ad7d2bb); /* 63 */ II(b, c, d, a, x[9], s[i++], 0xeb86d391); /* 64 */ _state[0] += a; _state[1] += b; _state[2] += c; _state[3] += d; } void MD5::encode(const UNIT *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); } } void MD5::decode(const byte *input, UNIT *output, size_t length){ for (size_t i = 0, j = 0; j<length; i++, j += 4){ output[i] = ((UNIT)(input[j]) | ((UNIT)(input[j + 1]) << 8) | ((UNIT)(input[j + 2]) << 16) | ((UNIT)(input[j + 3]) << 24)); } } 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; } string MD5::toString(){ return bytesToHexString(digest(), 16); } MD5::~MD5(void) { }
test.cpp
#include "md5.h" #include <iostream> using namespace std; void PrintMD5(const string &str, MD5 &md5) { cout << "MD5(\"" << str << "\") = " << md5.toString() << endl; } string FileDigest(const string &file) { ifstream in(file.c_str(), ios::binary); if (!in) return ""; MD5 md5; std::streamsize length; char buffer[1024]; while (!in.eof()) { in.read(buffer, 1024); length = in.gcount(); if (length > 0) md5.update(buffer, length); } in.close(); return md5.toString(); } int main() { cout << "Please input a string:" << endl; string str; while (getline(cin, str)){ cout << "Source Code:" << str << endl << "MD5:" << MD5(str).toString() << endl; cout << "Please input a string:" << endl; } system("pause"); /*cout << MD5(ifstream("D:\\test.txt")).toString() << endl; cout << MD5(ifstream("D:\\test.exe", ios::binary)).toString() << endl; cout << FileDigest("D:\\test.exe") << endl; */ /* 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; */ }
运行截图看一下
借鉴自:https://blog.csdn.net/linmude/article/details/39274555
发表评论: