2016-11-18 132 views
1

我正在接收文件及其數值的CRC。我必須重新計算收到的文件上的CRC並進行比較。我正在使用Crypto ++,但它沒有給我數字值。請讓我知道如何使用Crypto ++來計算文件的CRC32。使用Crypto ++將文件的CRC作爲數值計算出來

#include<iostream> 
#include<string> 
#include <cryptopp/sha.h> 
#include <cryptopp/crc.h> 
#include <cryptopp/hex.h> 
typedef int UInt32; 
#include <cryptopp/files.h> 

using namespace std; 

string calculateCRC(const string& fileName) 
{ 
    string result; 
    CryptoPP::CRC32 hash; 
    CryptoPP::FileSource(fileName.c_str(),true, 
    new CryptoPP::HashFilter(hash, 
    new CryptoPP::StringSink(result))); 
    return result; 
} 

int main(int argc, char** argv) 
{ 
    cout << endl << calculateCRC("./test.cpp"); 
} 

這給出結果爲「\ 271 \ 063 \ 307Q」。 由於

回答

0

這給出結果爲 「\ 271 \ 063 \ 307Q」

我相信這是3個字節的二進制數據的一個八進制顯示。在GDB下,您可以使用x/4b <address>以ASCII格式打印:B9 33 C7 ...

添加HexEncoderfilter chain,如果你想在ASCII而非二進制:

CRC32 hash; 
string result; 

FileSource fs(fileName.c_str(), true, 
    new HashFilter(hash, 
     new HexEncoder(
      new StringSink(result)))); 

cout << result << endl; 

它的輸出將類似於:

$ ./test.exe 
80BA6847 

typedef int UInt32; 

這可能不正確。 int遭受溢出和未定義的行爲;而unsigned int沒有。