HashFilter

From Crypto++ Wiki
Jump to: navigation, search

HashFilter.png

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.

Construction

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

The first parameter is the hash: MD5, SHA1, RIPEMD160, SHA256, etc. The second parameter specifies whether the HashFilter forwards input data to its attached transformation before outputing the hash. The final parameter, truncatedDigestSize, is used to specify the truncated size, if desired. truncatedDigestSize is used to create tags of the requested size.

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 ( 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( 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( filename, true /* PumpAll */,
              new HashFilter( hash, 
                 new HexEncoder( 
                    new StringSink( value ), true /*UCase*/, 2 /*Grouping*/
                 ) // HexEncoder 
               ) // HashFilter
            ); // FileSource