CryptoPP:md5加密、sha1签名
CryptoPP是一个强大的密码库,官网是https://www.cryptopp.com/。上面有比较详细的具体例子和说明文档,不过例子程序稍显片面,无法满足所有的应用场景,把这2天研究的一些加解密算法封装一下分享出来。
1、md5加密
这个很简单没什么好说的,CryptoPP最好的地方是有编码器可以简单快捷的格式化输出流,不用再丢md5加密后的数据进行处理,这里用了HexEncoder
std::string crypto::md5(std::string text)
{
std::string digest;
CryptoPP::Weak1::MD5 md5;
CryptoPP::HashFilter hashfilter(md5);
hashfilter.Attach(new CryptoPP::HexEncoder(new CryptoPP::StringSink(digest), false));
hashfilter.Put(reinterpret_cast<const unsigned char*>(text.c_str()), text.length());
hashfilter.MessageEnd();
return digest;
}
另外一个常用的场景是文件的md5加密码,用于校验文件传输的正确性。
std::string crypto::md5source(std::string filename)
{
std::string digest;
CryptoPP::Weak1::MD5 md5;
CryptoPP::HashFilter hashfilter(md5);
hashfilter.Attach(new CryptoPP::HexEncoder(new CryptoPP::StringSink(digest), false));
CryptoPP::FileSource(filen