开云手机版登录入口
标题: md5加密算法C++程序 [打印本页]
作者: 乐乐天 时间: 2004-11-26 20:11
标题: md5加密算法C++程序
#include "global.h"
2 V. w7 t# x5 y1 Q" o6 M% z
#include "md5.h"
7 D# V% N' m! E f/ P1 J
( |2 P1 o( t# ]8 U
/* Constants for MD5Transform routine.
*/
! U- @1 q1 Z! y/ \- S* Q3 h* x0 L
- ]& ?' e% L3 E
3 a( R' ?* d1 \8 z6 {
#define S11 7
5 x5 n9 q7 N9 [5 l- u
#define S12 12
3 d- m, {1 {" \& ^* e#define S13 17
; c& q8 \! f, Q#define S14 22
: Y- d# Y3 a& g/ N#define S21 5
& `! x& h$ \* W/ V6 ?) o5 ?3 ?' l$ g#define S22 9
* _, `" p0 S! [: T3 ]1 x
#define S23 14
- c) ], n; J9 i* L: B2 @#define S24 20
: a: X- _1 z( O#define S31 4
( ?' }. f! I5 @. q#define S32 11
# x: O! s+ k) J1 m7 ]+ ~/ F1 k#define S33 16
6 l! Z+ P' i6 Y1 s3 j0 K8 a: l: d
#define S34 23
9 ?2 v' L! I/ Q* S
#define S41 6
. T6 [, v8 @4 Q) I5 I
#define S42 10
. \8 {' h) ?. j6 o3 h6 x. Y/ S
#define S43 15
$ r: ^; z1 n: M* G' G5 Z& g% J1 J; p
#define S44 21
/ }: s6 a3 r* H( a
7 U% O* R" K! C; B$ l/ _
static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 w* d3 f0 M, z- w3 Fstatic void Encode PROTO_LIST
((unsigned char *, UINT4 *, unsigned int));
2 U& S; Q1 d4 ]1 v. `$ T. R/ m
static void Decode PROTO_LIST
((UINT4 *, unsigned char *, unsigned int));
( O! ~: Z% T/ D7 U1 Jstatic void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
; Y3 R$ y. l: H) t+ e3 Bstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
# I8 G, z) T# |+ A; [5 @( O
' w! s6 S3 ^! _# ^0 Y
static unsigned char 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
" g3 a$ S* P, e+ t: I( u. l( l
};
! c& ]: o, s7 X; g' {) E8 X
* U1 {2 K8 i. h/ b
/* F, G, H and I are basic MD5 functions.
*/
1 L( R( |: r) b* p, a! P#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
. k4 K" w. N2 P: f( `+ y) [: K
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
' m% f) V% o- r7 e* b
#define H(x, y, z) ((x) ^ (y) ^ (z))
( _6 I8 X! q' u% g1 |5 I! |: ~
#define I(x, y, z) ((y) ^ ((x) | (~z)))
m2 N: [+ t4 i' h8 u
" G; i4 @1 P6 X) u- R. K/* ROTATE_LEFT rotates x left n bits.
*/
1 U- r- R3 `/ F4 w, g1 n% t
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
. o" @" J, b/ q% y3 U! V1 O( W8 J
+ n8 c3 ^- Y' m/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+ V: W) J9 h0 x5 pRotation is separate from addition to prevent recomputation.
*/
) R# z' r6 @; c) t4 G) O+ f# v8 @#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
' |+ e! n2 }1 k; \4 n6 k2 Y" l& c
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
- P/ T' S, w3 a! c) y! u
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
5 j: @' b( r: w: c% ]0 G& G
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/ W$ r# [+ y1 m3 Y2 | F' Z9 |
7 _( ^( T9 K# D/ n
/* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
8 S6 l! M! ~& k. I }$ @5 Q( Xvoid MD5Init (context)
; S/ ~- a& C# s( n7 @) _MD5_CTX *context; /* context */
3 t [( ^) B" B/ J- N9 u{
context->count[0] = context->count[1] = 0;
/* Load magic initialization constants.
& n5 S2 N0 x- b) l" }*/
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
6 C6 q h- I7 a Z' V
}
- B2 u" o+ w" d. v; ~8 O7 y. o
5 Q! z3 t# y, ]% V J9 N
/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
( E$ }$ _7 _# A+ |3 Cvoid MD5Update (context, input, inputLen)
- v# Q/ F$ O0 J/ @# x# e9 X; ~& {MD5_CTX *context; /* context */
. V- `+ I& @! X8 X. Punsigned char *input; /* input block */
- a8 E* Y- D( I) k( j% z; A" m- C% L/ munsigned int inputLen; /* length of input block */
$ x. |" y# ]9 S0 V& v# q6 ~3 ]4 B{
unsigned int i, index, partLen;
; o; `7 D- x; g' q. f+ Q
/* Compute number of bytes mod 64 */
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
5 _% [- ]; `/ C8 m/ a7 J
/* Update number of bits */
if ((context->count[0] += ((UINT4)inputLen << 3))
&
作者: guoyun786 时间: 2004-11-26 20:22
标题: md5加密算法C++程序
- U' x% E! c5 c/ I
3 o3 B8 M$ a- ], T: Q
| 欢迎光临 开云手机版登录入口 (/) |
Powered by Discuz! X3.4 |