我好开心呀,我这破人终于把这个经典库的userguide里的错误改过来把程序调通了哈哈,撒花
注意,这是Cryptopp userguide中关于hash的例子 DumpHashes.cpp,偶的环境是suse9, gcc version 4.1.0:
哦,有几个地方需要注意的:
1,CryptoPP旧版本中的HashModule已经定义为新类HashTransformation,所以所有的HashModule都要改成HashTransformation.
2, md5是一个weak算法,所以需要 #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1,而在使用md5的时候也要加上weak::MD5
3, 第三个错误就是main函数里直接调用SHA()等构造函数赋值给HashTransformation,报错说错误的转换,从临时对象传给const*,只要在前面为SHA等初始化一个对象就可以了。
刚开始看到这么就懵了,觉得自己怎么可能会自己这么菜,既然没有人可以指点偶那就想想吧,刚翻开书到初始化这一页竟然就想到了,hoho,开心。虽然我知道自己真的很菜鸟嘿嘿。
4,调用HexEncoder的时候我就先懵了,竟然连传递参数这么简单的问题都不确定了hoho,即使后来对那个new仍然很不确定,问了某牛,就把new当作传地址好了...
5, 开始仍然有一堆莫名的错误,昨天下午彻底糊涂了,原来只要加上那些必要的头文件这些错误都会自动消失的嘿嘿。
6,其余错误都是小case啦,木有技术含量。
如下:
注意,这是Cryptopp userguide中关于hash的例子 DumpHashes.cpp,偶的环境是suse9, gcc version 4.1.0:
哦,有几个地方需要注意的:
1,CryptoPP旧版本中的HashModule已经定义为新类HashTransformation,所以所有的HashModule都要改成HashTransformation.
2, md5是一个weak算法,所以需要 #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1,而在使用md5的时候也要加上weak::MD5
3, 第三个错误就是main函数里直接调用SHA()等构造函数赋值给HashTransformation,报错说错误的转换,从临时对象传给const*,只要在前面为SHA等初始化一个对象就可以了。
刚开始看到这么就懵了,觉得自己怎么可能会自己这么菜,既然没有人可以指点偶那就想想吧,刚翻开书到初始化这一页竟然就想到了,hoho,开心。虽然我知道自己真的很菜鸟嘿嘿。
4,调用HexEncoder的时候我就先懵了,竟然连传递参数这么简单的问题都不确定了hoho,即使后来对那个new仍然很不确定,问了某牛,就把new当作传地址好了...
5, 开始仍然有一堆莫名的错误,昨天下午彻底糊涂了,原来只要加上那些必要的头文件这些错误都会自动消失的嘿嘿。
6,其余错误都是小case啦,木有技术含量。
如下:
- //codes in userguide, cryptlib.h, DumpHashes.cpp
- #include "./cryptlib/hex.h"
- #include "./cryptlib/files.h"
- #include "./cryptlib/cryptlib.h"
- #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
- #include <iostream>
- #include <string.h>
- 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;
- }
- }