2015-07-13 78 views

回答

2

不,Boost沒有實現MD5。爲此使用加密/散列庫。

CryptoC++在我的經驗很好。

的OpenSSL實現所有流行的消化,這是一個使用OpenSSL的樣本:

Live On Coliru

#include <openssl/md5.h> 
#include <iostream> 
#include <iomanip> 

// Print the MD5 sum as hex-digits. 
void print_md5_sum(unsigned char* md) { 
    for(unsigned i=0; i <MD5_DIGEST_LENGTH; i++) { 
     std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md[i]); 
    } 
} 

#include <string> 
#include <vector> 
#include <fstream> 

int main(int argc, char *argv[]) { 
    using namespace std; 
    vector<string> const args(argv+1, argv+argc); 

    for (auto& fname : args) { 

     MD5_CTX ctx; 
     MD5_Init(&ctx); 

     ifstream ifs(fname, std::ios::binary); 

     char file_buffer[4096]; 
     while (ifs.read(file_buffer, sizeof(file_buffer)) || ifs.gcount()) { 
      MD5_Update(&ctx, file_buffer, ifs.gcount()); 
     } 
     unsigned char digest[MD5_DIGEST_LENGTH] = {}; 
     MD5_Final(digest, &ctx); 

     print_md5_sum(digest); 
     std::cout << "\t" << fname << "\n"; 
    } 
} 
+0

這僅是Linux,這就是問題對我來說。 – Croll

+1

@joker crypto C++和OpenSsl都可以在windows上使用(也許大多數平臺是你可以夢想的) – sehe

+0

你的代碼(以及其他任何示例)使用linux頭文件,如果是這樣,你能顯示windows例子嗎?將是美妙的 – Croll