Given the filename, how can I get the Adler32 using Crypto ++
Given the "string file name" how can I get the Adler32 checksum using the C ++ Crypto ++ library. I'm a little confused about using the Source and Sink system.
Below I have a skeleton of code that does MD5, but I can't find any examples or tutorials on how to use Adler32.
string filename = "/tmp/data.txt"
string file_adler32_digest;
string file_md5_digest;
MD5 hashMD5;
FileSource fs( filename.c_str(),
true,
new HashFilter( hashMD5,
new HexEncoder( new StringSink( file_md5_digest ) ) ) );
/* Confusion begins here */
//how do I do the adler32 ?
/* Confusion ends here */
cout << file_adler32_digest << endl
<< file_md5_digest << endl;
Good samples and sample code here http://www.cryptopp.com/wiki/Category:Sample for all Crypto ++ (except for the Adler32 stuff I want)
a source to share
If you follow this http://www.cryptopp.com/wiki/HashFilter you need to change hashMD5 for hashAdler32 and file_md5_digest for file_adler32_digest
Adler32 hashAdler32;
FileSource( filename.c_str(),
true,
new HashFilter( hashAdler32,
new HexEncoder( new StringSink( file_adler32_digest ) ) ) );
After that file_adler32_digest
should contain the desired hash.
a source to share
.. a little confused about using their Source and Sink system.
OK. This design is inspired by the Unix pipe system. If you understand Unix pipes and how the stream flows through it, then you understand Crypto ++ Pipeline .
For example, the following Unix commands:
cat somefile.bin | hexdump
will become the next Crypto ++ program:
FileSource fs("somefile.bin", true /*pumpAll*/, new HexEncoder(new FileSink(cout)));
I have a skeleton of code that does MD5, but I need the code ... for Adler32 ...
As Ismail showed you, it's simple:
Adler32 adler32;
string digest;
FileSource fs(filename.c_str(), true /*pumpAll*/,
new HashFilter(adler32,
new HexEncoder(
new StringSink(digest))));
But here's the insight: In Crypto ++, data flows from Sources to Sinks . In between, it will encounter filters that transform the data.
There are two filters above, HashFilter and HexEncoder . All filters inherit from BufferedTransformation
, so they can all be daisy-chained.
Adler32
itself is not a filter. But it inherits from HashTransformation
, and this is what the HashFilter uses. This way you can change any HashFilter object and everything will work.
You can find the HashFilter list of the HashTransformation object Class reference . These include all hashes (e.g. MD5, SHA and Whirlpool), Adler32, and CRC32.
a source to share