一、MD5介紹
MD5(Message Digest Algorithm 5)是一種常用的哈希函數算法。将任意長度的資料作為輸入,并生成一個唯一的、固定長度(通常是128位)的哈希值,稱為MD5值。MD5算法以其高度可靠性和廣泛應用而聞名。
MD5算法主要具備以下特點:
(1)不可逆性:給定MD5值無法通過逆運算得到原始資料。
(2)唯一性:不同的輸入資料會生成不同的MD5值。
(3)高效性:對于給定的資料,計算其MD5值是非常快速的。
MD5值的應用場景包括:
(1)資料完整性驗證:MD5值可以用于驗證檔案是否在傳輸過程中被篡改。發送方計算檔案的MD5值并發送給接收方,接收方在接收到檔案後重新計算MD5值,然後與發送方的MD5值進行比較,如果一緻,則說明檔案未被篡改。
(2)密碼存儲:在許多系統中,使用者密碼通常不會以明文形式存儲,而是将其轉換為MD5值後存儲。當使用者登入時,系統會将使用者輸入的密碼轉換為MD5值,然後與存儲的MD5值進行比較,以驗證密碼的正确性。
(3)安全認證:MD5值也可用于數字證書等安全認證中,用于驗證檔案的完整性和認證資訊的真實性。
(4)資料指紋:MD5值可以作為資料的唯一辨別符,用于快速比對和查找重複資料。
二、示例代碼
2.1 擷取資料MD5值(openssl庫)
在C語言中擷取一段資料的MD5值,可以使用現有的第三方庫實作。以下是一個使用 OpenSSL 庫計算資料的MD5值的示例代碼:
(1)需要安裝 OpenSSL 庫(如果尚未安裝)并包含相關頭檔案:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/md5.h>
(2)建立一個子函數來計算資料的MD5值:
void calculate_md5(const unsigned char* data, size_t length, unsigned char* md5_hash) {
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, data, length);
MD5_Final(md5_hash, &ctx);
}
該函數接受三個參數:data 為待計算的資料指針,length 為資料長度,md5_hash 為存儲MD5值的數組。
下面是一個完整的程式,展示如何調用以上子函數并列印MD5值:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/md5.h>
void calculate_md5(const unsigned char* data, size_t length, unsigned char* md5_hash) {
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, data, length);
MD5_Final(md5_hash, &ctx);
}
void print_md5(const unsigned char* md5_hash) {
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
printf("%02x", md5_hash[i]);
}
printf("\n");
}
int main() {
const unsigned char data[] = "Hello, World!";
size_t length = sizeof(data) - 1; // 減去字元串末尾的空字元
unsigned char md5_hash[MD5_DIGEST_LENGTH];
calculate_md5(data, length, md5_hash);
printf("MD5: ");
print_md5(md5_hash);
return 0;
}
這個示例程式将輸出一段資料的MD5值。可以将待計算的資料存儲在 data 數組中,并根據需要調整資料長度。
這裡使用的是 OpenSSL 提供的 MD5 函數。在編譯時,需要連結 OpenSSL 庫。在 Linux 系統上,可以使用 -lssl -lcrypto 參數進行連結。在 Windows 系統上,需要下載下傳并安裝 OpenSSL 庫,并配置正确的連結路徑和庫檔案名稱。
2.2 擷取檔案的MD5值(openssl庫)
以下是使用 OpenSSL 庫計算檔案的MD5值的示例代碼:
(1)需要安裝 OpenSSL 庫(如果尚未安裝)并包含相關頭檔案:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/md5.h>
(2)建立一個子函數來計算檔案的MD5值:
void calculate_file_md5(const char* filename, unsigned char* md5_hash) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
printf("Failed to open file: %s\n", filename);
return;
}
MD5_CTX ctx;
MD5_Init(&ctx);
unsigned char buffer[1024];
size_t read;
while ((read = fread(buffer, 1, sizeof(buffer), file)) != 0) {
MD5_Update(&ctx, buffer, read);
}
fclose(file);
MD5_Final(md5_hash, &ctx);
}
該函數接受兩個參數:filename 為待計算的檔案名,md5_hash 為存儲MD5值的數組。
下面是一個完整的示例程式,展示如何調用以上子函數并列印檔案的MD5值:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/md5.h>
void calculate_file_md5(const char* filename, unsigned char* md5_hash) {
// ... 函數實作見上文 ...
void print_md5(const unsigned char* md5_hash) {
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
printf("%02x", md5_hash[i]);
}
printf("\n");
}
int main() {
const char* filename = "path/to/file";
unsigned char md5_hash[MD5_DIGEST_LENGTH];
calculate_file_md5(filename, md5_hash);
printf("MD5: ");
print_md5(md5_hash);
return 0;
}
這個示例程式将打開指定檔案并計算其MD5值。需要将檔案路徑存儲在 filename 字元串中,并根據需要調整該字元串。
請這裡使用的是 OpenSSL 提供的 MD5 函數。在編譯時,需要連結 OpenSSL 庫。在 Linux 系統上,可以使用 -lssl -lcrypto 參數進行連結。在 Windows 系統上,需要下載下傳并安裝 OpenSSL 庫,并配置正确的連結路徑和庫檔案名稱。
2.3 自己寫算法擷取MD5值
實作MD5算法比較複雜,涉及位操作、邏輯運算、位移等。
以下是一個簡化版本的純C語言MD5算法實作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char uint8;
typedef unsigned int uint32;
// MD5常量定義
const uint32 MD5_CONSTANTS[] = {
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, n) (((x) << (n)) | ((x) >> (32 - (n))))
// 轉換為大端位元組序
void to_big_endian(uint32 value, uint8* buffer) {
buffer[0] = (uint8)(value & 0xff);
buffer[1] = (uint8)((value >> 8) & 0xff);
buffer[2] = (uint8)((value >> 16) & 0xff);
buffer[3] = (uint8)((value >> 24) & 0xff);
}
// 處理消息塊
void process_block(const uint8* block, uint32* state) {
uint32 a = state[0];
uint32 b = state[1];
uint32 c = state[2];
uint32 d = state[3];
uint32 m[16];
// 将消息塊劃分為16個32位字,并進行位元組序轉換
for (int i = 0; i < 16; i++) {
m[i] = (((uint32)block[i * 4 + 0]) << 0) |
(((uint32)block[i * 4 + 1]) << 8) |
(((uint32)block[i * 4 + 2]) << 16) |
(((uint32)block[i * 4 + 3]) << 24);
}
// MD5循環運算
for (int i = 0; i < 64; i++) {
uint32 f, g;
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;
}
uint32 temp = d;
d = c;
c = b;
b = b + LEFT_ROTATE((a + f + MD5_CONSTANTS[i] + m[g]), 7);
a = temp;
}
// 更新狀态
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
// 計算MD5值
void calculate_md5(const uint8* message, size_t length, uint8* digest) {
// 初始化狀态
uint32 state[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
// 填充消息
size_t padded_length = ((length + 8) / 64 + 1) * 64;
uint8* padded_message = (uint8*)calloc(padded_length, 1);
memcpy(padded_message, message, length);
padded_message[length] = 0x80; // 添加一個1
to_big_endian((uint32)(length * 8), padded_message + padded_length - 8); // 添加長度(以位為機關)
// 處理消息塊
for (size_t i = 0; i < padded_length; i += 64) {
process_block(padded_message + i, state);
}
// 生成摘要
for (int i = 0; i < 4; i++) {
to_big_endian(state[i], digest + i * 4);
}
free(padded_message);
}
// 列印MD5值
void print_md5(const uint8* digest) {
for (int i = 0; i < 16; i++) {
printf("%02x", digest[i]);
}
printf("\n");
}
int main() {
const char* message = "Hello, World!";
size_t length = strlen(message);
uint8 digest[16];
calculate_md5((const uint8*)message, length, digest);
printf("MD5: ");
print_md5(digest);
return 0;
}
這個程式可以計算給定字元串的MD5值。将待計算的資料存儲在 message 字元串中,根據需要調整資料長度。