CryptoPP::HashModule例子纠正

我好开心呀,我这破人终于把这个经典库的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啦,木有技术含量。

如下:
  1. //codes in userguide, cryptlib.h, DumpHashes.cpp
  2. #include "./cryptlib/hex.h"
  3. #include "./cryptlib/files.h"
  4. #include "./cryptlib/cryptlib.h"
  5. #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
  6. #include <iostream>
  7. #include <string.h>
  8. using namespace std;
  9. using namespace CryptoPP;
  10. void DumpHash_SingleStep(CryptoPP::HashTransformation& hash, char const* szModuleName, std::string const& strData) {
  11.   //Can't use std::string for buffer;
  12.   // its internal storage might not be contiguous
  13.   SecByteBlock sbbDigest(hash.DigestSize());
  14.   hash.CalculateDigest(sbbDigest.begin(), (byte const*) strData.data(), strData.size());
  15.   cout << szModuleName << "SS:";
  16.   HexEncoder(new FileSink(cout)).Put(sbbDigest.begin(), sbbDigest.size());
  17.   cout << endl;
  18. }
  19. void DumpHash_MultiStep(CryptoPP::HashTransformation& hash, char const* szModuleName, std::string const& strDataPart1, std::string const& strDataPart2, std::string const& strDataPart3){
  20.   hash.Update((byte const*) strDataPart1.data(), strDataPart1.size());
  21.   hash.Update((byte const*) strDataPart2.data(), strDataPart2.size());
  22.   hash.Update((byte const*) strDataPart3.data(), strDataPart3.size());
  23.   //can't use std::string for buffer;
  24.   //its internal storage might not be contiguous
  25.   SecByteBlock sbbDigest(hash.DigestSize());
  26.  
  27.   hash.Final(sbbDigest.begin());
  28.   cout << szModuleName << "MS";
  29.   HexEncoder(new FileSink(cout)).Put(sbbDigest.begin(), sbbDigest.size());
  30.   cout << endl;
  31. }
  32.  void DumpHash_HashFilter(CryptoPP::HashTransformation& hash, char const* szModuleName, std::string const& strDataPart1, std::string const& strDataPart2, std::string const& strDataPart3){
  33.   //Here, we are free to use std::string as the destination,
  34.   //because StringSink uses the correct std::string interface to append data
  35.   string strDigest;
  36.   HashFilter hashFilter(hash, new StringSink(strDigest));
  37.   hashFilter.Put((byte const*) strDataPart1.data(), strDataPart1.size());
  38.   hashFilter.Put((byte const*) strDataPart2.data(), strDataPart2.size());
  39.   hashFilter.Put((byte const*) strDataPart3.data(), strDataPart3.size());
  40.   hashFilter.MessageEnd();
  41.   cout << szModuleName << "HF:";
  42.   StringSource(strDigest, truenew HexEncoder(new FileSink(cout)));
  43.   cout << endl;
  44. }
  45. void DumpHash(CryptoPP::HashTransformation& hash, char const* szModuleName, std::string const& strDataPart1, std::string const& strDataPart2, std::string const& strDataPart3){
  46.   DumpHash_SingleStep(hash, szModuleName, strDataPart1 + strDataPart2 + strDataPart3);
  47.   DumpHash_MultiStep(hash, szModuleName, strDataPart1, strDataPart2, strDataPart3);
  48.   DumpHash_HashFilter(hash, szModuleName, strDataPart1, strDataPart2, strDataPart3);
  49. }
  50. //Crypto++
  51. #include "./cryptlib/sha.h"
  52. #include "./cryptlib/ripemd.h"
  53. #include "./cryptlib/md5.h"
  54. #include "./cryptlib/crc.h"
  55. int main(){
  56.   using namespace std;
  57.   using namespace CryptoPP;
  58.   SHA sha;
  59.   SHA256 sha256;
  60.   RIPEMD160 ripemd160;
  61.   Weak::MD5 md5;
  62.   CRC32 crc32;
  63.   std::string strDataPart1 = "part 1;";
  64.   std::string strDataPart2 = "part two;";
  65.   std::string strDataPart3 = "PART THREE;";
  66.   try{
  67.     DumpHash(sha, "SHA", strDataPart1, strDataPart2, strDataPart3);
  68.     DumpHash(sha256, "SHA256", strDataPart1, strDataPart2, strDataPart3);
  69.     DumpHash(ripemd160, "RIPEMD160", strDataPart1, strDataPart2, strDataPart3);
  70.     DumpHash(md5, "MD5", strDataPart1, strDataPart2, strDataPart3);
  71.     DumpHash(crc32, "CRC32", strDataPart1, strDataPart2, strDataPart3);
  72.   }
  73.   catch(CryptoPP::Exception const& e){
  74.     cout << "CryptoPP::Exception caught:" << endl
  75.             << e.what() << endl;
  76.     return 1;
  77.   }
  78. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值