C語言實(shí)現(xiàn)獲取文件MD5值
一、MD5介紹
MD5(Message Digest Algorithm 5)是一種常用的哈希函數(shù)算法。將任意長(zhǎng)度的數(shù)據(jù)作為輸入,并生成一個(gè)唯一的、固定長(zhǎng)度(通常是128位)的哈希值,稱為MD5值。MD5算法以其高度可靠性和廣泛應(yīng)用而聞名。
MD5算法主要具備以下特點(diǎn):
(1)不可逆性:給定MD5值無法通過逆運(yùn)算得到原始數(shù)據(jù)。
(2)唯一性:不同的輸入數(shù)據(jù)會(huì)生成不同的MD5值。
(3)高效性:對(duì)于給定的數(shù)據(jù),計(jì)算其MD5值是非??焖俚摹?/p>
MD5值的應(yīng)用場(chǎng)景包括:
(1)數(shù)據(jù)完整性驗(yàn)證:MD5值可以用于驗(yàn)證文件是否在傳輸過程中被篡改。發(fā)送方計(jì)算文件的MD5值并發(fā)送給接收方,接收方在接收到文件后重新計(jì)算MD5值,然后與發(fā)送方的MD5值進(jìn)行比較,如果一致,則說明文件未被篡改。
(2)密碼存儲(chǔ):在許多系統(tǒng)中,用戶密碼通常不會(huì)以明文形式存儲(chǔ),而是將其轉(zhuǎn)換為MD5值后存儲(chǔ)。當(dāng)用戶登錄時(shí),系統(tǒng)會(huì)將用戶輸入的密碼轉(zhuǎn)換為MD5值,然后與存儲(chǔ)的MD5值進(jìn)行比較,以驗(yàn)證密碼的正確性。
(3)安全認(rèn)證:MD5值也可用于數(shù)字證書等安全認(rèn)證中,用于驗(yàn)證文件的完整性和認(rèn)證信息的真實(shí)性。
(4)數(shù)據(jù)指紋:MD5值可以作為數(shù)據(jù)的唯一標(biāo)識(shí)符,用于快速比對(duì)和查找重復(fù)數(shù)據(jù)。
二、示例代碼
2.1 獲取數(shù)據(jù)MD5值(openssl庫(kù))
在C語言中獲取一段數(shù)據(jù)的MD5值,可以使用現(xiàn)有的第三方庫(kù)實(shí)現(xiàn)。以下是一個(gè)使用 OpenSSL 庫(kù)計(jì)算數(shù)據(jù)的MD5值的示例代碼:
(1)需要安裝 OpenSSL 庫(kù)(如果尚未安裝)并包含相關(guān)頭文件:
#include <stdio.h> #include <stdlib.h> #include <openssl/md5.h>
(2)創(chuàng)建一個(gè)子函數(shù)來計(jì)算數(shù)據(jù)的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); }
該函數(shù)接受三個(gè)參數(shù):data
為待計(jì)算的數(shù)據(jù)指針,length
為數(shù)據(jù)長(zhǎng)度,md5_hash
為存儲(chǔ)MD5值的數(shù)組。
下面是一個(gè)完整的程序,展示如何調(diào)用以上子函數(shù)并打印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; }
這個(gè)示例程序?qū)⑤敵鲆欢螖?shù)據(jù)的MD5值。可以將待計(jì)算的數(shù)據(jù)存儲(chǔ)在 data
數(shù)組中,并根據(jù)需要調(diào)整數(shù)據(jù)長(zhǎng)度。
這里使用的是 OpenSSL 提供的 MD5 函數(shù)。在編譯時(shí),需要鏈接 OpenSSL 庫(kù)。在 Linux 系統(tǒng)上,可以使用 -lssl -lcrypto
參數(shù)進(jìn)行鏈接。在 Windows 系統(tǒng)上,需要下載并安裝 OpenSSL 庫(kù),并配置正確的鏈接路徑和庫(kù)文件名稱。
2.2 獲取文件的MD5值(openssl庫(kù))
以下是使用 OpenSSL 庫(kù)計(jì)算文件的MD5值的示例代碼:
(1)需要安裝 OpenSSL 庫(kù)(如果尚未安裝)并包含相關(guān)頭文件:
#include <stdio.h> #include <stdlib.h> #include <openssl/md5.h>
(2)創(chuàng)建一個(gè)子函數(shù)來計(jì)算文件的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); }
該函數(shù)接受兩個(gè)參數(shù):filename
為待計(jì)算的文件名,md5_hash
為存儲(chǔ)MD5值的數(shù)組。
下面是一個(gè)完整的示例程序,展示如何調(diào)用以上子函數(shù)并打印文件的MD5值:
#include <stdio.h> #include <stdlib.h> #include <openssl/md5.h> ? void calculate_file_md5(const char* filename, unsigned char* md5_hash) { ? ?// ... 函數(shù)實(shí)現(xiàn)見上文 ... ? 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; }
這個(gè)示例程序?qū)⒋蜷_指定文件并計(jì)算其MD5值。需要將文件路徑存儲(chǔ)在 filename
字符串中,并根據(jù)需要調(diào)整該字符串。
請(qǐng)這里使用的是 OpenSSL 提供的 MD5 函數(shù)。在編譯時(shí),需要鏈接 OpenSSL 庫(kù)。在 Linux 系統(tǒng)上,可以使用 -lssl -lcrypto
參數(shù)進(jìn)行鏈接。在 Windows 系統(tǒng)上,需要下載并安裝 OpenSSL 庫(kù),并配置正確的鏈接路徑和庫(kù)文件名稱。
2.3 自己寫算法獲取MD5值
實(shí)現(xiàn)MD5算法比較復(fù)雜,涉及位操作、邏輯運(yùn)算、位移等。
以下是一個(gè)簡(jiǎn)化版本的純C語言MD5算法實(shí)現(xiàn):
#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 }; ? // 循環(huán)左移 #define LEFT_ROTATE(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) ? // 轉(zhuǎn)換為大端字節(jié)序 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個(gè)32位字,并進(jìn)行字節(jié)序轉(zhuǎn)換 ? ?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循環(huán)運(yùn)算 ? ?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; ? } ? ? ?// 更新狀態(tài) ? ?state[0] += a; ? ?state[1] += b; ? ?state[2] += c; ? ?state[3] += d; } ? // 計(jì)算MD5值 void calculate_md5(const uint8* message, size_t length, uint8* digest) { ? ?// 初始化狀態(tài) ? ?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; ?// 添加一個(gè)1 ? ?to_big_endian((uint32)(length * 8), padded_message + padded_length - 8); ?// 添加長(zhǎng)度(以位為單位) ? ? ?// 處理消息塊 ? ?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; }
這個(gè)程序可以計(jì)算給定字符串的MD5值。將待計(jì)算的數(shù)據(jù)存儲(chǔ)在 message
字符串中,根據(jù)需要調(diào)整數(shù)據(jù)長(zhǎng)度。
到此這篇關(guān)于C語言實(shí)現(xiàn)獲取文件MD5值的文章就介紹到這了,更多相關(guān)C語言獲取MD5值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++訪問std::variant類型數(shù)據(jù)的幾種方式小結(jié)
std::variant是?C++17中引入的一個(gè)新的類模板,提供了一種存儲(chǔ)不同類型的值的方式,本文主要介紹了C++訪問std::variant類型數(shù)據(jù)的幾種方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02C++如何實(shí)現(xiàn)簡(jiǎn)單的計(jì)時(shí)器詳解
因?yàn)樽罱e著無聊就想著要不用C++寫點(diǎn)什么東西,仔細(xì)想了想其實(shí)自己的C++學(xué)的也不怎么好,寫個(gè)簡(jiǎn)單的計(jì)時(shí)器吧!所以下面這篇文章主要介紹了利用C++如何實(shí)現(xiàn)簡(jiǎn)單的計(jì)時(shí)器,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01基于C語言實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了基于C語言實(shí)現(xiàn)簡(jiǎn)單的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11非常漂亮的新年祝福!C語言實(shí)現(xiàn)漂亮的煙花效果
非常漂亮的新年祝福!這篇文章主要介紹了C語言實(shí)現(xiàn)漂亮的煙花效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02C語言數(shù)據(jù)結(jié)構(gòu)與算法之鏈表(二)
在這篇文章中,我們將拋開令人頭禿的指針和結(jié)構(gòu)體,我們將另外使用一種數(shù)組來實(shí)現(xiàn)的方式,叫做模擬鏈表。讓來跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Visual Studio Community 2022(VS2022)安裝圖文方法
這篇文章主要介紹了Visual Studio Community 2022(VS2022)安裝方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09