【发布时间】:2016-04-11 14:16:31
【问题描述】:
我开始使用crypto++ lib,也许我有一些误解。
我不明白为什么下面的代码会产生错误的 sha 256
# include <string>
# include <iostream>
# include "cryptopp/cryptlib.h"
# include <cryptopp/sha.h>
# include <cryptopp/hex.h>
using namespace std;
string sha256_hex(const string & str)
{
byte digest[CryptoPP::SHA256::DIGESTSIZE];
CryptoPP::SHA256().CalculateDigest(digest, (byte*) &str[0], str.size());
string ret;
CryptoPP::HexEncoder encoder;
encoder.Attach(new CryptoPP::StringSink(ret));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
return ret;
}
int main(int argc, char *argv[])
{
auto sha256 = sha256_hex(argv[1]);
cout << "str = " << argv[1] << endl
<< "sha 256 = " << sha256_hex(sha256) << endl;
return 0;
}
以下命令
./test-sha256 hello
产生以下输出
str = hello
sha 256 = DD9F20FF4F1DD817C567DE6C16915DC0A731A4DF51088F55CEF4CD2F89CF9620
但是根据这个在线计算器enter link description here,"hello" 的正确 sha 256 应该是 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824。
所以我的问题是我做错了什么?
我还有一个问题:应该如何以及何时释放StringSink 对象使用的内存?
提前致谢
【问题讨论】:
-
嗨@lrleon 我正面临这种错误 --> 错误:未定义引用'vtable for CryptoPP::SHA256' 我想用cryptopp生成SHA256,你能帮帮我吗?跨度>