【发布时间】:2014-10-28 16:07:48
【问题描述】:
当我使用 openssl-command-line 计算文件的 sha256-digest 时,它与使用 openssl-library 在 c++ 中生成的摘要完全不同。
命令行:
openssl dgst -binary -sha256 MyFile.txt
c++:
BIO* bio = BIO_new_file("MyFile.txt", "rb");
if (bio != NULL)
{
OpenSSL_add_all_digests();
const EVP_MD* md = EVP_sha256();
if (md != NULL)
{
EVP_MD_CTX* ctx = EVP_MD_CTX_create();
if (EVP_DigestInit_ex(ctx, md, NULL))
{
const int bufLength = 4096;
unsigned char buf[bufLength];
unsigned int len;
while (BIO_read(bio, buf, bufLength))
EVP_DigestUpdate(ctx, buf, bufLength);
unsigned char digest[EVP_MAX_MD_SIZE];
int ok = EVP_DigestFinal_ex(ctx, digest, &len); // ok == 1; digest in variable "digest" is totally different from the one calculated on command-line
}
EVP_MD_CTX_cleanup(ctx);
BIO_free_all(bio);
}
}
为什么通过 c++ 计算的摘要与命令行计算的摘要完全不同? (MyFile.txt 中没有换行符或空格)
亲切的问候, 马蒂亚斯
【问题讨论】:
标签: c++ command-line openssl digest