📄 md5.cpp
字号:
//
// (C) 2000, Tsung-Fan Lin.
// MD5 algorithm implementation. Based on RFC1321.
// 2000/8/2 modified.
// Note: MD5 data is base on little-endian.
#include "stdafx.h"
#include <string.h>
#include "md5.h"
#define MACHINE_LITTLE_ENDIAN // Intel x86 is little-endian
#ifdef MACHINE_LITTLE_ENDIAN
#define convert4LittleEndian(buf, len) // Nothing to do in little-endian machines.
#else
// convert4LittleEndian() is no effect on little-endian machine.
// Note: len is in 4-byte.
void convert4LittleEndian(unsigned char* buf, unsigned len)
{
while (len)
{
uint32 t;
t = (uint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*((uint32*) buf) = t;
buf += 4;
len--;
}
}
#endif // #ifdef MACHINE_LITTLE_ENDIAN
//
// MD5 Initialization of MD5Context.
//
void MD5Init(MD5Context* context)
{
context->buf[0] = 0x67452301; // A
context->buf[1] = 0xefcdab89; // B
context->buf[2] = 0x98badcfe; // C
context->buf[3] = 0x10325476; // D
context->bits[0] = context->bits[1] = 0;
}
//
// Input data for MD5 operations.
//
void MD5Update(MD5Context* context, const unsigned char* input, unsigned int inputLen)
{
// Update bit count with inputLen.
uint32 count = context->bits[0];
if ((context->bits[0] += ((uint32) inputLen << 3)) < count) // check if bits[0] carry.
context->bits[1]++;
context->bits[1] += inputLen >> 29;
// Calculate original bytes in context->in[] buffer.
count = (count >> 3) & 0x3F;
// If there are previous data in the context->in[] buffer, we first move input data
// to in[] with previous data. Move as much as 64 bytes, and send to MD5Transform()
// if data is enough.
if (count)
{
unsigned char* p = &context->in[count];
count = 64 - count; // count becomes empty bytes to 64 bytes.
if (inputLen < count)
{ // not enough to become 64-byte block. just copy, no MD5Transform() needed.
memcpy(p, input, inputLen);
return;
}
// Enough to become 64-byte block. Copy and MD5Transform() with previous data.
memcpy(p, input, count);
convert4LittleEndian(context->in, 16); // 16 is 64-bytes/4-byte(sizeof uint32)
MD5Transform(context->buf, (uint32*) context->in);
// Modify input and inputLen bypass processed data.
input += count;
inputLen -= count;
}
// if 64-byte block available, process it to MD5Transform()
while (inputLen >= 64)
{
memcpy(context->in, input, 64);
convert4LittleEndian(context->in, 16); // 16 is 64-bytes/4-byte(sizeof uint32)
MD5Transform(context->buf, (uint32*) context->in);
input += 64;
inputLen -= 64;
}
// Handle Remaining data in input not enough to 64 byte.
memcpy(context->in, input, inputLen);
}
//
// Final processing of MD5 operations. It also output final results.
//
void MD5Final(unsigned char digest[16], MD5Context* context)
{
// Calculate original bytes in context->in[] buffer.
uint32 count = (context->bits[0] >> 3) & 0x3F;
// Set First char of padding to 0x80. This is safe since there is always
// at leat one byte free in context->in[].
unsigned char* p = &context->in[count];
*p++ = 0x80;
// Check if the empty space left is enough 2 uint32 to put bits count.
count = 64 - 1 - count; // count becomes empty bytes to 64 bytes.
if (count < 8)
{ // No space left. MD5Transform() this block first.
memset(p, 0, count);
convert4LittleEndian(context->in, 16);
MD5Transform(context->buf, (uint32*) context->in);
// And prepare next block with (64-8) 0s.
memset(context->in, 0, 56);
}
else
{ // Space is enough to directly put the 2-uint32 bits count.
// Padding 0 to left only 8 bytes space.
memset(p, 0, count - 8);
}
// Ready to MD5Transform(), put the 2-uint32 bits count in the end of context->in[].
convert4LittleEndian(context->in, 14);
((uint32 *)context->in)[14] = context->bits[0]; // MD5 is little-endian. So bits[0] first.
((uint32 *)context->in)[15] = context->bits[1];
MD5Transform(context->buf, (uint32*) context->in);
// Copy final result to digest.
convert4LittleEndian(context->buf, 4);
memcpy(digest, context->buf, 16);
// Zero context to avoid disclosure.
memset(context, 0, sizeof(MD5Context));
}
//
// Nonlinear function of MD5
//
// F, G, H and I are basic MD5 functions.
//#define F(x, y, z) (x & y | ~x & z)
#define F(x, y, z) (z ^ (x & (y ^ z))) // Optimized version.
//#define G(x, y, z) (x & z | y & ~z)
#define G(x, y, z) F(z, x, y)
#define H(x, y, z) (x ^ y ^ z)
#define I(x, y, z) (y ^ (x | ~z))
// The central funtion of MD5.
#define MD5STEP(f, w, x, y, z, data, s) \
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
void MD5Transform(uint32 buf[4], const uint32 in[16])
{
register uint32 a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD5STEP(F, a, b, c, d, in[0] + 0xd76aa478, 7);
MD5STEP(F, d, a, b, c, in[1] + 0xe8c7b756, 12);
MD5STEP(F, c, d, a, b, in[2] + 0x242070db, 17);
MD5STEP(F, b, c, d, a, in[3] + 0xc1bdceee, 22);
MD5STEP(F, a, b, c, d, in[4] + 0xf57c0faf, 7);
MD5STEP(F, d, a, b, c, in[5] + 0x4787c62a, 12);
MD5STEP(F, c, d, a, b, in[6] + 0xa8304613, 17);
MD5STEP(F, b, c, d, a, in[7] + 0xfd469501, 22);
MD5STEP(F, a, b, c, d, in[8] + 0x698098d8, 7);
MD5STEP(F, d, a, b, c, in[9] + 0x8b44f7af, 12);
MD5STEP(F, c, d, a, b, in[10] + 0xffff5bb1, 17);
MD5STEP(F, b, c, d, a, in[11] + 0x895cd7be, 22);
MD5STEP(F, a, b, c, d, in[12] + 0x6b901122, 7);
MD5STEP(F, d, a, b, c, in[13] + 0xfd987193, 12);
MD5STEP(F, c, d, a, b, in[14] + 0xa679438e, 17);
MD5STEP(F, b, c, d, a, in[15] + 0x49b40821, 22);
MD5STEP(G, a, b, c, d, in[1] + 0xf61e2562, 5);
MD5STEP(G, d, a, b, c, in[6] + 0xc040b340, 9);
MD5STEP(G, c, d, a, b, in[11] + 0x265e5a51, 14);
MD5STEP(G, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
MD5STEP(G, a, b, c, d, in[5] + 0xd62f105d, 5);
MD5STEP(G, d, a, b, c, in[10] + 0x02441453, 9);
MD5STEP(G, c, d, a, b, in[15] + 0xd8a1e681, 14);
MD5STEP(G, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
MD5STEP(G, a, b, c, d, in[9] + 0x21e1cde6, 5);
MD5STEP(G, d, a, b, c, in[14] + 0xc33707d6, 9);
MD5STEP(G, c, d, a, b, in[3] + 0xf4d50d87, 14);
MD5STEP(G, b, c, d, a, in[8] + 0x455a14ed, 20);
MD5STEP(G, a, b, c, d, in[13] + 0xa9e3e905, 5);
MD5STEP(G, d, a, b, c, in[2] + 0xfcefa3f8, 9);
MD5STEP(G, c, d, a, b, in[7] + 0x676f02d9, 14);
MD5STEP(G, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
MD5STEP(H, a, b, c, d, in[5] + 0xfffa3942, 4);
MD5STEP(H, d, a, b, c, in[8] + 0x8771f681, 11);
MD5STEP(H, c, d, a, b, in[11] + 0x6d9d6122, 16);
MD5STEP(H, b, c, d, a, in[14] + 0xfde5380c, 23);
MD5STEP(H, a, b, c, d, in[1] + 0xa4beea44, 4);
MD5STEP(H, d, a, b, c, in[4] + 0x4bdecfa9, 11);
MD5STEP(H, c, d, a, b, in[7] + 0xf6bb4b60, 16);
MD5STEP(H, b, c, d, a, in[10] + 0xbebfbc70, 23);
MD5STEP(H, a, b, c, d, in[13] + 0x289b7ec6, 4);
MD5STEP(H, d, a, b, c, in[0] + 0xeaa127fa, 11);
MD5STEP(H, c, d, a, b, in[3] + 0xd4ef3085, 16);
MD5STEP(H, b, c, d, a, in[6] + 0x04881d05, 23);
MD5STEP(H, a, b, c, d, in[9] + 0xd9d4d039, 4);
MD5STEP(H, d, a, b, c, in[12] + 0xe6db99e5, 11);
MD5STEP(H, c, d, a, b, in[15] + 0x1fa27cf8, 16);
MD5STEP(H, b, c, d, a, in[2] + 0xc4ac5665, 23);
MD5STEP(I, a, b, c, d, in[0] + 0xf4292244, 6);
MD5STEP(I, d, a, b, c, in[7] + 0x432aff97, 10);
MD5STEP(I, c, d, a, b, in[14] + 0xab9423a7, 15);
MD5STEP(I, b, c, d, a, in[5] + 0xfc93a039, 21);
MD5STEP(I, a, b, c, d, in[12] + 0x655b59c3, 6);
MD5STEP(I, d, a, b, c, in[3] + 0x8f0ccc92, 10);
MD5STEP(I, c, d, a, b, in[10] + 0xffeff47d, 15);
MD5STEP(I, b, c, d, a, in[1] + 0x85845dd1, 21);
MD5STEP(I, a, b, c, d, in[8] + 0x6fa87e4f, 6);
MD5STEP(I, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
MD5STEP(I, c, d, a, b, in[6] + 0xa3014314, 15);
MD5STEP(I, b, c, d, a, in[13] + 0x4e0811a1, 21);
MD5STEP(I, a, b, c, d, in[4] + 0xf7537e82, 6);
MD5STEP(I, d, a, b, c, in[11] + 0xbd3af235, 10);
MD5STEP(I, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
MD5STEP(I, b, c, d, a, in[9] + 0xeb86d391, 21);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -