Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1136244
  • 博文数量: 646
  • 博客积分: 288
  • 博客等级: 二等列兵
  • 技术积分: 5375
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-08 14:33
个人简介

为了技术,我不会停下学习的脚步,我相信我还能走二十年。

文章分类

全部博文(646)

文章存档

2014年(8)

2013年(134)

2012年(504)

分类: C/C++

2013-03-09 14:21:26

原文地址:md5加密 作者:bjpiao

1.使用openssl库
编写test.cpp文件
  1. #include<stdio.h>
  2. #include<openssl/md5.h>
  3. #include<string.h>

  4. int main( int argc, char **argv )
  5. {
  6. MD5_CTX ctx;
  7. char *data="19870617";
  8. unsigned char md[16];
  9. char buf[33]={'\0'};
  10. char tmp[3]={'\0'};
  11. int i;

  12. MD5_Init(&ctx);
  13. MD5_Update(&ctx,(unsigned char *)data,strlen(data));
  14. MD5_Final(md,&ctx);

  15. for( i=0; i<16; i++ ){
  16. sprintf(tmp,"%02X",md[i]);
  17. //sprintf(tmp, "%2.2x", md[i]);
  18. strcat(buf,tmp);
  19. }
  20. printf("%s\n",buf);
  21. return 0;
  22. }

编译
g++ test.cpp -I./include/ -L./lib/ -lssl -lcrypto

2.改进版
  1. #include<stdio.h>
  2. #include<openssl/md5.h>
  3. #include<string.h>

  4. char c_b2s[256][4] = {
  5. "00","01","02","03","04","05","06","07","08","09",
  6. "0a","0b","0c","0d","0e","0f","10","11","12","13",
  7. "14","15","16","17","18","19","1a","1b","1c","1d",
  8. "1e","1f","20","21","22","23","24","25","26","27",
  9. "28","29","2a","2b","2c","2d","2e","2f","30","31",
  10. "32","33","34","35","36","37","38","39","3a","3b",
  11. "3c","3d","3e","3f","40","41","42","43","44","45","46",
  12. "47","48","49","4a","4b","4c","4d","4e","4f","50","51",
  13. "52","53","54","55","56","57","58","59","5a","5b","5c",
  14. "5d","5e","5f","60","61","62","63","64","65","66","67",
  15. "68","69","6a","6b","6c","6d","6e","6f","70","71","72",
  16. "73","74","75","76","77","78","79","7a","7b","7c","7d","7e",
  17. "7f","80","81","82","83","84","85","86","87","88","89","8a",
  18. "8b","8c","8d","8e","8f","90","91","92","93","94","95","96",
  19. "97","98","99","9a","9b","9c","9d","9e","9f","a0","a1","a2","a3",
  20. "a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af","b0",
  21. "b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd",
  22. "be","bf","c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb",
  23. "cc","cd","ce","cf","d0","d1","d2","d3","d4","d5","d6","d7","d8","d9",
  24. "da","db","dc","dd","de","df","e0","e1","e2","e3","e4","e5","e6","e7",
  25. "e8","e9","ea","eb","ec","ed","ee","ef","f0","f1","f2","f3","f4","f5","f6",
  26. "f7","f8","f9","fa","fb","fc","fd","fe","ff"
  27. };

  28. string Char2Str16(char *pBuff,int iBuffLen)
  29. {
  30.     int i;
  31.     unsigned char iTmp = 0;
  32.     int index = 0;
  33.     char cTemp[1024];

  34.     string ret = "";

  35.     for(i=0; i<iBuffLen; i++)
  36.     {
  37.         iTmp = (unsigned char) pBuff[i];
  38.         cTemp[index++] = c_b2s[iTmp][0];
  39.         cTemp[index++] = c_b2s[iTmp][1];
  40.     }
  41.     cTemp[index] = '\0';
  42.     ret += cTemp;
  43.     return ret;
  44. }

  45. int Md5(const string& src, string &output)
  46. {
  47.     MD5_CTX c;
  48.     unsigned char md[MD5_DIGEST_LENGTH];        
  49.     int i;
  50.     char buf[MAXPSWLEN];    
  51.     memset(buf, 0, MAXPSWLEN);
  52.     strncpy(buf, src.c_str(), src.size());
  53.     i = src.size();

  54.     MD5_Init(&c);            
  55.     MD5_Update(&c,(unsigned char *)buf,(unsigned long)i);            
  56.     MD5_Final(&(md[0]),&c);

  57.     output = Char2Str16((char *)md, MD5_DIGEST_LENGTH);
  58.     return 0;

3. 生成md5类
md5.h
  1. #ifndef _MD5_H_
  2. #define _MD5_H_

  3. typedef unsigned char *POINTER;
  4. typedef unsigned short int UINT2;
  5. typedef unsigned long int UINT4;

  6. //CDESCrypt类的定义
  7. class MD5
  8. {
  9. public:

  10. typedef struct {
  11.   UINT4 state[4]; /* state (ABCD) */
  12.   UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
  13.   unsigned char buffer[64]; /* input buffer */
  14. } MD5_CTX;

  15. public:
  16.     MD5(){}
  17.     virtual ~MD5(){}

  18.     // 供外部使用的哈希函数:生成16bytes的hash值
  19.     static void encode(unsigned char* p_pszDest, const char* p_pszSrc);

  20.     // 供外部使用的哈希函数:生成long long的字符串形式hash值
  21.     static void Hash64(const char* sInputUrl, char* sOutputDocID);    

  22.     // 供外部使用的哈希函数:生成long long的hash值
  23.     static void Hash64(const char* sInputUrl, long long & ddwDocID);

  24. private:
  25.     // MD5 初始化. 开始一个MD5操作, 写一个新的上下文
  26.     static void MD5Init (MD5_CTX *);

  27.     // MD5块的更新操作,继续MD5报文摘要操作,处理另外的报文块,并更新上下文
  28.     static void MD5Update(MD5_CTX *, unsigned char *, unsigned int);

  29.     // MD5结束操作,写报文摘要并清空上下文
  30.     static void MD5Final (unsigned char [16], MD5_CTX *);

  31.     // MD5的主要变换过程
  32.     static void MD5Transform (UINT4 [4], unsigned char [64]);

  33.     // 把输入的UINT4数据编码后输出成unsigned char
  34.     static void innerEncode (unsigned char *, UINT4 *, unsigned int);

  35.     // 把输入的unsigned char数据解码后输出成UINT4
  36.     static void innerDecode (UINT4 *, unsigned char *, unsigned int);

  37.     // 两个内存块数据之间复制,注意:这里不能用strcpy
  38.     static void MD5_memcpy (POINTER, POINTER, unsigned int);

  39.     // 把一块内存全部初始化为某一个值
  40.     static void MD5_memset (POINTER, int, unsigned int);
  41.     
  42. };

  43. #endif
  44. //

md5.cpp
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "md5.h"

  6. #define S11 7 //这是md5变换时要用到的常量数据
  7. #define S12 12
  8. #define S13 17
  9. #define S14 22
  10. #define S21 5
  11. #define S22 9
  12. #define S23 14
  13. #define S24 20
  14. #define S31 4
  15. #define S32 11
  16. #define S33 16
  17. #define S34 23
  18. #define S41 6
  19. #define S42 10
  20. #define S43 15
  21. #define S44 21

  22. static unsigned char PADDING[64] = { //这是补位时第一个要加上的数
  23.   0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

  26. // F, G, H and I 是基本的MD5处理位操作函数,X,Y,Z为32位整数
  27. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  28. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  29. #define H(x, y, z) ((x) ^ (y) ^ (z))
  30. #define I(x, y, z) ((y) ^ ((x) | (~z)))

  31. // 把x左移n位
  32. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))

  33. //第1轮变换:以[a b c d x s ac]表示如下操作a = b + ((a + F(b,c,d) + X[k] + T[ac]) <<< s).
  34. #define FF(a, b, c, d, x, s, ac) { \
  35.  (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  36.  (a) = ROTATE_LEFT ((a), (s)); \
  37.  (a) += (b); }

  38. //第2轮变换:以[a b c d x s ac]表示如下操作a=b + ((a + G(b,c,d) + X[k] + T[ac]) <<< s).
  39. #define GG(a, b, c, d, x, s, ac) { \
  40.  (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  41.  (a) = ROTATE_LEFT ((a), (s)); \
  42.  (a) += (b); \
  43.   }

  44. //第3轮变换:以[a b c d x s ac]表示如下操作a = b + ((a + H(b,c,d) + X[k] + T[ac]) <<< s).
  45. #define HH(a, b, c, d, x, s, ac) { \
  46.  (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  47.  (a) = ROTATE_LEFT ((a), (s)); \
  48.  (a) += (b); \
  49.   }

  50. //第4轮变换:以[a b c d x s ac]表示如下操作a = b + ((a + I(b,c,d) + X[k] + T[ac]) <<< s).
  51. #define II(a, b, c, d, x, s, ac) { \
  52.  (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  53.  (a) = ROTATE_LEFT ((a), (s)); \
  54.  (a) += (b); \
  55.   }

  56. /**
  57.  *功能描述: MD5 初始化. 开始一个MD5操作, 写一个新的上下文
  58.  *入口参数: MD5_CTX *context 加密上下文
  59.  *返回值: 无
  60.  */
  61.  void MD5::MD5Init (MD5_CTX *context)
  62. {
  63.     //这两个32位整数是用来保存待加密数据的长度,这里先初始化为0
  64.     context->count[0] = context->count[1] = 0;

  65.     //四个32位整数 (A,B,C,D) 用来计算信息摘要,初始化使用的是十六进制表示的数字
  66.     context->state[0] = 0x67452301;
  67.     context->state[1] = 0xefcdab89;
  68.     context->state[2] = 0x98badcfe;
  69.     context->state[3] = 0x10325476;
  70. }

  71. /**
  72.  *功能描述:MD5块的更新操作,继续MD5报文摘要操作,处理另外的报文块,并更新上下文
  73.  *入口参数:
  74.  *             MD5_CTX *context 加密上下文
  75.  *             unsigned char * input 待加密的数据
  76.  *             unsigned int inputLen 待加密的数据的长度
  77.  *返回值: 无
  78.  */
  79. void MD5::MD5Update (MD5_CTX * context, unsigned char * input, unsigned int inputLen)
  80. {
  81.     unsigned int i, index, partLen;

  82.     //按模64计算字节数
  83.     index = (unsigned int)((context->count[0] >> 3) & 0x3F);

  84.     // Update number of bits
  85.     if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3))
  86.         context->count[1]++;
  87.     context->count[1] += ((UINT4)inputLen >> 29);

  88.     partLen = 64 - index;

  89.     if (inputLen >= partLen)
  90.     {
  91.         // 被64整除余下的部分先进行加密
  92.         MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
  93.         MD5Transform (context->state, context->buffer);

  94.         // 以64个字节为一个单位来进行变换
  95.         for (i = partLen; i + 63 < inputLen; i += 64)
  96.             MD5Transform (context->state, &input[i]);

  97.         index = 0;
  98.     }
  99.     else
  100.         i = 0;

  101.     /* Buffer remaining input */
  102.     MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
  103. }

  104. /**
  105.  *功能描述: MD5结束操作,写报文摘要并清空上下文
  106.  *入口参数:
  107.  *            unsigned char digest[16],
  108.  *            MD5_CTX * context
  109.  *返回值: 无
  110.  */
  111. void MD5::MD5Final (unsigned char digest[16], MD5_CTX * context)
  112. {
  113.     unsigned char bits[8];
  114.     unsigned int index, padLen;

  115.     /* Save number of bits */
  116.     innerEncode (bits, context->count, 8);

  117.     /* Pad out to 56 mod 64.*/
  118.     index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  119.     padLen = (index < 56) ? (56 - index) : (120 - index);
  120.     MD5Update (context, PADDING, padLen);

  121.     /* Append length (before padding) */
  122.     MD5Update (context, bits, 8);

  123.     // 保存状态到摘要
  124.     innerEncode (digest, context->state, 16);

  125.     // 把x所指向的内存块清0
  126.     MD5_memset ((POINTER)context, 0, sizeof (*context));
  127. }

  128. /**
  129.  *功能描述:MD5的主要变换过程
  130.  *入口参数:
  131.  *            UINT4 state[4] 四个32位整数,用来计算信息摘要
  132.  *            unsigned char block[64]
  133.  *返回值: 无
  134.  *算法说明:512位的报文数据分成4个128位的数据块,依次被送到不同的散列函数进行4轮计算;
  135.  * 每一轮都按32位的小数据块进行复杂的运算,计算完成后全部写入ABCD寄存器,用于下
  136.  * 一轮的计算。等到最后计算完毕,在寄存器ABCD中的数据就是我们要求的MD5代码
  137.  */
  138. void MD5::MD5Transform (UINT4 state[4], unsigned char block[64])
  139. {
  140.     UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];

  141.     //每一次,把数据原文存放在16个元素的数组X中
  142.     innerDecode (x, block, 64);

  143.     // 第一轮变换
  144.     FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  145.     FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  146.     FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  147.     FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  148.     FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  149.     FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  150.     FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  151.     FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  152.     FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  153.     FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  154.     FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  155.     FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  156.     FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  157.     FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  158.     FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  159.     FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */

  160.     // 第二轮变换
  161.     GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  162.     GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  163.     GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  164.     GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  165.     GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  166.     GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  167.     GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  168.     GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  169.     GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  170.     GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  171.     GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  172.     GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  173.     GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  174.     GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  175.     GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  176.     GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */

  177.     // 第三轮变换
  178.     HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  179.     HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  180.     HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  181.     HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  182.     HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  183.     HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  184.     HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  185.     HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  186.     HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  187.     HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  188.     HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  189.     HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  190.     HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  191.     HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  192.     HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  193.     HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */

  194.     // 第四轮变换
  195.     II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  196.     II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  197.     II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  198.     II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  199.     II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  200.     II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  201.     II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  202.     II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  203.     II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  204.     II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  205.     II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  206.     II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  207.     II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  208.     II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  209.     II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  210.     II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */

  211.     state[0] += a;
  212.     state[1] += b;
  213.     state[2] += c;
  214.     state[3] += d;

  215.     //把x所指向的内存块清0
  216.     MD5_memset ((POINTER)x, 0, sizeof (x));
  217. }

  218. /**
  219.  *功能描述: 把输入的UINT4数据编码后输出成unsigned char,假设长度len是4的倍数
  220.  *入口参数:
  221.  *            unsigned char *output;
  222.  *            UINT4 *input;
  223.  *            unsigned int len
  224.  *返回值: 无
  225.  */
  226. void MD5::innerEncode (unsigned char * output, UINT4 * input,unsigned int len)
  227. {
  228.     unsigned int i, j;

  229.     for (i = 0, j = 0; j < len; i++, j += 4)
  230.     {
  231.         output[j] = (unsigned char)(input[i] & 0xff);
  232.         output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  233.         output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  234.         output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  235.     }
  236. }

  237. /**
  238.  *功能描述: 把输入的unsigned char数据解码后输出成UINT4,假设长度len是4的倍数
  239.  *入口参数:
  240.  *            UINT4 *output;
  241.  *            unsigned char *input;
  242.  *            unsigned int len;
  243.  *返回值: 无
  244.  */
  245. void MD5::innerDecode (UINT4 * output, unsigned char * input, unsigned int len)
  246. {
  247.     unsigned int i, j;

  248.     for (i = 0, j = 0; j < len; i++, j += 4)
  249.     {
  250.         output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  251.                  (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  252.     }
  253. }

  254. /**
  255.  *功能描述:两个内存块数据之间复制
  256.  *入口参数:
  257.  *            POINTER output 要复制到的目的数据
  258.  *            POINTER input 要复制的源数据
  259.  *            unsigned int len 要复制的长度
  260.  *返回值: 无
  261.  */
  262. void MD5::MD5_memcpy (POINTER output, POINTER input, unsigned int len)
  263. {
  264.     unsigned int i;

  265.     for (i = 0; i < len; i++)
  266.         output[i] = input[i];
  267. }

  268. /**
  269.  *功能描述: 把一块内存全部初始化为某一个值
  270.  *入口参数:
  271.  *            POINTER output 要初始化的内存块
  272.  *            int value 初值
  273.  *            unsigned int len 要初始化的长度
  274.  *返回值: 无
  275.  */
  276. void MD5::MD5_memset (POINTER output, int value, unsigned int len)
  277. {
  278.     unsigned int i;

  279.     for (i = 0; i < len; i++)
  280.         ((char *)output)[i] = (char)value;
  281. }

  282. /**
  283.  * 功能描述: 供外部使用的加密函数
  284.  * 入口参数:
  285.  * unsigned char* p_pszDest
  286.  * const char* p_pszSrc
  287.  * 返回值: 无
  288.  */
  289. void MD5::encode(unsigned char* p_pszDest, const char* p_pszSrc)
  290. {
  291.     MD5_CTX context;
  292.     unsigned int len = strlen (p_pszSrc);

  293.     MD5Init (&context);
  294.     MD5Update (&context, (unsigned char* )p_pszSrc, len);
  295.     MD5Final (p_pszDest, &context);
  296. }

  297. void MD5::Hash64(const char* sInputUrl, char* sOutputDocID)
  298. {
  299.     char sLine[16];
  300.     char szResult[8];
  301.     memset(sLine, 0, 16);
  302.     
  303.     encode((unsigned char* )sLine, (const char* )sInputUrl);
  304.     memcpy(szResult, sLine, 8);

  305. #ifdef WIN32
  306.     __int64 ddwDocID = *((__int64 *)(szResult));
  307.     _i64toa(ddwDocID, sOutputDocID, 10); //转换为10进制数
  308. #else
  309.     long long ddwDocID = *((long long *)(szResult));
  310.     sprintf (sOutputDocID, "%llu", ddwDocID);
  311. #endif
  312. }

  313. void MD5::Hash64(const char* sInputUrl, long long& ddwDocID)
  314. {
  315.     char sLine[16];
  316.     char szResult[8];
  317.     memset(sLine, 0, 16);

  318.     encode((unsigned char* )sLine, (const char* )sInputUrl);
  319.     memcpy(szResult, sLine, 8);

  320.     ddwDocID = *((long long *)(szResult));
  321. }

  322. //#define __MD5_MAIN__
  323. #ifdef __MD5_MAIN__
  324. void main()
  325. {
  326.     unsigned char digest[16];
  327.     char *string = "hello world";

  328.     MD5 md5;
  329.     md5.encode(digest, string);

  330.     std::cout << "Before MD5: " << string << std::endl;
  331.     std::cout << "After MD5: " << digest << std::endl;
  332.  
  333. }
  334. #endif

阅读(666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~