Can someone help me how can I easily use hash functions from Crypto++ library?
I tried used these codes for SHA1 and MD5. I have many errors on line where is StringSink
. The errors are like:
undefined reference to `CryptoPP::StringSinkTemplate::StringSinkTemplate(std::string&)'
Thanks for help.
// 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;
I have many errors on line where is StringSink
This line with the StringSink
looks OK to me. You should provide the exact error that you are encountering.
Error on line with StringSink is: undefined reference to `CryptoPP::StringSinkTemplate::StringSinkTemplate(std::string&)'
Based on the additional information you provided, see Building and linking test code for Crypto++
How use easily apply Crypto++ hash functions?
Below is from the Crypto++ wiki on the ChannelSwitch class. Since you want MD5, you need to #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
, and then #include <cryptopp/md5.h>
. After that, you can find MD5 in 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;
A run of the program produces the results shown below:
$ ./cryptopp-test.exe password
Message: password
SHA-1: 5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8
SHA-224: D63DC919E201D7BC4C825630D2CF25FDC93D4B2F0D46706D29038D01
SHA-256: 5E884898DA28047151D0E56F8DC6292773603D0D6AABBDD62A11EF721D1542D8
SHA-512: B109F3BBBC244EB82441917ED06D618B9008DD09B3BEFD1B5E07394C706A8BB9
80B1D7785E5976EC049B46DF5F1326AF5A2EA6D103FD07C95385FFAB0CACBC86