当前位置:编程学习 > 网站相关 >>

C++的MD5加密

<pre name="code" class="cpp"> 
#ifndef _MD5_H 
#define _MD5_H  
 
#pragma warning(disable:4786)  
 
#include <string> 
using namespace std;  
 
struct MD5_DATA 

 unsigned long data[4]; 
 bool operator < (const MD5_DATA& p) const 
 { 
  return memcmp(data,p.data,4*sizeof(long))>0; 
 } 
};  
 
class CMD5 

private: 
 #define uint8  unsigned char 
 #define uint32 unsigned long int  
 
 struct md5_context 
 { 
  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, size_t length ); 
 void md5_finish( struct md5_context *ctx, uint8 digest[16] );  
 
public: 
 //! construct a CMD5 from any buffer 
 void GenerateMD5(unsigned char* buffer,size_t bufferlen);  
 
 //! construct a CMD5 
 CMD5();  
 
 //! construct a md5src from char * 
 CMD5(const char * md5src);  
 
 //! construct a CMD5 from a 16 bytes md5 
 CMD5(unsigned long* md5src);  
 
 //! add a other md5 
 CMD5 operator +(CMD5 adder);  
 
 //! just if equal 
 bool operator ==(CMD5 cmper);  
 
 //! give the value from equer 
 // void operator =(CMD5 equer);  
 
 //! to a string 
 string ToString();  
 
 unsigned long m_data[4]; 
}; 
#endif /* md5.h */ 
#include "stdafx.h" 
#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) >> 16) & 0xFF);      \ 
    (((uint8 *) b)[(i)+3]) = (uint8) (((n) >> 24) & 0xFF);      \ 
}  
 
void CMD5::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 CMD5::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, 60 );  
 
#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]; 补充:综合编程 , 安全编程 ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,