c++实现MD5的代码
时间: 2023-08-31 08:20:25 浏览: 66
### 回答1:
以下是Python中实现MD5的代码:
```python
import hashlib
def md5(string):
m = hashlib.md5()
m.update(string.encode('utf-8'))
return m.hexdigest()
```
使用示例:
```python
my_string = 'Hello, world!'
print(md5(my_string)) # 输出: b94d27b9934d3e08a52e52d7da7dabfa
```
需要注意的是,MD5算法已经不再被视为安全的密码哈希算法,因为它容易被暴力破解或者碰撞攻击,因此在实际应用中,建议使用更安全的哈希算法,比如SHA-256或SHA-512。
### 回答2:
以下是一个使用C语言实现MD5算法的简单示例代码:
```c
#include <stdio.h>
#include <stdint.h>
#include <string.h>
// MD5算法中需要使用的常量
const uint32_t 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
};
// 左循环移位函数
#define LEFT_ROTATE(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
// MD5算法中的四个基本操作
#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)))
// MD5算法的主循环
void md5(uint8_t *input, uint32_t len, uint8_t *output) {
uint32_t a0 = 0x67452301;
uint32_t b0 = 0xefcdab89;
uint32_t c0 = 0x98badcfe;
uint32_t d0 = 0x10325476;
// 填充数据
uint32_t new_len = ((len + 8) / 64 + 1) * 64;
uint8_t *padded_input = (uint8_t *)malloc(new_len);
memcpy(padded_input, input, len);
memset(padded_input + len, 0, new_len - len);
padded_input[len] = 0x80;
*(uint64_t *)(padded_input + new_len - 8) = (uint64_t)len * 8;
// 处理数据块
for (uint32_t i = 0; i < new_len; i += 64) {
uint32_t a = a0;
uint32_t b = b0;
uint32_t c = c0;
uint32_t d = d0;
uint32_t *block = (uint32_t *)(padded_input + i);
for (uint32_t j = 0; j < 64; ++j) {
uint32_t f, g;
if (j < 16) {
f = F(b, c, d);
g = j;
} else if (j < 32) {
f = G(b, c, d);
g = (5 * j + 1) % 16;
} else if (j < 48) {
f = H(b, c, d);
g = (3 * j + 5) % 16;
} else {
f = I(b, c, d);
g = (7 * j) % 16;
}
uint32_t temp = d;
d = c;
c = b;
b = b + LEFT_ROTATE((a + f + k[j] + block[g]), (j % 32));
a = temp;
}
a0 += a;
b0 += b;
c0 += c;
d0 += d;
}
// 生成输出
*(uint32_t *)(output) = a0;
*(uint32_t *)(output + 4) = b0;
*(uint32_t *)(output + 8) = c0;
*(uint32_t *)(output + 12) = d0;
}
int main() {
char input[] = "Hello, world!";
uint8_t output[16];
md5((uint8_t *)input, strlen(input), output);
printf("MD5: ");
for (int i = 0; i < 16; ++i) {
printf("%02x", output[i]);
}
printf("\n");
return 0;
}
```
以上代码使用了C语言实现了MD5算法,并通过示例代码对字符串 "Hello, world!" 进行了MD5哈希计算。最终输出的MD5结果为:5eb63bbbe01eeed093cb22bb8f5acdc3
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)