HashFilter

From Crypto++ Wiki
Jump to navigation Jump to search
HashTransformation
Documentation
#include <cryptopp/filters.h>

HashFilter uses the specified hash algorithm to calculate the hash of all input data up to the first MessageEnd signal, at which time it outputs the resulting hash value to its attached transformation. The companion class is the HashVerificationFilter.

The HashFilter takes a pointer to a BufferedTransformation. Because a pointer is taken, the HashFilter owns the attached transformation, and therefore will destroy it. See ownership for more details.

Sources, filters and sinks are discussed at Pipelining. The pipeline article explains the design and shows you how to use them.

Construction

HashFilter(HashTransformation &hm,
           BufferedTransformation *attachment=NULL,
           bool putMessage=false,
           int truncatedDigestSize=-1)

hm is a hash, like MD5, SHA-1, RIPEMD-160, SHA-256.

attachment is a BufferedTransformation, such as another filter or sink. If attachment is NULL, then the HashFilter object will internally accumulate the output byte stream.

putMessage indicates whether the hash should be output along with the message. If true, then the hash is appended to the message.

truncatedDigestSize indicates the size of the digest to output. If the value is -1, then the full digest size is output.

Examples

Hash the string "Yoda said, Do or do not. There is no try." with SHA256:

string source, value;
SHA256 hash;

source = "Yoda said, Do or do not. There is no try.";
StringSource ss( source, true /* PumpAll */,
                 new HashFilter( hash, 
                   new HexEncoder( 
                     new StringSink( value )
                   ) // HexEncoder
                 ) // HashFilter
              ); // StringSource

cout << source << endl;
cout << value << endl;

Hash the contents of a file rather than a string:

FileSource fs( filename, true /* PumpAll */,
              new HashFilter( hash, 
                new HexEncoder( 
                  new StringSink( value )
                ) // HexEncoder
              ) // HashFilter
            ); // FileSource

Format the string to something similar to 0C:AE:59:41:2D:32:72:C1:2A:49:85:93:0F:31:3D:C1:

FileSource fs( filename, true /* PumpAll */,
              new HashFilter( hash, 
                 new HexEncoder( 
                    new StringSink( value ), true /*UCase*/, 2 /*Grouping*/
                 ) // HexEncoder 
               ) // HashFilter
            ); // FileSource

Downloads

No downloads.