CryptoPP: HashModule example correction

Source: Internet
Author: User
Tags crc32
CryptoPP: The HashModule example rectifies the Linux general technology-Linux programming and kernel information. For details, refer to the following section. I'm so happy. I finally changed the error in the userguide of this classic library and called the program. Haha, sahua

This is the hash example DumpHashes. cpp in Cryptopp userguide. The even environment is suse9, gcc version 4.1.0:

Note the following:

1. In the earlier version of CryptoPP, The HashModule has been defined as a new HashTransformation class. Therefore, all hashmodules must be changed to HashTransformation.

2. md5 is a weak algorithm, so # define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 is required, and weak: md5 must be added when MD5 is used.

3. The third error is that the main function directly calls the constructor SHA () and assigns a value to HashTransformation. An error is reported to indicate the conversion of an error, which is passed to the const * from a temporary object *, you only need to initialize an object for SHA and so on.

At the beginning, I was so confused that I thought how could I cook my own food. Since no one could give me some advice, let's think about it, when I opened the book and initialized the page, I thought of hoho. Although I know that I am really a newbie.

4. When I called HexEncoder, I first called hoho. Even if hoho was still uncertain about the new one, I asked a guy, just use new as the transfer address...

5. At the beginning, there were still a bunch of inexplicable errors, which were completely confused yesterday afternoon. It turns out that all these errors will automatically disappear if necessary header files are added.

6. The rest of the errors are case-insensitive.

As follows:
CODE: // codes in userguide, cryptlib. h, DumpHashes. cpp
# Include "./cryptlib/hex. h"
# Include "./cryptlib/files. h"
# Include "./cryptlib. h"
# Define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1

# Include
# Include

Using namespace std;
Using namespace CryptoPP;

Void DumpHash_SingleStep (CryptoPP: HashTransformation & hash, char const * szModuleName, std: string const & strData ){
// Can't use std: string for buffer;
// Its internal storage might not be contiguous
SecByteBlock sbbDigest (hash. DigestSize ());

Hash. CalculateDigest (sbbDigest. begin (), (byte const *) strData. data (), strData. size ());

Cout <szModuleName <"SS :";
HexEncoder (new FileSink (cout). Put (sbbDigest. begin (), sbbDigest. size ());
Cout <endl;
}

Void DumpHash_MultiStep (CryptoPP: HashTransformation & hash, char const * szModuleName, std: string const & strDataPart1, std: string const & strDataPart2, std: string const & strDataPart3 ){
Hash. Update (byte const *) strDataPart1.data (), strDataPart1.size ());
Hash. Update (byte const *) strDataPart2.data (), strDataPart2.size ());
Hash. Update (byte const *) strDataPart3.data (), strDataPart3.size ());

// Can't use std: string for buffer;
// Its internal storage might not be contiguous
SecByteBlock sbbDigest (hash. DigestSize ());

Hash. Final (sbbDigest. begin ());

Cout <szModuleName <"MS ";
HexEncoder (new FileSink (cout). Put (sbbDigest. begin (), sbbDigest. size ());
Cout <endl;
}

Void DumpHash_HashFilter (CryptoPP: HashTransformation & hash, char const * szModuleName, std: string const & strDataPart1, std: string const & strDataPart2, std: string const & strDataPart3 ){
// Here, we are free to use std: string as the destination,
// Because StringSink uses the correct std: string interface to append data
String strDigest;
HashFilter hashFilter (hash, new StringSink (strDigest ));
HashFilter. Put (byte const *) strDataPart1.data (), strDataPart1.size ());
HashFilter. Put (byte const *) strDataPart2.data (), strDataPart2.size ());
HashFilter. Put (byte const *) strDataPart3.data (), strDataPart3.size ());
HashFilter. MessageEnd ();

Cout <szModuleName <"HF :";
StringSource (strDigest, true, new HexEncoder (new FileSink (cout )));
Cout <endl;
}

Void DumpHash (CryptoPP: HashTransformation & hash, char const * szModuleName, std: string const & strDataPart1, std: string const & strDataPart2, std: string const & strDataPart3 ){
DumpHash_SingleStep (hash, szModuleName, strDataPart1 + strDataPart2 + strDataPart3 );
DumpHash_MultiStep (hash, szModuleName, strDataPart1, strDataPart2, strDataPart3 );
DumpHash_HashFilter (hash, szModuleName, strDataPart1, strDataPart2, strDataPart3 );
}

// Crypto ++
# Include "./cryptlib/sha. h"
# Include "./cryptlib/ripemd. h"
# Include "./cryptlib/md5.h"
# Include "./cryptlib/crc. h"

Int main (){
Using namespace std;
Using namespace CryptoPP;

SHA sha;
SHA256 sha256;
RIPEMD160 ripemd160;
Weak: MD5 md5;
CRC32 crc32;

Std: string strDataPart1 = "part 1 ;";
Std: string strDataPart2 = "part two ;";
Std: string strDataPart3 = "part three ;";

Try {
DumpHash (sha, "SHA", strDataPart1, strDataPart2, strDataPart3 );
DumpHash (sha256, "SHA256", strDataPart1, strDataPart2, strDataPart3 );
DumpHash (ripemd160, "RIPEMD160", strDataPart1, strDataPart2, strDataPart3 );
DumpHash (md5, "MD5", strDataPart1, strDataPart2, strDataPart3 );
DumpHash (crc32, "CRC32", strDataPart1, strDataPart2, strDataPart3 );
}
Catch (CryptoPP: Exception const & e ){
Cout <"CryptoPP: Exception caught:" <endl
<E. what () <endl;
Return 1;
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.