📄 md5.c
字号:
/***************************************************************************** ** HPACK Multi-System Archiver ** =========================== ** ** MD5 Message Digest Code ** MD5.C Updated 02/10/91 ** ** This program is protected by copyright and as such any use or copying of ** this code for your own purposes directly or indirectly is highly uncool ** and if you do so there will be....trubble. ** And remember: We know where your kids go to school. ** ** Copyright 1991 Peter C.Gutmann. All rights reserved ** *****************************************************************************/#include <stdlib.h>#include <string.h>#include "md5.h"/***************************************************************************** ** Implementation RSA Data Security, Inc. MD5 Message-Digest Algorithm ** Created 2/17/90 RLR, revised 1/91 SRD,AJ,BSK,JT Reference C Version ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved ** *****************************************************************************//* Define the following to use the assembly-language version of MDTransform(). This is desirable since MDTransform() uses many rotates which cannot be easily represented in C and must be done inefficiently via shifts */#ifdef __MSDOS__ #define ASM_MD5#endif /* __MSDOS__ */#ifdef ASM_MD5 void MD5Transform( LONG *buf, LONG *in );#endif /* ASM_MD5 *//* To form the message digest for a message M, initialize a context buffer mdContext using MD5Init(); call MD5Update() on mdContext and M; and call MD5Final() on mdContext. The message digest is now in mdContext->digest[ 0 ... 15 ] *//***************************************************************************** ** The MD5 Transformation ** *****************************************************************************//* The Mysterious Constants used in the MD5 transformation */static LONG md5const[] = { 3614090360L, 3905402710L, 606105819L, 3250441966L, 4118548399L, 1200080426L, 2821735955L, 4249261313L, 1770035416L, 2336552879L, 4294925233L, 2304563134L, 1804603682L, 4254626195L, 2792965006L, 1236535329L, 4129170786L, 3225465664L, 643717713L, 3921069994L, 3593408605L, 38016083L, 3634488961L, 3889429448L, 568446438L, 3275163606L, 4107603335L, 1163531501L, 2850285829L, 4243563512L, 1735328473L, 2368359562L, 4294588738L, 2272392833L, 1839030562L, 4259657740L, 2763975236L, 1272893353L, 4139469664L, 3200236656L, 681279174L, 3936430074L, 3572445317L, 76029189L, 3654602809L, 3873151461L, 530742520L, 3299628645L, 4096336452L, 1126891415L, 2878612391L, 4237533241L, 1700485571L, 2399980690L, 4293915773L, 2240044497L, 1873313359L, 4264355552L, 2734768916L, 1309151649L, 4149444226L, 3174756917L, 718787259L, 3951481745L };/* Storage for the Mysterious Constants (either md5const or user-defined) */LONG mConst[ MD5_ROUNDS ];#ifndef ASM_MD5/* F, G, H and I are basic MD5 functions */#define F(X,Y,Z) ( ( X & Y ) | ( ~X & Z ) )#define G(X,Y,Z) ( ( X & Z ) | ( Y & ~Z ) )#define H(X,Y,Z) ( X ^ Y ^ Z )#define I(X,Y,Z) ( Y ^ ( X | ~Z ) )/* ROTATE_LEFT rotates x left n bits */#define ROTATE_LEFT(x,n) ( ( x << n ) | ( x >> ( 32 - n ) ) )/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation */#define FF(A,B,C,D,X,shiftAmt,magicConst) \ { \ A += F( B, C, D ) + X + magicConst; \ A = ROTATE_LEFT( A, shiftAmt ); \ A += B; \ }#define GG(A,B,C,D,X,shiftAmt,magicConst) \ { \ A += G( B, C, D ) + X + magicConst; \ A = ROTATE_LEFT( A, shiftAmt ); \ A += B; \ }#define HH(A,B,C,D,X,shiftAmt,magicConst) \ { \ A += H( B, C, D ) + X + magicConst; \ A = ROTATE_LEFT( A, shiftAmt ); \ A += B; \ }#define II(A,B,C,D,X,shiftAmt,magicConst) \ { \ A += I( B, C, D ) + X + magicConst; \ A = ROTATE_LEFT( A, shiftAmt ); \ A += B; \ }/* Round 1 shift amounts */#define S11 7#define S12 12#define S13 17#define S14 22/* Round 2 shift amounts */#define S21 5#define S22 9#define S23 14#define S24 20/* Round 3 shift amounts */#define S31 4#define S32 11#define S33 16#define S34 23/* Round 4 shift amounts */#define S41 6#define S42 10#define S43 15#define S44 21/* Basic MD5 step. Transforms buf based on in. Note that if the Mysterious Constants are arranged backwards in little-endian order and decrypted with the DES they produce OCCULT MESSAGES! */#if defined( __ARC__ ) || defined( IRIX ) || defined( __TSC__ )/* MD5Transform is split into its component rounds since many optimizers choke on the grand unified version */static void MD5TransformRound1( LONG *A, LONG *B, LONG *C, LONG *D, LONG *in ) { /* Round 1 */ FF( *A, *B, *C, *D, in[ 0 ], S11, mConst[ 0 ] ); /* 1 */ FF( *D, *A, *B, *C, in[ 1 ], S12, mConst[ 1 ] ); /* 2 */ FF( *C, *D, *A, *B, in[ 2 ], S13, mConst[ 2 ] ); /* 3 */ FF( *B, *C, *D, *A, in[ 3 ], S14, mConst[ 3 ] ); /* 4 */ FF( *A, *B, *C, *D, in[ 4 ], S11, mConst[ 4 ] ); /* 5 */ FF( *D, *A, *B, *C, in[ 5 ], S12, mConst[ 5 ] ); /* 6 */ FF( *C, *D, *A, *B, in[ 6 ], S13, mConst[ 6 ] ); /* 7 */ FF( *B, *C, *D, *A, in[ 7 ], S14, mConst[ 7 ] ); /* 8 */ FF( *A, *B, *C, *D, in[ 8 ], S11, mConst[ 8 ] ); /* 9 */ FF( *D, *A, *B, *C, in[ 9 ], S12, mConst[ 9 ] ); /* 10 */ FF( *C, *D, *A, *B, in[ 10 ], S13, mConst[ 10 ] ); /* 11 */ FF( *B, *C, *D, *A, in[ 11 ], S14, mConst[ 11 ] ); /* 12 */ FF( *A, *B, *C, *D, in[ 12 ], S11, mConst[ 12 ] ); /* 13 */ FF( *D, *A, *B, *C, in[ 13 ], S12, mConst[ 13 ] ); /* 14 */ FF( *C, *D, *A, *B, in[ 14 ], S13, mConst[ 14 ] ); /* 15 */ FF( *B, *C, *D, *A, in[ 15 ], S14, mConst[ 15 ] ); /* 16 */ }static void MD5TransformRound2( LONG *A, LONG *B, LONG *C, LONG *D, LONG *in ) { /* Round 2 */ GG( *A, *B, *C, *D, in[ 1 ], S21, mConst[ 16 ] ); /* 17 */ GG( *D, *A, *B, *C, in[ 6 ], S22, mConst[ 17 ] ); /* 18 */ GG( *C, *D, *A, *B, in[ 11 ], S23, mConst[ 18 ] ); /* 19 */ GG( *B, *C, *D, *A, in[ 0 ], S24, mConst[ 19 ] ); /* 20 */ GG( *A, *B, *C, *D, in[ 5 ], S21, mConst[ 20 ] ); /* 21 */ GG( *D, *A, *B, *C, in[ 10 ], S22, mConst[ 21 ] ); /* 22 */ GG( *C, *D, *A, *B, in[ 15 ], S23, mConst[ 22 ] ); /* 23 */ GG( *B, *C, *D, *A, in[ 4 ], S24, mConst[ 23 ] ); /* 24 */ GG( *A, *B, *C, *D, in[ 9 ], S21, mConst[ 24 ] ); /* 25 */ GG( *D, *A, *B, *C, in[ 14 ], S22, mConst[ 25 ] ); /* 26 */ GG( *C, *D, *A, *B, in[ 3 ], S23, mConst[ 26 ] ); /* 27 */ GG( *B, *C, *D, *A, in[ 8 ], S24, mConst[ 27 ] ); /* 28 */ GG( *A, *B, *C, *D, in[ 13 ], S21, mConst[ 28 ] ); /* 29 */ GG( *D, *A, *B, *C, in[ 2 ], S22, mConst[ 29 ] ); /* 30 */ GG( *C, *D, *A, *B, in[ 7 ], S23, mConst[ 30 ] ); /* 31 */ GG( *B, *C, *D, *A, in[ 12 ], S24, mConst[ 31 ] ); /* 32 */ }static void MD5TransformRound3( LONG *A, LONG *B, LONG *C, LONG *D, LONG *in ) { /* Round 3 */ HH( *A, *B, *C, *D, in[ 5 ], S31, mConst[ 32 ] ); /* 33 */ HH( *D, *A, *B, *C, in[ 8 ], S32, mConst[ 33 ] ); /* 34 */ HH( *C, *D, *A, *B, in[ 11 ], S33, mConst[ 34 ] ); /* 35 */ HH( *B, *C, *D, *A, in[ 14 ], S34, mConst[ 35 ] ); /* 36 */ HH( *A, *B, *C, *D, in[ 1 ], S31, mConst[ 36 ] ); /* 37 */ HH( *D, *A, *B, *C, in[ 4 ], S32, mConst[ 37 ] ); /* 38 */ HH( *C, *D, *A, *B, in[ 7 ], S33, mConst[ 38 ] ); /* 39 */ HH( *B, *C, *D, *A, in[ 10 ], S34, mConst[ 39 ] ); /* 40 */ HH( *A, *B, *C, *D, in[ 13 ], S31, mConst[ 40 ] ); /* 41 */ HH( *D, *A, *B, *C, in[ 0 ], S32, mConst[ 41 ] ); /* 42 */ HH( *C, *D, *A, *B, in[ 3 ], S33, mConst[ 42 ] ); /* 43 */ HH( *B, *C, *D, *A, in[ 6 ], S34, mConst[ 43 ] ); /* 44 */ HH( *A, *B, *C, *D, in[ 9 ], S31, mConst[ 44 ] ); /* 45 */ HH( *D, *A, *B, *C, in[ 12 ], S32, mConst[ 45 ] ); /* 46 */ HH( *C, *D, *A, *B, in[ 15 ], S33, mConst[ 46 ] ); /* 47 */ HH( *B, *C, *D, *A, in[ 2 ], S34, mConst[ 47 ] ); /* 48 */ }static void MD5TransformRound4( LONG *A, LONG *B, LONG *C, LONG *D, LONG *in ) { /* Round 4 */ II( *A, *B, *C, *D, in[ 0 ], S41, mConst[ 48 ] ); /* 49 */ II( *D, *A, *B, *C, in[ 7 ], S42, mConst[ 49 ] ); /* 50 */ II( *C, *D, *A, *B, in[ 14 ], S43, mConst[ 50 ] ); /* 51 */ II( *B, *C, *D, *A, in[ 5 ], S44, mConst[ 51 ] ); /* 52 */ II( *A, *B, *C, *D, in[ 12 ], S41, mConst[ 52 ] ); /* 53 */ II( *D, *A, *B, *C, in[ 3 ], S42, mConst[ 53 ] ); /* 54 */ II( *C, *D, *A, *B, in[ 10 ], S43, mConst[ 54 ] ); /* 55 */ II( *B, *C, *D, *A, in[ 1 ], S44, mConst[ 55 ] ); /* 56 */ II( *A, *B, *C, *D, in[ 8 ], S41, mConst[ 56 ] ); /* 57 */ II( *D, *A, *B, *C, in[ 15 ], S42, mConst[ 57 ] ); /* 58 */ II( *C, *D, *A, *B, in[ 6 ], S43, mConst[ 58 ] ); /* 59 */ II( *B, *C, *D, *A, in[ 13 ], S44, mConst[ 59 ] ); /* 60 */ II( *A, *B, *C, *D, in[ 4 ], S41, mConst[ 60 ] ); /* 61 */ II( *D, *A, *B, *C, in[ 11 ], S42, mConst[ 61 ] ); /* 62 */ II( *C, *D, *A, *B, in[ 2 ], S43, mConst[ 62 ] ); /* 63 */ II( *B, *C, *D, *A, in[ 9 ], S44, mConst[ 63 ] ); /* 64 */ }void MD5Transform( LONG *buf, LONG *in ) { LONG A = buf[ 0 ], B = buf[ 1 ], C = buf[ 2 ], D = buf[ 3 ]; MD5TransformRound1 (&A, &B, &C, &D, in); MD5TransformRound2 (&A, &B, &C, &D, in); MD5TransformRound3 (&A, &B, &C, &D, in); MD5TransformRound4 (&A, &B, &C, &D, in); buf[ 0 ] += A; buf[ 1 ] += B; buf[ 2 ] += C; buf[ 3 ] += D; }#elsevoid MD5Transform( LONG *buf, LONG *in ) { LONG A = buf[ 0 ], B = buf[ 1 ], C = buf[ 2 ], D = buf[ 3 ]; /* Round 1 */ FF( A, B, C, D, in[ 0 ], S11, mConst[ 0 ] ); /* 1 */ FF( D, A, B, C, in[ 1 ], S12, mConst[ 1 ] ); /* 2 */ FF( C, D, A, B, in[ 2 ], S13, mConst[ 2 ] ); /* 3 */ FF( B, C, D, A, in[ 3 ], S14, mConst[ 3 ] ); /* 4 */ FF( A, B, C, D, in[ 4 ], S11, mConst[ 4 ] ); /* 5 */ FF( D, A, B, C, in[ 5 ], S12, mConst[ 5 ] ); /* 6 */ FF( C, D, A, B, in[ 6 ], S13, mConst[ 6 ] ); /* 7 */ FF( B, C, D, A, in[ 7 ], S14, mConst[ 7 ] ); /* 8 */ FF( A, B, C, D, in[ 8 ], S11, mConst[ 8 ] ); /* 9 */ FF( D, A, B, C, in[ 9 ], S12, mConst[ 9 ] ); /* 10 */ FF( C, D, A, B, in[ 10 ], S13, mConst[ 10 ] ); /* 11 */ FF( B, C, D, A, in[ 11 ], S14, mConst[ 11 ] ); /* 12 */ FF( A, B, C, D, in[ 12 ], S11, mConst[ 12 ] ); /* 13 */ FF( D, A, B, C, in[ 13 ], S12, mConst[ 13 ] ); /* 14 */ FF( C, D, A, B, in[ 14 ], S13, mConst[ 14 ] ); /* 15 */ FF( B, C, D, A, in[ 15 ], S14, mConst[ 15 ] ); /* 16 */ /* Round 2 */ GG( A, B, C, D, in[ 1 ], S21, mConst[ 16 ] ); /* 17 */ GG( D, A, B, C, in[ 6 ], S22, mConst[ 17 ] ); /* 18 */ GG( C, D, A, B, in[ 11 ], S23, mConst[ 18 ] ); /* 19 */ GG( B, C, D, A, in[ 0 ], S24, mConst[ 19 ] ); /* 20 */ GG( A, B, C, D, in[ 5 ], S21, mConst[ 20 ] ); /* 21 */ GG( D, A, B, C, in[ 10 ], S22, mConst[ 21 ] ); /* 22 */ GG( C, D, A, B, in[ 15 ], S23, mConst[ 22 ] ); /* 23 */ GG( B, C, D, A, in[ 4 ], S24, mConst[ 23 ] ); /* 24 */ GG( A, B, C, D, in[ 9 ], S21, mConst[ 24 ] ); /* 25 */ GG( D, A, B, C, in[ 14 ], S22, mConst[ 25 ] ); /* 26 */ GG( C, D, A, B, in[ 3 ], S23, mConst[ 26 ] ); /* 27 */ GG( B, C, D, A, in[ 8 ], S24, mConst[ 27 ] ); /* 28 */ GG( A, B, C, D, in[ 13 ], S21, mConst[ 28 ] ); /* 29 */ GG( D, A, B, C, in[ 2 ], S22, mConst[ 29 ] ); /* 30 */ GG( C, D, A, B, in[ 7 ], S23, mConst[ 30 ] ); /* 31 */ GG( B, C, D, A, in[ 12 ], S24, mConst[ 31 ] ); /* 32 */ /* Round 3 */ HH( A, B, C, D, in[ 5 ], S31, mConst[ 32 ] ); /* 33 */ HH( D, A, B, C, in[ 8 ], S32, mConst[ 33 ] ); /* 34 */ HH( C, D, A, B, in[ 11 ], S33, mConst[ 34 ] ); /* 35 */ HH( B, C, D, A, in[ 14 ], S34, mConst[ 35 ] ); /* 36 */ HH( A, B, C, D, in[ 1 ], S31, mConst[ 36 ] ); /* 37 */ HH( D, A, B, C, in[ 4 ], S32, mConst[ 37 ] ); /* 38 */ HH( C, D, A, B, in[ 7 ], S33, mConst[ 38 ] ); /* 39 */ HH( B, C, D, A, in[ 10 ], S34, mConst[ 39 ] ); /* 40 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -