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(filename.c_str(), true, &hashfilter);
return digest;
}

本文介绍了如何使用CryptoPP库进行MD5加密和SHA1签名。CryptoPP是一个强大的密码库,提供了方便的编码器进行数据格式化。文章详细讲述了MD5加密的简单应用,并提及了其在文件校验中的作用。同时,也提到了SHA1签名的实现,与MD5类似。
最低0.47元/天 解锁文章
4298

被折叠的 条评论
为什么被折叠?



