金山开源MD5加密算法c++版.docx_第1页
金山开源MD5加密算法c++版.docx_第2页
金山开源MD5加密算法c++版.docx_第3页
金山开源MD5加密算法c++版.docx_第4页
金山开源MD5加密算法c++版.docx_第5页
免费预览已结束,剩余7页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

md5.h#ifndef MD5_H 02#define MD5_H 0304#ifdef _cplusplus 05extern C 06#endif 0708typedef unsigned int word32; 09#define MD5_SIZE 16 1011struct MD5Context 12word32 buf4; 13word32 bits2; 14unsigned char in64; 15; 1617void MD5Init(struct MD5Context *context); 18void MD5Update(struct MD5Context *context, unsigned char const *buf, 19unsigned len); 20void MD5Final(unsigned char digest16, struct MD5Context *context); 21void MD5Transform(word32 buf4, word32 const in16); 2223/* 24* This is needed to make RSAREF happy on some MS-DOS compilers. 25*/26typedef struct MD5Context MD5_CTX; 2728char* crypt_md5(const char* pw, const char* salt); 29void _crypt_to64(char* s,unsigned long v, int n); 3031#ifdef _cplusplus 32 33#endif 3435#endif /* !MD5_H */md5.c#include 002#include md5.h 003004#ifndef WORDS_BIGENDIAN 005#define byteReverse(buf, len) /* Nothing */ 006#else 007static void byteReverse(unsigned char *buf, unsigned longs); 008009/* 010* Note: this code is harmless on little-endian machines. 011*/012static void byteReverse(unsigned char *buf, unsigned longs) 013 014word32 t; 015do 016t = (word32) (unsigned) buf3 8 | buf2) 16 | 017(unsigned) buf1 buf0 = 0x67452301; 031ctx-buf1 = 0xefcdab89; 032ctx-buf2 = 0x98badcfe; 033ctx-buf3 = 0x10325476; 034035ctx-bits0 = 0; 036ctx-bits1 = 0; 037 038039/* 040* Update context to reflect the concatenation of another buffer full 041* of bytes. 042*/043void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) 044 045register word32 t; 046047/* Update bitcount */048049t = ctx-bits0; 050if (ctx-bits0 = t + (word32) len 3) bits1+; /* Carry from low to high */052ctx-bits1 += len 29; 053054t = (t 3) & 0x3f; /* Bytes already in shsInfo-data */055056/* Handle any leading odd-sized chunks */057058if (t) 059unsigned char *p = (unsigned char *) ctx-in + t; 060061t = 64 - t; 062if (len in, 16); 068MD5Transform(ctx-buf, (word32 *) ctx-in); 069buf += t; 070len -= t; 071 072/* Process data in 64-byte chunks */073074while (len = 64) 075memmove(ctx-in, buf, 64); 076byteReverse(ctx-in, 16); 077MD5Transform(ctx-buf, (word32 *) ctx-in); 078buf += 64; 079len -= 64; 080 081082/* Handle any remaining bytes of data. */083084memmove(ctx-in, buf, len); 085 086087/* 088* Final wrapup - pad to 64-byte boundary with the bit pattern 089* 1 0* (64-bit count of bits processed, MSB-first) 090*/091void MD5Final(unsigned char digest16, struct MD5Context *ctx) 092 093unsigned int count; 094unsigned char *p; 095096/* Compute number of bytes mod 64 */097count = (ctx-bits0 3) & 0x3F; 098099/* Set the first char of padding to 0x80. This is safe since there is 100always at least one byte free */101p = ctx-in + count; 102*p+ = 0x80; 103104/* Bytes of padding needed to make 64 bytes */105count = 64 - 1 - count; 106107/* Pad out to 56 mod 64 */108if (count in, 16); 112MD5Transform(ctx-buf, (word32 *) ctx-in); 113114/* Now fill the next block with 56 bytes */115memset(ctx-in, 0, 56); 116 else 117/* Pad block to 56 bytes */118memset(p, 0, count - 8); 119 120byteReverse(ctx-in, 14); 121122/* Append length in bits and transform */123(word32 *) ctx-in)14 = ctx-bits0; 124(word32 *) ctx-in)15 = ctx-bits1; 125126MD5Transform(ctx-buf, (word32 *) ctx-in); 127byteReverse(unsigned char *) ctx-buf, 4); 128memmove(digest, ctx-buf, 16); 129memset(ctx, 0, sizeof(ctx); /* In case its sensitive */130 131132/* The four core functions - F1 is optimized somewhat */133134/* #define F1(x, y, z) (x & y | x & z) */135#define F1(x, y, z) (z (x & (y z) 136#define F2(x, y, z) F1(z, x, y) 137#define F3(x, y, z) (x y z) 138#define F4(x, y, z) (y (x | z) 139140/* This is the central step in the MD5 algorithm. */141#define MD5STEP(f, w, x, y, z, data, s) 142( w += f(x, y, z) + data, w = w(32-s), w += x ) 143144/* 145* The core of the MD5 algorithm, this alters an existing MD5 hash to 146* reflect the addition of 16 longwords of new data. MD5Update blocks 147* the data and converts bytes into longwords for this routine. 148*/149void MD5Transform(word32 buf4, word32 const in16) 150 151register word32 a, b, c, d; 152153a = buf0; 154b = buf1; 155c = buf2; 156d = buf3; 157158MD5STEP(F1, a, b, c, d, in0 + 0xd76aa478, 7); 159MD5STEP(F1, d, a, b, c, in1 + 0xe8c7b756, 12); 160MD5STEP(F1, c, d, a, b, in2 + 0x242070db, 17); 161MD5STEP(F1, b, c, d, a, in3 + 0xc1bdceee, 22); 162MD5STEP(F1, a, b, c, d, in4 + 0xf57c0faf, 7); 163MD5STEP(F1, d, a, b, c, in5 + 0x4787c62a, 12); 164MD5STEP(F1, c, d, a, b, in6 + 0xa8304613, 17); 165MD5STEP(F1, b, c, d, a, in7 + 0xfd469501, 22); 166MD5STEP(F1, a, b, c, d, in8 + 0x698098d8, 7); 167MD5STEP(F1, d, a, b, c, in9 + 0x8b44f7af, 12); 168MD5STEP(F1, c, d, a, b, in10 + 0xffff5bb1, 17); 169MD5STEP(F1, b, c, d, a, in11 + 0x895cd7be, 22); 170MD5STEP(F1, a, b, c, d, in12 + 0x6b901122, 7); 171MD5STEP(F1, d, a, b, c, in13 + 0xfd987193, 12); 172MD5STEP(F1, c, d, a, b, in14 + 0xa679438e, 17); 173MD5STEP(F1, b, c, d, a, in15 + 0x49b40821, 22); 174175MD5STEP(F2, a, b, c, d, in1 + 0xf61e2562, 5); 176MD5STEP(F2, d, a, b, c, in6 + 0xc040b340, 9); 177MD5STEP(F2, c, d, a, b, in11 + 0x265e5a51, 14); 178MD5STEP(F2, b, c, d, a, in0 + 0xe9b6c7aa, 20); 179MD5STEP(F2, a, b, c, d, in5 + 0xd62f105d, 5); 180MD5STEP(F2, d, a, b, c, in10 + 0x02441453, 9); 181MD5STEP(F2, c, d, a, b, in15 + 0xd8a1e681, 14); 182MD5STEP(F2, b, c, d, a, in4 + 0xe7d3fbc8, 20); 183MD5STEP(F2, a, b, c, d, in9 + 0x21e1cde6, 5); 184MD5STEP(F2, d, a, b, c, in14 + 0xc33707d6, 9); 185MD5STEP(F2, c, d, a, b, in3 + 0xf4d50d87, 14); 186MD5STEP(F2, b, c, d, a, in8 + 0x455a14ed, 20); 187MD5STEP(F2, a, b, c, d, in13 + 0xa9e3e905, 5); 188MD5STEP(F2, d, a, b, c, in2 + 0xfcefa3f8, 9); 189MD5STEP(F2, c, d, a, b, in7 + 0x676f02d9, 14); 190MD5STEP(F2, b, c, d, a, in12 + 0x8d2a4c8a, 20); 191192MD5STEP(F3, a, b, c, d, in5 + 0xfffa3942, 4); 193MD5STEP(F3, d, a, b, c, in8 + 0x8771f681, 11); 194MD5STEP(F3, c, d, a, b, in11 + 0x6d9d6122, 16); 195MD5STEP(F3, b, c, d, a, in14 + 0xfde5380c, 23); 196MD5STEP(F3, a, b, c, d, in1 + 0xa4beea44, 4); 197MD5STEP(F3, d, a, b, c, in4 + 0x4bdecfa9, 11); 198MD5STEP(F3, c, d, a, b, in7 + 0xf6bb4b60, 16); 199MD5STEP(F3, b, c, d, a, in10 + 0xbebfbc70, 23); 200MD5STEP(F3, a, b, c, d, in13 + 0x289b7ec6, 4); 201MD5STEP(F3, d, a, b, c, in0 + 0xeaa127fa, 11); 202MD5STEP(F3, c, d, a, b, in3 + 0xd4ef3085, 16); 203MD5STEP(F3, b, c, d, a, in6 + 0x04881d05, 23); 204MD5STEP(F3, a, b, c, d, in9 + 0xd9d4d039, 4); 205MD5STEP(F3, d, a, b, c, in12 + 0xe6db99e5, 11); 206MD5STEP(F3, c, d, a, b, in15 + 0x1fa27cf8, 16); 207MD5STEP(F3, b, c, d, a, in2 + 0xc4ac5665, 23); 208209MD5STEP(F4, a, b, c, d, in0 + 0xf4292244, 6); 210MD5STEP(F4, d, a, b, c, in7 + 0x432aff97, 10); 211MD5STEP(F4, c, d, a, b, in14 + 0xab9423a7, 15); 212MD5STEP(F4, b, c, d, a, in5 + 0xfc93a039, 21); 213MD5STEP(F4, a, b, c, d, in12 + 0x655b59c3, 6); 214MD5STEP(F4, d, a, b, c, in3 + 0x8f0ccc92, 10); 215MD5STEP(F4, c, d, a, b, in10 + 0xffeff47d, 15); 216MD5STEP(F4, b, c, d, a, in1 + 0x85845dd1, 21); 217MD5STEP(F4, a, b, c, d, in8 + 0x6fa87e4f, 6); 218MD5STEP(F4, d, a, b, c, in15 + 0xfe2ce6e0, 10); 219MD5STEP(F4, c, d, a, b, in6 + 0xa3014314, 15); 220MD5STEP(F4, b, c, d, a, in13 + 0x4e0811a1, 21); 221MD5STEP(F4, a, b, c, d, in4 + 0xf7537e82, 6); 222MD5STEP(F4, d, a, b, c, in11 + 0xbd3af235, 10); 223MD5STEP(F4, c, d, a, b, in2 + 0x2ad7d2bb, 15); 224MD5STEP(F4, b, c, d, a, in9 + 0xeb86d391, 21); 225226buf0 += a; 227buf1 += b; 228buf2 += c; 229buf3 += d; 230 231232char* crypt_md5(const char* pw, const char* salt) 233 234static char *magic = $1$; /* 235* This string is magic for 236* this algorithm. Having 237* it this way, we can get 238* get better later on 239*/240static char passwd120, *p; 241static const char *sp,*ep; 242unsigned char finalMD5_SIZE; 243int sl,pl,i; 244MD5_CTX ctx,ctx1; 245unsigned long l; 246247/* Refine the Salt first */248sp = salt; 249250/* If it starts with the magic string, then skip that */251if(!strncmp(sp,magic,strlen(magic) 252sp += strlen(magic); 253254/* It stops at the first $, max 8 chars */255for(ep=sp;*ep & *ep != $ & ep 0; pl -= MD5_SIZE) 279MD5Update(&ctx,final,plMD5_SIZE ? MD5_SIZE : pl); 280281/* Dont leave anything around in vm they could use. */282memset(final,0,sizeof final); 283284/* Then something really weird. */285for (i = strlen(pw); i ; i = 1) 286if(i&1) 287MD5Update(&ctx, final, 1); 288else289MD5Update(&ctx, pw, 1); 290291/* Now make the output string */292strcpy(passwd,magic); 293strncat(passwd,sp,sl); 294strcat(passwd,$); 295296MD5Final(final,&ctx); 297298/* 299* and now, just to make sure things dont run too fast 300* On a 60 Mhz Pentium this takes 34 msec, so you would 301* need 30 se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论