我有一段代碼應該計算文件的SHA256
。我正在按塊讀取文件塊,並使用EVP_DigestUpdate
作爲塊。當我用文件來測試代碼,有內容OpenSSL SHA256錯誤的結果
Test Message Hello World
,它給我的97b2bc0cd1c3849436c6532d9c8de85456e1ce926d1e872a1e9b76a33183655f
SHA256值,但該值應該是318b20b83a6730b928c46163a2a1cefee4466132731c95c39613acb547ccb715
,可以驗證here too。
下面是代碼:
#include <openssl\evp.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
const int MAX_BUFFER_SIZE = 1024;
std::string FileChecksum(std::string, std::string);
int main()
{
std::string checksum = FileChecksum("C:\\Users\\Dell\\Downloads\\somefile.txt","sha256");
std::cout << checksum << std::endl;
return 0;
}
std::string FileChecksum(std::string file_path, std::string algorithm)
{
EVP_MD_CTX *mdctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
int i;
unsigned int md_len;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(algorithm.c_str());
if(!md) {
printf("Unknown message digest %s\n",algorithm);
exit(1);
}
mdctx = EVP_MD_CTX_create();
std::ifstream readfile(file_path,std::ifstream::in|std::ifstream::binary);
if(!readfile.is_open())
{
std::cout << "COuldnot open file\n";
return 0;
}
readfile.seekg(0, std::ios::end);
long filelen = readfile.tellg();
std::cout << "LEN IS " << filelen << std::endl;
readfile.seekg(0, std::ios::beg);
if(filelen == -1)
{
std::cout << "Return Null \n";
return 0;
}
EVP_DigestInit_ex(mdctx, md, NULL);
long temp_fil = filelen;
while(!readfile.eof() && readfile.is_open() && temp_fil>0)
{
int bufferS = (temp_fil < MAX_BUFFER_SIZE) ? temp_fil : MAX_BUFFER_SIZE;
char *buffer = new char[bufferS+1];
buffer[bufferS] = 0;
readfile.read(buffer, bufferS);
std::cout << strlen(buffer) << std::endl;
EVP_DigestUpdate(mdctx, buffer, strlen(buffer));
temp_fil -= bufferS;
delete[] buffer;
}
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);
printf("Digest is: ");
//char *checksum_msg = new char[md_len];
//int cx(0);
for(i = 0; i < md_len; i++)
{
//_snprintf(checksum_msg+cx,md_len-cx,"%02x",md_value[i]);
printf("%02x", md_value[i]);
}
//std::string res(checksum_msg);
//delete[] checksum_msg;
printf("\n");
/* Call this once before exit. */
EVP_cleanup();
return "";
}
我試着寫爲使用_snprintf
字符串由程序生成的哈希值,但它並沒有奏效。我怎樣才能生成正確的哈希值並返回值爲FileChecksum
函數的字符串?平臺是Windows。
編輯:看來問題是因爲CRLF
問題。當Windows使用\r\n
保存文件時,計算出的校驗和不同。如何處理這個?
您的示例輸入應該是'Test Message \ nHello World \ n'。 – Phylogenesis 2015-03-03 09:27:15
請再次參考問題。我已經更新了細節。 – Pant 2015-03-03 09:30:33