如何轻松应用Crypto ++哈希函数?

有人可以帮助我如何轻松地使用来自Crypto ++库的哈希函数? 我试过用SHA1和MD5这些代码。 StringSink在哪里有很多错误。 错误是这样的:

 undefined reference to `CryptoPP::StringSinkTemplate::StringSinkTemplate(std::string&)' 

感谢帮助。

  // SHA CryptoPP::SHA1 sha1; std::string source = "Hello"; std::string hash = ""; CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash)))); std::cout << hash; // MD5 CryptoPP::MD5 hash; byte digest[ CryptoPP::MD5::DIGESTSIZE ]; std::string message = "abcdefghijklmnopqrstuvwxyz"; hash.CalculateDigest( digest, (byte*) message.c_str(), message.length() ); CryptoPP::HexEncoder encoder; std::string output; encoder.Attach( new CryptoPP::StringSink( output ) ); encoder.Put( digest, sizeof(digest) ); encoder.MessageEnd(); std::cout << output << std::endl; 

StringSink在哪里有很多错误

这条线与StringSink看起来OK。 你应该提供你遇到的确切的错误。


与StringSink一致的错误是:对CryptoPP :: StringSinkTemplate :: StringSinkTemplate(std :: string&)的未定义引用

根据您提供的附加信息,请参阅构建并链接Crypto ++的测试代码


如何轻松应用Crypto ++哈希函数?

以下是来自ChannelSwitch类的Crypto ++ wiki。 既然你想要MD5,你需要#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 ,然后#include <cryptopp/md5.h> 。 之后,你可以在Weak::MD5找到Weak::MD5

 string message = "Now is the time for all good men to come to the aide of their country"; if(argc == 2 && argv[1] != NULL) message = string(argv[1]); string s1, s2, s3, s4; SHA1 sha1; SHA224 sha224; SHA256 sha256; SHA512 sha512; HashFilter f1(sha1, new HexEncoder(new StringSink(s1))); HashFilter f2(sha224, new HexEncoder(new StringSink(s2))); HashFilter f3(sha256, new HexEncoder(new StringSink(s3))); HashFilter f4(sha512, new HexEncoder(new StringSink(s4))); ChannelSwitch cs; cs.AddDefaultRoute(f1); cs.AddDefaultRoute(f2); cs.AddDefaultRoute(f3); cs.AddDefaultRoute(f4); StringSource ss(message, true /*pumpAll*/, new Redirector(cs)); cout << Message: " << message << endl; cout << "SHA-1: " << s1 << endl; cout << "SHA-224: " << s2 << endl; cout << "SHA-256: " << s3 << endl; cout << "SHA-512: " << s4 << endl; 

程序运行产生如下所示的结果:

 $ ./cryptopp-test.exe password Message: password SHA-1: 5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8 SHA-224: D63DC919E201D7BC4C825630D2CF25FDC93D4B2F0D46706D29038D01 SHA-256: 5E884898DA28047151D0E56F8DC6292773603D0D6AABBDD62A11EF721D1542D8 SHA-512: B109F3BBBC244EB82441917ED06D618B9008DD09B3BEFD1B5E07394C706A8BB9 80B1D7785E5976EC049B46DF5F1326AF5A2EA6D103FD07C95385FFAB0CACBC86