(Answer) (Category) Crypto++ Faq-O-Matic :
How do I use a hash function?
Moderator: weidai (inherited from parent)
(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 -e
2009-Mar-17 6:04am cryptopp, weidai
[Append to This Answer]
2009-Mar-17 6:04am
Previous: (Answer) How do I use a stream cipher?
Next: (Answer) How do I use a message authentication code?
This document is: http://www.cryptopp.com/cgi-bin/fom.cgi?file=50
[Search] [Appearance] [Show Expert Edit Commands]
This is a Faq-O-Matic 2.721.
This FAQ is administered by Wei Dai.