C++中实现MD5的代码
时间: 2023-09-13 08:05:20 浏览: 62
### 回答1:
以下是 C 语言实现 MD5 的代码:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
void md5(const uint8_t *initial_msg, size_t initial_len, uint8_t *digest) {
// These vars will contain the hash
uint32_t h, h1, h2, h3;
// Message (to prepare)
uint8_t *msg = NULL;
// Message length in bits
uint32_t new_len, offset;
uint32_t w[16];
uint32_t a, b, c, d, i, f, g, temp;
// Initialize variables - simple count in nibbles:
h = x67452301;
h1 = xEFCDAB89;
h2 = x98BADCFE;
h3 = x10325476;
//Pre-processing:
//append "1" bit to message
//append "" bits until message length in bits ≡ 448 (mod 512)
//append length mod (2^64) to message
for (new_len = initial_len + 1; new_len % (512 / 8) != 448 / 8; new_len++)
;
msg = (uint8_t *)calloc(new_len + 8, 1); // also appends "" bits
memcpy(msg, initial_msg, initial_len);
msg[initial_len] = x80; // append the "1" bit
// append length in bits and transform
offset = new_len / 8;
msg[offset++] = x80;
msg[offset++] = x80;
msg[offset++] = x80;
msg[offset++] = x80;
msg[offset++] = ;
msg[offset++] = ;
msg[offset++] = ;
msg[offset++] = initial_len * 8;
// Process the message in successive 512-bit chunks:
//for each 512-bit chunk of message:
for (offset = ; offset < new_len; offset += (512 / 8)) {
// break chunk into sixteen 32-bit words w[j], ≤ j ≤ 15
for (i = ; i < 16; i++)
w[i] = (msg[offset + i * 4 + 3] << 24) |
(msg[offset + i * 4 + 2] << 16) |
(msg[offset + i * 4 + 1] << 8) |
(msg[offset + i * 4]);
// Initialize hash value for this chunk:
a = h;
b = h1;
c = h2;
d = h3;
// Main loop:
for (i = ; i < 64; i++) {
if (i < 16) {
f = (b & c) | ((~b) & d);
g = i;
} else if (i < 32) {
f = (d & b) | ((~d) & 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;
}
temp = d;
d = c;
c = b;
b = b + LEFTROTATE((a + f + x5A827999 + w[g]), 7);
a = temp;
temp = d;
d = c;
c = b;
b = b + LEFTROTATE((a + f + x6ED9EBA1 + w[g]), 12);
a = temp;
temp = d;
d = c;
c = b;
b = b + LEFTROTATE((a + f + x8F1BBCDC + w[g]), 17);
a = temp;
temp = d;
d = c;
c = b;
b = b + LEFTROTATE((a + f + xCA62C1D6 + w[g]), 22);
a = temp;
}
// Add this chunk's hash to result so far:
h += a;
h1 += b;
h2 += c;
h3 += d;
}
// cleanup
free(msg);
// Output hash
digest[] = (h >> 24) & xFF;
digest[1] = (h >> 16) & xFF;
digest[2] = (h >> 8) & xFF;
digest[3] = h & xFF;
digest[4] = (h1 >> 24) & xFF;
digest[5] = (h1 >> 16) & xFF;
digest[6] = (h1 >> 8) & xFF;
digest[7] = h1 & xFF;
digest[8] = (h2 >> 24) & xFF;
digest[9] = (h2 >> 16) & xFF;
digest[10] = (h2 >> 8) & xFF;
digest[11] = h2 & xFF;
digest[12] = (h3 >> 24) & xFF;
digest[13] = (h3 >> 16) & xFF;
digest[14] = (h3 >> 8) & xFF;
digest[15] = h3 & xFF;
}
int main(int argc, char **argv) {
uint8_t digest[16];
char *msg = "hello world";
size_t len = strlen(msg);
md5((uint8_t *)msg, len, digest);
int i;
for (i = ; i < 16; i++)
printf("%02x", digest[i]);
printf("\n");
return ;
}
### 回答2:
首先,在C语言中实现MD5算法需要包含md5.h的头文件和md5.c的源文件。
md5.h的内容如下:
```c
#ifndef MD5_H
#define MD5_H
#include <stdint.h>
void md5(const uint8_t* initial_msg, size_t initial_len, uint8_t* digest);
#endif
```
md5.c的内容如下:
```c
#include "md5.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
void md5(const uint8_t* initial_msg, size_t initial_len, uint8_t* digest) {
uint32_t h0, h1, h2, h3;
uint8_t* msg = NULL;
size_t new_len;
uint32_t bits_len;
uint32_t i, j;
h0 = 0x67452301;
h1 = 0xEFCDAB89;
h2 = 0x98BADCFE;
h3 = 0x10325476;
new_len = (((initial_len + 8) / 64) + 1) * 64;
msg = (uint8_t*)malloc(new_len);
memcpy(msg, initial_msg, initial_len);
msg[initial_len] = 0x80;
bits_len = 8 * initial_len;
memcpy(msg + new_len - 8, &bits_len, 4);
uint32_t* words = (uint32_t*)msg;
uint32_t a, b, c, d, f, g, temp;
for (i = 0; i < new_len; i += 64) {
a = h0;
b = h1;
c = h2;
d = h3;
for (j = 0; j < 64; j++) {
if (j < 16) {
f = (b & c) | ((~b) & d);
g = j;
}
else if (j < 32) {
f = (d & b) | ((~d) & c);
g = (5 * j + 1) % 16;
}
else if (j < 48) {
f = b ^ c ^ d;
g = (3 * j + 5) % 16;
}
else {
f = c ^ (b | (~d));
g = (7 * j) % 16;
}
temp = d;
d = c;
c = b;
b = b + LEFTROTATE((a + f + words[g] + 0x5A827999), 7);
a = temp;
}
h0 += a;
h1 += b;
h2 += c;
h3 += d;
}
free(msg);
memcpy(digest, &h0, 4);
memcpy(digest + 4, &h1, 4);
memcpy(digest + 8, &h2, 4);
memcpy(digest + 12, &h3, 4);
}
```
然后,你可以使用以下代码来调用md5函数并计算给定字符串或文件的MD5值。
```c
#include "md5.h"
#include <stdio.h>
#include <string.h>
void print_md5(const uint8_t* digest) {
int i;
for (i = 0; i < 16; i++) {
printf("%02x", digest[i]);
}
printf("\n");
}
int main() {
// 用字符串计算MD5
const char* msg_str = "Hello, World!";
size_t msg_len = strlen(msg_str);
uint8_t digest_str[16];
md5((uint8_t*)msg_str, msg_len, digest_str);
printf("MD5 for \"%s\": ", msg_str);
print_md5(digest_str);
// 用文件计算MD5
FILE* file = fopen("myfile.txt", "rb");
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
fseek(file, 0, SEEK_END);
size_t file_len = ftell(file);
fseek(file, 0, SEEK_SET);
uint8_t* msg_file = (uint8_t*)malloc(file_len);
fread(msg_file, 1, file_len, file);
uint8_t digest_file[16];
md5(msg_file, file_len, digest_file);
printf("MD5 for file: ");
print_md5(digest_file);
fclose(file);
free(msg_file);
return 0;
}
```
使用以上代码,你可以获得给定字符串和文件的MD5值。
### 回答3:
MD5(Message Digest Algorithm 5)是一种常用的哈希算法,用于将任意长度的数据转换为固定长度的哈希值。下面是在C语言中实现MD5的代码示例:
```c
#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>
void print_md5_sum(unsigned char* md) {
int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
printf("%02x", md[i]);
}
printf("\n");
}
int main() {
unsigned char digest[MD5_DIGEST_LENGTH];
char message[] = "Hello, World!";
MD5((unsigned char*)&message, strlen(message), (unsigned char*)&digest);
printf("MD5: ");
print_md5_sum(digest);
return 0;
}
```
上述代码使用了OpenSSL库中的`MD5()`函数计算了字符串"Hello, World!"的MD5哈希值,并通过`print_md5_sum()`函数将哈希值以十六进制的形式输出在控制台上。
首先,我们声明了一个`digest`数组来存储计算后的MD5值。然后,我们声明了要进行哈希的字符串`message`。接下来,我们调用了`MD5()`函数来计算MD5值,传入参数为要进行哈希的数据、数据长度和用于存储结果的缓冲区。最后,我们使用`print_md5_sum()`函数将计算得到的MD5哈希值以十六进制形式打印在控制台上。
需要注意的是,上述代码中使用了OpenSSL库的`MD5()`函数。以确保哈希算法的正确性和安全性,请确保您的编译环境已正确安装和配置相关的库文件。
相关推荐
![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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)