如何轻松应用Crypto ++哈希函数? [英] How to easily apply Crypto++ hash functions?

查看:283
本文介绍了如何轻松应用Crypto ++哈希函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何轻松地使用Crypto ++库中的哈希函数吗? 我尝试将这些代码用于SHA1和MD5.我在StringSink的行上有很多错误.错误如下:

Can someone help me how can I easily use hash functions from Crypto++ library? I tried used these codes for SHA1 and MD5. I have many errors on line where is StringSink. The errors are like:

undefined reference to `CryptoPP::StringSinkTemplate::StringSinkTemplate(std::string&)'

感谢您的帮助.

 // SHA
CryptoPP::SHA1 sha1;
std::string source = "Hello";
std::string hash = "";
CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));
std::cout << hash;

// MD5
CryptoPP::MD5 hash;
byte digest[ CryptoPP::MD5::DIGESTSIZE ];
std::string message = "abcdefghijklmnopqrstuvwxyz";

hash.CalculateDigest( digest, (byte*) message.c_str(), message.length() );

CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach( new CryptoPP::StringSink( output ) );
encoder.Put( digest, sizeof(digest) );
encoder.MessageEnd();

std::cout << output << std::endl;

推荐答案

我在StringSink所在的行上有很多错误

I have many errors on line where is StringSink

这行与StringSink对我来说看起来不错.您应该提供遇到的确切错误.

This line with the StringSink looks OK to me. You should provide the exact error that you are encountering.

与StringSink一起出现的错误是:对`CryptoPP :: StringSinkTemplate :: StringSinkTemplate(std :: string&)'的未定义引用

Error on line with StringSink is: undefined reference to `CryptoPP::StringSinkTemplate::StringSinkTemplate(std::string&)'

根据您提供的其他信息,请参阅为以下对象构建和链接测试代码加密++

Based on the additional information you provided, see Building and linking test code for Crypto++

如何轻松使用Crypto ++哈希函数?

How use easily apply Crypto++ hash functions?

以下内容来自 ChannelSwitch 类上的Crypto ++ Wiki.由于需要MD5,因此需要先#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1,然后再#include <cryptopp/md5.h>.之后,您可以在Weak::MD5中找到MD5.

Below is from the Crypto++ wiki on the ChannelSwitch class. Since you want MD5, you need to #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1, and then #include <cryptopp/md5.h>. After that, you can find MD5 in Weak::MD5.

string message = "Now is the time for all good men to come to the aide of their country";
if(argc == 2 && argv[1] != NULL)
    message = string(argv[1]);

string s1, s2, s3, s4;
SHA1 sha1; SHA224 sha224; SHA256 sha256; SHA512 sha512;

HashFilter f1(sha1, new HexEncoder(new StringSink(s1)));
HashFilter f2(sha224, new HexEncoder(new StringSink(s2)));
HashFilter f3(sha256, new HexEncoder(new StringSink(s3)));
HashFilter f4(sha512, new HexEncoder(new StringSink(s4)));

ChannelSwitch cs;
cs.AddDefaultRoute(f1);
cs.AddDefaultRoute(f2);
cs.AddDefaultRoute(f3);
cs.AddDefaultRoute(f4);

StringSource ss(message, true /*pumpAll*/, new Redirector(cs));

cout << Message: " << message << endl;
cout << "SHA-1: " << s1 << endl;
cout << "SHA-224: " << s2 << endl;
cout << "SHA-256: " << s3 << endl;
cout << "SHA-512: " << s4 << endl;

运行该程序会产生如下所示的结果:

A run of the program produces the results shown below:

$ ./cryptopp-test.exe password
Message: password
SHA-1: 5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8
SHA-224: D63DC919E201D7BC4C825630D2CF25FDC93D4B2F0D46706D29038D01
SHA-256: 5E884898DA28047151D0E56F8DC6292773603D0D6AABBDD62A11EF721D1542D8
SHA-512: B109F3BBBC244EB82441917ED06D618B9008DD09B3BEFD1B5E07394C706A8BB9
         80B1D7785E5976EC049B46DF5F1326AF5A2EA6D103FD07C95385FFAB0CACBC86

这篇关于如何轻松应用Crypto ++哈希函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆