📄 md5main.cpp
字号:
/*===========================================================================*
* INCLUDE FILES
*============================================================================*/
#include <memory.h> /* for memcpy() */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <bios.h>
#include <time.h>
#include <io.h>
#include <fcntl.h>
#include <ctype.h>
#include "md5.h"
#include "uti.h"
#define _md5_owner 1
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long uint32;
union int_or_char{
int i;
uchar buf[2];
}anumber;
/*===========================================================================*
* DEFINITIONS & MACROS LOCAL TO THIS FILE
=============================================================================*/
/* MD5 context. */
typedef struct
{
uint32 iState[ 4 ]; /* state (ABCD) */
uint32 iBits[ 2 ]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char cInBuf[ 64 ]; /* input buffer */
} Md5_tContext;
void
Md5_vInit ( Md5_tContext *pCtx ); /* ptr to context */
void
Md5_vUpdate ( Md5_tContext *pCtx, /* ptr to context */
unsigned char *pBuf, /* ptr to working buffer */
unsigned iLen ); /* length of buffer */
void
Md5_vFinal ( Md5_tContext *pCtx ); /* ptr to context */
void
Md5_vTransform ( uint32 *pState, /* ptr to state */
uint32 *pIn ); /* ptr to working buffer */
unsigned char Md5_cPasswordDigest[ 16 ]; /* digest store */
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
* initialization constants.
*/
void
Md5_vInit ( Md5_tContext *pCtx ) /* ptr to context */
{
pCtx->iState[ 0 ] = 0x67452301;
pCtx->iState[ 1 ] = 0xefcdab89;
pCtx->iState[ 2 ] = 0x98badcfe;
pCtx->iState[ 3 ] = 0x10325476;
pCtx->iBits[ 0 ] = 0;
pCtx->iBits[ 1 ] = 0;
}
/*
* Update context to reflect the concatenation of another buffer full
* of bytes.
*/
void
Md5_vUpdate ( Md5_tContext *pCtx, /* ptr to context */
unsigned char *pBuf, /* ptr to working buffer */
unsigned iLen ) /* length of buffer */
{
uint32 t;
/* Update bitcount */
t = pCtx->iBits[ 0 ];
if ((pCtx->iBits[ 0 ] = t + ( ( uint32 )iLen << 3 ) ) < t )
{
pCtx->iBits[ 1 ]++; /* Carry from low to high */
}
pCtx->iBits[ 1 ] += iLen >> 29;
t = ( t >> 3 ) & 0x3f; /* Bytes already in shsInfo->data */
/* Handle any leading odd-sized chunks */
if ( t )
{
unsigned char *p = ( unsigned char * )pCtx->cInBuf + t;
t = 64 - t;
if ( iLen < t )
{
memcpy( p, pBuf, iLen );
return;
}
memcpy( p, pBuf, t );
Md5_vTransform( pCtx->iState, ( uint32 * )pCtx->cInBuf );
pBuf += t;
iLen -= t;
}
/* Process data in 64-byte chunks */
while ( iLen >= 64 )
{
memcpy( pCtx->cInBuf, pBuf, 64 );
Md5_vTransform( pCtx->iState, ( uint32 * )pCtx->cInBuf );
pBuf += 64;
iLen -= 64;
}
/* Handle any remaining bytes of data. */
memcpy( pCtx->cInBuf, pBuf, iLen );
}
/*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
void
Md5_vFinal ( Md5_tContext *pCtx ) /* ptr to context */
{
unsigned iCount;
unsigned char *pC;
/* Compute number of bytes mod 64 */
iCount = ( pCtx->iBits[ 0 ] >> 3 ) & 0x3F;
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
pC = pCtx->cInBuf + iCount;
*pC++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
iCount = 64 - 1 - iCount;
/* Pad out to 56 mod 64 */
if ( iCount < 8 )
{
/* Two lots of padding: Pad the first block to 64 bytes */
memset( pC, 0, iCount );
Md5_vTransform( pCtx->iState, ( uint32 * ) pCtx->cInBuf );
/* Now fill the next block with 56 bytes */
memset( pCtx->cInBuf, 0, 56 );
}
else
{
/* Pad block to 56 bytes */
memset( pC, 0, iCount - 8 );
}
/* Append length in bits and transform */
( ( uint32 * )pCtx->cInBuf)[ 14 ] = pCtx->iBits[ 0 ];
( ( uint32 * )pCtx->cInBuf)[ 15 ] = pCtx->iBits[ 1 ];
Md5_vTransform( pCtx->iState, ( uint32 * )pCtx->cInBuf );
memcpy( Md5_cPasswordDigest, pCtx->iState, 16 );
memset( pCtx, 0, sizeof( pCtx ) ); /* In case it's sensitive */
}
/*
* The core of the MD5 algorithm, this alters an existing MD5 hash to
* reflect the addition of 16 longwords of new data. Md5_vUpdate blocks
* the data and converts bytes into longwords for this routine.
*/
void
Md5_vTransform ( uint32 *pState, /* ptr to state */
uint32 *pIn ) /* ptr to working buffer */
{
/* The four core functions - mF1 is optimized somewhat */
/* #define mF1(x, y, z) (x & y | ~x & z) */
#define mF1( x, y, z ) ( z ^ ( x & ( y ^ z ) ) )
#define mF2( x, y, z ) mF1( z, x, y )
#define mF3( x, y, z ) ( x ^ y ^ z )
#define mF4( x, y, z ) ( y ^ ( x | ~z ) )
/* This is the central step in the MD5 algorithm. */
#define mStep( f, w, x, y, z, data, s ) { \
w += f( x, y, z ) + data; \
w = w << s | w>>( 32 - s ); \
w += x; \
}
register uint32 a, b, c, d;
a = pState[0];
b = pState[1];
c = pState[2];
d = pState[3];
mStep( mF1, a, b, c, d, pIn[ 0 ] + 0xd76aa478, 7 );
mStep( mF1, d, a, b, c, pIn[ 1 ] + 0xe8c7b756, 12 );
mStep( mF1, c, d, a, b, pIn[ 2 ] + 0x242070db, 17 );
mStep( mF1, b, c, d, a, pIn[ 3 ] + 0xc1bdceee, 22 );
mStep( mF1, a, b, c, d, pIn[ 4 ] + 0xf57c0faf, 7 );
mStep( mF1, d, a, b, c, pIn[ 5 ] + 0x4787c62a, 12 );
mStep( mF1, c, d, a, b, pIn[ 6 ] + 0xa8304613, 17 );
mStep( mF1, b, c, d, a, pIn[ 7 ] + 0xfd469501, 22 );
mStep( mF1, a, b, c, d, pIn[ 8 ] + 0x698098d8, 7 );
mStep( mF1, d, a, b, c, pIn[ 9 ] + 0x8b44f7af, 12 );
mStep( mF1, c, d, a, b, pIn[ 10 ] + 0xffff5bb1, 17 );
mStep( mF1, b, c, d, a, pIn[ 11 ] + 0x895cd7be, 22 );
mStep( mF1, a, b, c, d, pIn[ 12 ] + 0x6b901122, 7 );
mStep( mF1, d, a, b, c, pIn[ 13 ] + 0xfd987193, 12 );
mStep( mF1, c, d, a, b, pIn[ 14 ] + 0xa679438e, 17 );
mStep( mF1, b, c, d, a, pIn[ 15 ] + 0x49b40821, 22 );
mStep( mF2, a, b, c, d, pIn[ 1 ] + 0xf61e2562, 5 );
mStep( mF2, d, a, b, c, pIn[ 6 ] + 0xc040b340, 9 );
mStep( mF2, c, d, a, b, pIn[ 11 ] + 0x265e5a51, 14 );
mStep( mF2, b, c, d, a, pIn[ 0 ] + 0xe9b6c7aa, 20 );
mStep( mF2, a, b, c, d, pIn[ 5 ] + 0xd62f105d, 5 );
mStep( mF2, d, a, b, c, pIn[ 10 ] + 0x02441453, 9 );
mStep( mF2, c, d, a, b, pIn[ 15 ] + 0xd8a1e681, 14 );
mStep( mF2, b, c, d, a, pIn[ 4 ] + 0xe7d3fbc8, 20 );
mStep( mF2, a, b, c, d, pIn[ 9 ] + 0x21e1cde6, 5 );
mStep( mF2, d, a, b, c, pIn[ 14 ] + 0xc33707d6, 9 );
mStep( mF2, c, d, a, b, pIn[ 3 ] + 0xf4d50d87, 14 );
mStep( mF2, b, c, d, a, pIn[ 8 ] + 0x455a14ed, 20 );
mStep( mF2, a, b, c, d, pIn[ 13 ] + 0xa9e3e905, 5 );
mStep( mF2, d, a, b, c, pIn[ 2 ] + 0xfcefa3f8, 9 );
mStep( mF2, c, d, a, b, pIn[ 7 ] + 0x676f02d9, 14 );
mStep( mF2, b, c, d, a, pIn[ 12 ] + 0x8d2a4c8a, 20 );
mStep( mF3, a, b, c, d, pIn[ 5 ] + 0xfffa3942, 4 );
mStep( mF3, d, a, b, c, pIn[ 8 ] + 0x8771f681, 11 );
mStep( mF3, c, d, a, b, pIn[ 11 ] + 0x6d9d6122, 16 );
mStep( mF3, b, c, d, a, pIn[ 14 ] + 0xfde5380c, 23 );
mStep( mF3, a, b, c, d, pIn[ 1 ] + 0xa4beea44, 4 );
mStep( mF3, d, a, b, c, pIn[ 4 ] + 0x4bdecfa9, 11 );
mStep( mF3, c, d, a, b, pIn[ 7 ] + 0xf6bb4b60, 16 );
mStep( mF3, b, c, d, a, pIn[ 10 ] + 0xbebfbc70, 23 );
mStep( mF3, a, b, c, d, pIn[ 13 ] + 0x289b7ec6, 4 );
mStep( mF3, d, a, b, c, pIn[ 0 ] + 0xeaa127fa, 11 );
mStep( mF3, c, d, a, b, pIn[ 3 ] + 0xd4ef3085, 16 );
mStep( mF3, b, c, d, a, pIn[ 6 ] + 0x04881d05, 23 );
mStep( mF3, a, b, c, d, pIn[ 9 ] + 0xd9d4d039, 4 );
mStep( mF3, d, a, b, c, pIn[ 12 ] + 0xe6db99e5, 11 );
mStep( mF3, c, d, a, b, pIn[ 15 ] + 0x1fa27cf8, 16 );
mStep( mF3, b, c, d, a, pIn[ 2 ] + 0xc4ac5665, 23 );
mStep( mF4, a, b, c, d, pIn[ 0 ] + 0xf4292244, 6 );
mStep( mF4, d, a, b, c, pIn[ 7 ] + 0x432aff97, 10 );
mStep( mF4, c, d, a, b, pIn[ 14 ] + 0xab9423a7, 15 );
mStep( mF4, b, c, d, a, pIn[ 5 ] + 0xfc93a039, 21 );
mStep( mF4, a, b, c, d, pIn[ 12 ] + 0x655b59c3, 6 );
mStep( mF4, d, a, b, c, pIn[ 3 ] + 0x8f0ccc92, 10 );
mStep( mF4, c, d, a, b, pIn[ 10 ] + 0xffeff47d, 15 );
mStep( mF4, b, c, d, a, pIn[ 1 ] + 0x85845dd1, 21 );
mStep( mF4, a, b, c, d, pIn[ 8 ] + 0x6fa87e4f, 6 );
mStep( mF4, d, a, b, c, pIn[ 15 ] + 0xfe2ce6e0, 10 );
mStep( mF4, c, d, a, b, pIn[ 6 ] + 0xa3014314, 15 );
mStep( mF4, b, c, d, a, pIn[ 13 ] + 0x4e0811a1, 21 );
mStep( mF4, a, b, c, d, pIn[ 4 ] + 0xf7537e82, 6 );
mStep( mF4, d, a, b, c, pIn[ 11 ] + 0xbd3af235, 10 );
mStep( mF4, c, d, a, b, pIn[ 2 ] + 0x2ad7d2bb, 15 );
mStep( mF4, b, c, d, a, pIn[ 9 ] + 0xeb86d391, 21 );
pState[ 0 ] += a;
pState[ 1 ] += b;
pState[ 2 ] += c;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -