md5算法c++实现
时间: 2023-12-07 14:38:35 浏览: 85
以下是一个使用C++实现MD5算法的例子:
```c++
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef unsigned int uint32;
typedef unsigned char uint8;
const uint32 kBlockSize = 64;
class MD5 {
public:
MD5();
void Update(const void* data, size_t length);
void Final(uint8* result);
private:
void Transform(const uint8 block[kBlockSize]);
void Encode(uint8* output, const uint32* input, size_t length);
void Decode(uint32* output, const uint8* input, size_t length);
uint32 state_[4];
uint32 count_[2];
uint8 buffer_[kBlockSize];
};
MD5::MD5() {
state_[0] = 0x67452301;
state_[1] = 0xefcdab89;
state_[2] = 0x98badcfe;
state_[3] = 0x10325476;
count_[0] = 0;
count_[1] = 0;
}
void MD5::Update(const void* data, size_t length) {
const uint8* input = static_cast<const uint8*>(data);
uint32 buffer_size = count_[0] % kBlockSize;
count_[0] += length;
if (count_[0] < length) {
count_[1]++;
}
count_[1] += length / kBlockSize;
while (length > 0) {
uint32 copy_size = min(length, kBlockSize - buffer_size);
memcpy(buffer_ + buffer_size, input, copy_size);
buffer_size += copy_size;
input += copy_size;
length -= copy_size;
if (buffer_size == kBlockSize) {
Transform(buffer_);
buffer_size = 0;
}
}
}
void MD5::Final(uint8* result) {
uint32 buffer_size = count_[0] % kBlockSize;
buffer_[buffer_size++] = 0x80;
if (buffer_size > kBlockSize - 8) {
memset(buffer_ + buffer_size, 0, kBlockSize - buffer_size);
Transform(buffer_);
buffer_size = 0;
}
memset(buffer_ + buffer_size, 0, kBlockSize - buffer_size - 8);
uint32 bit_count[2];
bit_count[0] = count_[0] << 3;
bit_count[1] = (count_[1] << 3) | (count_[0] >> 29);
uint32* bit_count_ptr = bit_count;
Encode(buffer_ + kBlockSize - 8, bit_count_ptr, 2);
Transform(buffer_);
uint8* result_ptr = result;
Encode(result_ptr, state_, 4);
memset(this, 0, sizeof(*this));
}
void MD5::Transform(const uint8 block[kBlockSize]) {
static const uint32 k[64] = {
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
};
uint32 a = state_[0];
uint32 b = state_[1];
uint32 c = state_[2];
uint32 d = state_[3];
uint32 x[16];
Decode(x, block, kBlockSize);
a += ((b & c) | (~b & d)) + x[0] + k[0];
a = ((a << 7) | (a >> 25)) + b;
d += ((a & b) | (~a & c)) + x[1] + k[1];
d = ((d << 12) | (d >> 20)) + a;
c += ((d & a) | (~d & b)) + x[2] + k[2];
c = ((c << 17) | (c >> 15)) + d;
b += ((c & d) | (~c & a)) + x[3] + k[3];
b = ((b << 22) | (b >> 10)) + c;
a += ((b & c) | (~b & d)) + x[4] + k[4];
a = ((a << 7) | (a >> 25)) + b;
d += ((a & b) | (~a & c)) + x[5] + k[5];
d = ((d << 12) | (d >> 20)) + a;
c += ((d & a) | (~d & b)) + x[6] + k[6];
c = ((c << 17) | (c >> 15)) + d;
b += ((c & d) | (~c & a)) + x[7] + k[7];
b = ((b << 22) | (b >> 10)) + c;
a += ((b & c) | (~b & d)) + x[8] + k[8];
a = ((a << 7) | (a >> 25)) + b;
d += ((a & b) | (~a & c)) + x[9] + k[9];
d = ((d << 12) | (d >> 20)) + a;
c += ((d & a) | (~d & b)) + x[10] + k[10];
c = ((c << 17) | (c >> 15)) + d;
b += ((c & d) | (~c & a)) + x[11] + k[11];
b = ((b << 22) | (b >> 10)) + c;
a += ((b & c) | (~b & d)) + x[12] + k[12];
a = ((a << 7) | (a >> 25)) + b;
d += ((a & b) | (~a & c)) + x[13] + k[13];
d = ((d << 12) | (d >> 20)) + a;
c += ((d & a) | (~d & b)) + x[14] + k[14];
c = ((c << 17) | (c >> 15)) + d;
b += ((c & d) | (~c & a)) + x[15] + k[15];
b = ((b << 22) | (b >> 10)) + c;
a += ((b & d) | (c & ~d)) + x[1] + k[16];
a = ((a << 5) | (a >> 27)) + b;
d += ((a & c) | (b & ~c)) + x[6] + k[17];
d = ((d << 9) | (d >> 23)) + a;
c += ((d & b) | (a & ~b)) + x[11] + k[18];
c = ((c << 14) | (c >> 18)) + d;
b += ((c & a) | (d & ~a)) + x[0] + k[19];
b = ((b << 20) | (b >> 12)) + c;
a += ((b & d) | (c & ~d)) + x[5] + k[20];
a = ((a << 5) | (a >> 27)) + b;
d += ((a & c) | (b & ~c)) + x[10] + k[21];
d = ((d << 9) | (d >> 23)) + a;
c += ((d & b) | (a & ~b)) + x[15] + k[22];
c = ((c << 14) | (c >> 18)) + d;
b += ((c & a) | (d & ~a)) + x[4] + k[23];
b = ((b << 20) | (b >> 12)) + c;
a += ((b & d) | (c & ~d)) + x[9] + k[24];
a = ((a << 5) | (a >> 27)) + b;
d += ((a & c) | (b & ~c)) + x[14] + k[25];
d = ((d << 9) | (d >> 23)) + a;
c += ((d & b) | (a & ~b)) + x[3] + k[26];
c = ((c << 14) | (c >> 18)) + d;
b += ((c & a) | (d & ~a)) + x[8] + k[27];
b = ((b << 20) | (b >> 12)) + c;
a += ((b & d) | (c & ~d)) + x[13] + k[28];
a = ((a << 5) | (a >> 27)) + b;
d += ((a & c) | (b & ~c)) + x[2] + k[29];
d = ((d << 9) | (d >> 23)) + a;
c += ((d & b) | (a & ~b)) + x[7] + k[30];
c = ((c << 14) | (c >> 18)) + d;
b += ((c & a) | (d & ~a)) + x[12] + k[31];
b = ((b << 20) | (b >> 12)) + c;
a += (b ^ c ^ d) + x[5] + k[32];
a = ((a << 4) | (a >> 28)) + b;
d += (a ^ b ^ c) + x[8] + k[33];
d = ((d << 11) | (d >> 21)) + a;
c += (d ^ a ^ b) + x[11] + k[34];
c = ((c << 16) | (c >> 16)) + d;
b += (c ^ d ^ a) + x[14] + k[35];
b = ((b << 23) | (b >> 9)) + c;
a += (b ^ c ^ d) + x[1] + k[36];
a = ((a << 4) | (a >> 28)) + b;
d += (a ^ b ^ c) + x[4] + k[37];
d = ((d << 11) | (d >> 21)) + a;
c += (d ^ a ^ b) + x[7] + k[38];
c = ((c << 16) | (c >> 16)) + d;
b += (c ^ d ^ a) + x[10] + k[39];
b = ((b << 23) | (b >> 9)) + c;
a += (b ^ c ^ d) + x[13] + k[40];
a = ((a << 4) | (a >> 28)) + b;
d += (a ^ b ^ c) + x[0] + k[41];
d = ((d << 11) | (d >> 21)) + a;
c += (d ^ a ^ b) + x[3] + k[42];
c = ((c << 16) | (c >> 16)) + d;
b += (c ^ d ^ a) + x[6] + k[43];
b = ((b << 23) | (b >> 9)) + c;
a += (b ^ c ^ d) + x[9] + k[44];
a = ((a << 4) | (a >> 28)) + b;
d += (a ^ b ^ c) + x[12] + k[45];
d = ((d << 11) | (d >> 21)) + a;
c += (
阅读全文