MD5 Features:
1. Input any length of information, after processing, the output is 128 bits of information (digital fingerprint);
2. Different inputs to obtain different results (uniqueness);
3. It is impossible to reverse the input information (irreversible) according to the output of 128 bits;
MD5 use: Add the following header file and implementation file
string C,key; c= "203"; MD5 iMD5; IMD5.GENERATEMD5 (unsigned char*) c.c_str (), c.size ()); Key = Imd5.tostring ();
Header file:
#ifndef _cmd5_h #define _CMD5_H//#pragma warning (disable:4786) #include <string> using namespace std; /*! * Manage MD5. */class MD5 {private: #define UINT8 unsigned char #define uint32 unsigned long int struct md5_conte XT {UInt32 total[2]; UInt32 State[4]; Uint8 buffer[64]; }; void Md5_starts (struct md5_context *ctx); void md5_process (struct md5_context *ctx, uint8 data[64]); void Md5_update (struct md5_context *ctx, uint8 *input, uint32 length); void Md5_finish (struct md5_context *ctx, uint8 digest[16]); Public://! Construct a CMD5 from any buffer void GenerateMD5 (unsigned char* buffer,int bufferlen); //! Construct a CMD5 MD5 (); //! Construct a md5src from char * MD5 (const char * md5src); //! Construct a CMD5 from a bytes md5 MD5 (unsigned long* md5src); //! Add a other MD5 MD5 operator + (MD5 adder); //!Just if equal bool operator = = (MD5 cmper); //! Give the value from Equer//void operator = (CMD5 equer); //! To a string string ToString (); unsigned long m_data[4]; }; #endif/* md5.h */
Implementation file:
/* * RFC 1321 compliant MD5 implementation, * by Christophe Devine <[email protected]>; * This program is licensed under the GPL. */#include "Md5.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #define GET_UINT32 (N,b, i) { (n) = (UInt32) ((uint8 *) b) [(i)] | (((UInt32) (((uint8 *) b) [(i) +1]) << 8) | (((UInt32) (((uint8 *) b) [(i) +2]) << 16) | (((UInt32) (((uint8 *) b) [(i) +3]) << 24); } #define PUT_UINT32 (n,b,i) { (((uint8 *) b) [(i)]) = (uint8) (((n)) & 0xFF); (((uint8 *) b) [(i) +1]) = (uint8) (((n) >> 8) & 0xFF); (((uint8 *) b) [(i)+2]) = (uint8) (((n) >>) & 0xFF); (((uint8 *) b) [(i) +3]) = (uint8) (((n) >>) & 0xFF); }//extern pthread_mutex_t mutexmemory;void md5::md5_starts (struct md5_context *ctx) {ctx->total[0] = 0; CTX->TOTAL[1] = 0; Ctx->state[0] = 0x67452301; CTX->STATE[1] = 0xefcdab89; CTX->STATE[2] = 0x98badcfe; CTX->STATE[3] = 0x10325476;} void md5::md5_process (struct md5_context *ctx, uint8 data[64]) {UInt32 A, B, C, D, x[16]; Get_uint32 (x[0], data, 0); Get_uint32 (x[1], data, 4); Get_uint32 (x[2], data, 8); Get_uint32 (x[3], data, 12); Get_uint32 (x[4], data, 16); Get_uint32 (x[5], data, 20); Get_uint32 (x[6], data, 24); Get_uint32 (x[7], data, 28); Get_uint32 (X[8], data, 32); Get_uint32 (x[9], data, 36); Get_uint32 (x[10], data, 40); Get_uint32 (x[11], data, 44); Get_uint32 (x[12], data, 48); Get_uint32 (x[13], data, 52); Get_uint32 (x[14], data, 56); Get_uint32 (x[15], data, each); #define S (X,n) ((X << N) | ((x & 0xFFFFFFFF) >> (32-n))) #define P (a,b,c,d,k,s,t) { A + = F (b,c,d) + x[k] + t; A = S (a,s) + B; } A = ctx->state[0]; B = ctx->state[1]; C = ctx->state[2]; D = ctx->state[3]; #define F (x, Y, z) (z ^) P (A, B, C, D, 0, 7, 0xd76aa478); P (D, A, B, C, 1, 0xe8c7b756); P (C, D, A, B, 2, N, 0x242070db); P (B, C, D, A, 3, 0xc1bdceee); P (A, B, C, D, 4, 7, 0XF57C0FAF); P (D, A, B, C, 5, 0x4787c62a); P (C, D, A, B, 6, N, 0xa8304613); P (B, C, D, A, 7, 0xfd469501); P (A, B, C, D, 8, 7, 0X698098D8); P (D, A, B, C, 9, 0X8B44F7AF); P (C, D, A, B, ten, N, 0XFFFF5BB1); P (B, C, D, A, one, A, 0x895cd7be); P (A, B, C, D, 7, 0x6b901122); P (D, A, B, C, 0xfd987193, N); P (C, D, A, B, +, 0xa679438e); P (b, C, D, A, 1, p, 0x49b40821); #undef f#define F (z & (x ^ y)) (y ^) (A, B, C, D, 5, 0xf61e2562 ); P (D, A, B, C, 6, 9, 0xc040b340); P (C, D, A, B, one, A, 0x265e5a51); P (B, C, D, A, 0, 0XE9B6C7AA); P (A, B, C, D, 5, 5, 0xd62f105d); P (D, A, B, C, ten, 9, 0x02441453); P (C, D, A, B, N, 0xd8a1e681); P (B, C, D, A, 4, 0XE7D3FBC8); P (A, B, C, D, 9, 5, 0X21E1CDE6); P (D, A, B, C, 9, 0xc33707d6); P (C, D, A, B, 3, 0xf4d50d87); P (B, C, D, A, 8, 0x455a14ed); P (A, B, C, D, 5, 0xa9e3e905); P (D, A, B, C, 2, 9, 0XFCEFA3F8); P (C, D, A, B, 7, 0X676F02D9); P (b, C, D, A, A, B, 0x8d2a4c8a), #undef F #define F (x, Y, z) p (A, B., C, D, 5, 4, 0xfffa3942); P (D, A, B, C, 8, one, 0x8771f681); P (C, D, A, B, one, A, 0x6d9d6122); P (B, C, D, A, +, 0xfde5380c); P (A, B, C, D, 1, 4, 0xa4beea44); P (D, A, B, C, 4, one, 0x4bdecfa9); P (C, D, A, B, 7, 0xf6bb4b60); P (B, C, D, A, ten, Max, 0XBEBFBC70); P (A, B, C, D, 4, 0X289B7EC6); P (D, A, B, C, 0, one, 0XEAA127FA); P (C, D, A, B, 3, 0xd4ef3085); P (B, C, D, A, 6, 23°c, 0X04881D05); P (A, B, C, D, 9, 4, 0xd9d4d039); P (D, A, B, C, N, one, 0xe6db99e5); P (C, D, A, B, 0x1fa27cf8); P (b, C, D, A, 2, p, 0xc4ac5665); #undef f#define F (x, Y, z) (~z) (A, B, C, D, 0, 6, 0xf4292244); P (D, A, B, C, 7, ten, 0X432AFF97); P (C, D, A, B, +, 0XAB9423A7); P (B, C, D, A, 5, 0xfc93a039); P (A, B, C, D, 6, 0X655B59C3); P (D, A, B, C, 3, ten, 0x8f0ccc92); P (C, D, A, B, ten, 0xffeff47d); P (B, C, D, A, 1, 0X85845DD1); P (A, B, C, D, 8, 6, 0x6fa87e4f); P (D, A, B, C, +, 0XFE2CE6E0); P (C, D, A, B, 6, 0xa3014314); P (B, C, D, A, +, 0X4E0811A1); P (A, B, C, D, 4, 6, 0xf7537e82); P (D, A, B, C, one, ten, 0xbd3af235); P (C, D, A, B, 2, 0X2AD7D2BB); P (B, C, D, A, 9, 0xeb86d391); #undef F ctx->state[0] + A; CTX->STATE[1] + = B; CTX->STATE[2] + = C; CTX->STATE[3] + = D;} void Md5::md5_update (struct md5_context *ctx, uint8 *input, UInt32 length) {UInt32 left, fill; if (! length) return; left = (Ctx->total[0] >> 3) & 0x3F; fill = 64-left; Ctx->total[0] + = length << 3; CTX->TOTAL[1] + = length >> 29; Ctx->total[0] &= 0xFFFFFFFF; CTX->TOTAL[1] + = Ctx->total[0] < length << 3; if (left && length >= fill) {memcpy (void *) (Ctx->buffer + left), (void *) input, fill); Md5_process (CTX, Ctx->buffer); Length-= fill; Input + = fill; left = 0; } while (length >=) {md5_process (CTX, input); Length-= 64; Input + = 64; } if (length) { memcpy (void *) (Ctx->buffer + left), (void *) input, length); }}static uint8 md5_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};void M D5::md5_finish (struct Md5_context *ctx, uint8 digest[16]) {UInt32 last, Padn; Uint8 Msglen[8]; Put_uint32 (Ctx->total[0], Msglen, 0); Put_uint32 (Ctx->total[1], Msglen, 4); Last = (Ctx->total[0] >> 3) & 0x3F; Padn = (Last < 56)? (56-last): (120-last); Md5_update (CTX, md5_padding, PADN); Md5_update (CTX, Msglen, 8); Put_uint32 (Ctx->state[0], digest, 0); Put_uint32 (Ctx->state[1], Digest, 4); Put_uint32 (Ctx->state[2], Digest, 8); Put_uint32 (Ctx->state[3], Digest, 12);} void Md5::generatemd5 (unsigned char* buffer,int bufferlen) {struct Md5_context context; Md5_starts (&cOntext); Md5_update (&context, buffer, bufferlen); Md5_finish (&context, (unsigned char*) m_data);} Md5::md5 () {for (int i=0;i<4;i++) m_data[i]=0;} MD5::MD5 (unsigned long* md5src) {memcpy (m_data,md5src,16);} int _httoi (const char *value) {struct Chexmap {char chr; int value; }; const int HEXMAPL = 16; Chexmap HEXMAP[HEXMAPL] = {{' 0 ', 0}, {' 1 ', 1}, {' 2 ', 2}, {' 3 ', 3}, {' 4 ', 4}, {' 5 ', 5}, {' 6 ', 6}, {' 8 ', 8}, {' 9 ', 9}, {' A ', ' ten} ', {' B ', ' one} ', {' C ', +}, {' d ', ', ',}, {' E ', +}, {' F ', 15}}; Pthread_mutex_lock (&mutexmemory); Char *mstr = strdup (value); Pthread_mutex_unlock (&mutexmemory); char *s = MSTR; int result = 0; if (*s = = ' 0 ' && * (s + 1) = = ' X ') s + = 2; BOOL Firsttime = true; while (*s! = '/0 ') {bool found = false; for (int i = 0; i < HEXMAPL; i++) {if (*s = = HEXMAP[I].CHR) {if (!firsttime) result <<= 4; Result |= Hexmap[i].value; Found = true; Break }} if (!found) break; s++; Firsttime = false; }//pthread_mutex_lock (&mutexmemory); Free (MSTR); Pthread_mutex_unlock (&mutexmemory); return result;} MD5::MD5 (const char* MD5SRC) {if (strcmp (MD5SRC, "") ==0) {for (int i=0;i<4;i++) m_data[i]=0; Return } for (int j = 0; J <; J + +) {char buf[10]; strncpy (buf,md5src,2); md5src+=2; ((unsigned char*) m_data) [j] = _httoi (BUF); }}MD5 Md5::operator + (MD5 adder) {unsigned long m_newdata[4]; for (int i=0;i<4;i++) m_newdata[i]=m_data[i]^ (Adder.m_data[i]); Return MD5 (M_newdata);} BOOL Md5::operator = = (MD5 cmper) {return (memcmp (Cmper.m_data, m_data,16) ==0);} void Md5::operator = (MD5 equer)//{//memcpy (m_data,equer.m_data, +);//}string md5::tostring () {char output[33]; for (int j = 0; J <; J + +) {sprintf (output + J * 2, "%02x", ((unsigned char*) m_data) [j]); } return STring (output);}
MD5 Encryption algorithm