(excerpt from a message posted to the Crypto++ mailing list)
SHA-256 (or any other hash module) is used like this:
#include "sha.h"
.
.
.
SHA256().CalculateDigest(pbOutputBuffer, pbData, nDataLen);
// pbOutputBuffer must be SHA256::DIGESTSIZE bytes in length
or, if you have data that's made up of multiple pieces:
#include "sha.h"
.
.
.
SHA256 hash;
hash.Update(pbData1, nData1Len);
hash.Update(pbData2, nData2Len);
hash.Update(pbData3, nData3Len);
hash.Final(pbOutputBuffer);
See also the definition of HashTransformation::CalculateDigest() in
cryptlib.h.2009-Mar-16 6:20pm faq-o-matic, weidai |
// example of hashing followed by base64 encoding, using filters
#include "sha.h"
#include "base64.h"
std::string digest;
CryptoPP::SHA256 hash; // don't use MD5 anymore. It is considered insecure
// Thank you, Wei Dai, for making this possible:
CryptoPP::StringSource foo("CryptoPP is cool", true,
new CryptoPP::HashFilter(hash,
new CryptoPP::Base64Encoder (
new CryptoPP::StringSink(digest))));
std::cout << digest << std::endl;
---------------------
Check:
#!/bin/bash
echo -n "CryptoPP is cool" | openssl dgst -sha256 -binary | openssl base64 -e2009-Mar-17 6:04am cryptopp, weidai |