Question

Given a "string filename", how can I get the Adler32 checksum using the C++ Crypto++ library. I am a little confused about using their Source and Sink system.

Below I have the skeleton of the code that does MD5, but I can't seem to find any examples or tutorials on the Adler32 usage.

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 the Crypto++ (except for the Adler32 stuff I want)

Was it helpful?

Solution

If you follow this http://www.cryptopp.com/wiki/HashFilter, you have 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 this file_adler32_digest should contain the desired hash.

OTHER TIPS

.. a little confused about using their Source and Sink system.

OK. That particular design is inspired by the Unix pipe system. If you understand Unix pipes and how doata flows through it, then you understand the Crypto++ Pipeline.

For example, the following Unix commands:

cat somefile.bin | hexdump

would become the following Crypto++ program:

FileSource fs("somefile.bin", true /*pumpAll*/, new HexEncoder(new FileSink(cout)));

I have the skeleton of the code that does MD5, but I need the code ... for Adler32 ...

As Ismael showed you, its as simple as:

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.

Above, you have two filters: the HashFilter and the HexEncoder. All the filters inherit from a BufferedTransformation, so they can all be chained together in a consistent manner.

Adler32 itself is not a filter. But it inherits from HashTransformation, and that is what HashFilter uses. So you can swap in any HashFilter based object and things will just work.

You can find a list of HashFilter based object at HashTransformation Class Reference. They include all the hashes (like MD5, SHA and Whirlpool), Adler32 and CRC32.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top