This commit is contained in:
2025-05-15 16:54:43 +08:00
parent d8dba2f70e
commit e444af237e
3 changed files with 248 additions and 30 deletions

View File

@@ -11,6 +11,8 @@
#define SHA224_DIGEST_LENGTH 28
#define SHA1_DIGEST_LENGTH 20
#define SHA1_BLOCK_SIZE 64
#define MD5_DIGEST_LENGTH 16
#define MD5_BLOCK_SIZE 64
using namespace hash_lib;
@@ -721,3 +723,137 @@ size_t SHA1::hashBlocks(const uint8_t* m, size_t pos, size_t len) {
}
return pos;
}
MD5::MD5() {
this->reset();
}
int MD5::digestLength() {
return MD5_DIGEST_LENGTH; // MD5 produces a 128-bit hash value (16 bytes)
}
int MD5::blockSize() {
return MD5_BLOCK_SIZE; // MD5 processes data in 512-bit blocks (64 bytes)
}
void MD5::_initState() {
state[0] = 0x67452301;
state[1] = 0xEFCDAB89;
state[2] = 0x98BADCFE;
state[3] = 0x10325476;
}
Hash* MD5::reset() {
_initState();
_bufferLength = 0;
_bytesHashed = 0;
_finished = false;
return this;
}
void MD5::clean() {
cleanBuffer(_buffer);
cleanBuffer(_temp);
reset();
}
Hash* MD5::update(const uint8_t* data, size_t len) {
if (_finished) return this;
size_t dataPos = 0;
_bytesHashed += len;
if (_bufferLength > 0) {
while (_bufferLength < MD5_BLOCK_SIZE && len > 0) {
_buffer[_bufferLength++] = data[dataPos++];
len--;
}
if (_bufferLength == MD5_BLOCK_SIZE) {
hashBlocks(_buffer, 0, MD5_BLOCK_SIZE);
_bufferLength = 0;
}
}
if (len >= MD5_BLOCK_SIZE) {
dataPos = hashBlocks(data, dataPos, len);
len %= MD5_BLOCK_SIZE;
}
while (len > 0) {
_buffer[_bufferLength++] = data[dataPos++];
len--;
}
return this;
}
Hash* MD5::finish(uint8_t* data, size_t len) {
if (!_finished) {
size_t bytesHashed = _bytesHashed;
size_t left = _bufferLength;
uint64_t bitLen = bytesHashed << 3;
size_t padLength = (bytesHashed % MD5_BLOCK_SIZE) < 56 ? 64 : 128;
_buffer[left] = 0x80;
memset(_buffer + left + 1, 0, padLength - left - 9);
cstr_write_uint64(_buffer + padLength - 8, bitLen, 0);
hashBlocks(_buffer, 0, padLength);
_finished = true;
}
for (int i = 0; i < this->digestLength() / 4 && i < len / 4; i++) {
cstr_write_uint32(data + i * 4, state[i], 0);
}
return this;
}
const uint8_t MD5_s[] = {
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
};
const uint32_t MD5_K[] = {
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
};
size_t MD5::hashBlocks(const uint8_t* m, size_t pos, size_t len) {
while (len >= 64) {
uint32_t a = state[0], b = state[1], c = state[2], d = state[3];
for (int i = 0; i < 16; i++) {
size_t j = i * 4 + pos;
_temp[i] = cstr_read_uint32(m + j, 0);
}
for (int i = 0; i < 64; i++) {
uint32_t f, g;
if (i < 16) {
f = d ^ (b & (c ^ d));
g = i;
} else if (i < 32) {
f = c ^ (d & (b ^ c));
g = (5 * i + 1) % 16;
} else if (i < 48) {
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
} else {
f = c ^ (b | ~d);
g = (7 * i) % 16;
}
f += a + MD5_K[i] + _temp[g];
a = d;
d = c;
c = b;
b = b + ((f << MD5_s[i]) | (f >> (32 - MD5_s[i])));
}
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
pos += 64;
len -= 64;
}
return pos;
}