利用crypto++库,实现计算string的md5值

时间:2022-04-27
本文章向大家介绍利用crypto++库,实现计算string的md5值,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include <cryptopp/hex.h>
#include <cryptopp/files.h>
#include <cryptopp/md5.h>
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 
/*
 * Description: to calculate the hash of the message, and return it(string).
 * Input:
 * message: need to calculate its hash value
 * Output:
 * return the hash value(string) of the message.
 */
string md5_string(const string & message)
{
    string digest;
    Weak::MD5 md5;
    StringSource(message, true,
                new HashFilter(md5, new HexEncoder(new StringSink(digest))));
    return digest;
}