OpenSSL编程

文章目录

官方文档:https://www.openssl.org/docs/manmaster/man3/

哈希

封装了一下计算哈希的函数:

class Hash 
{
private:
    EVP_MD_CTX *ctx;
public:
    Hash();
    Hash(const string &type);
    void init(const string &type);
    void update(const string &msg);
    string hash();
};

Hash::Hash()
{
}
Hash::Hash(const string &type)
{
    init(type);
}
void Hash::init(const string &type)
{
    const EVP_MD *md = EVP_get_digestbyname(type.c_str());
    if(md == NULL)
    {
        cout<<"Unknown message digest "<<type<<endl;
        cout<<"eg: \"sha256\", \"md5\", etc...\n";
        exit(1);
    }
    ctx = EVP_MD_CTX_new();
    EVP_DigestInit_ex(ctx, md, NULL);

}
void Hash::update(const string &msg)
{
    EVP_DigestUpdate(ctx, msg.c_str(), msg.size());
}
string Hash::hash()
{
    unsigned char md_value[EVP_MAX_MD_SIZE];
    unsigned int md_len;
    EVP_DigestFinal_ex(ctx, md_value, &md_len);
    EVP_MD_CTX_free(ctx);
    std::stringstream ss;
    for(int i = 0; i < md_len; i++)
    {
        ss << std::hex << std::setw(2) << std::setfill('0') << (int)md_value[i];
    }
    return ss.str();
}

TODO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值