【问题标题】:HashVerificationFilter and "message hash or MAC not valid" exceptionHashVerificationFilter 和“消息哈希或 MAC 无效”异常
【发布时间】:2016-06-02 05:00:06
【问题描述】:

在 Crpto++ 中,可以轻松地使用管道对输入进行哈希处理。

std::string out;
CryptoPP::SHA256 sha;
CryptoPP::StringSource ss(input,true,
                          new CryptoPP::HashFilter(sha,
                              new CryptoPP::HexEncoder(
                                  new CryptoPP::StringSink(out))));

现在为了验证给定消息 x 产生相同的哈希输出,我想使用 HashVerificationFilter。我已经尝试过了,但它不起作用。有人知道正确的语法吗?

const int flags = CryptoPP::HashVerificationFilter::THROW_EXCEPTION | CryptoPP::HashVerificationFilter::HASH_AT_END;
CryptoPP::SHA256 sha;
try
{
    CryptoPP::StringSource ss(input + out, true,
                              new CryptoPP::HashVerificationFilter(sha, NULL , flags));
}
catch(const CryptoPP::Exception& e)
{
    std::cerr << e.what() << std::endl;
    exit(1);
}

我得到了输出:

HashVerificationFilter: message hash or MAC not valid

【问题讨论】:

    标签: c++ cryptography crypto++


    【解决方案1】:
    std::string out;
    SHA256 sha;
    StringSource ss(input,true,
        new HashFilter(sha,
            new HexEncoder(
                new StringSink(out)
    )));
    

    HexEncode 你的哈希值。您需要在将其传递给过滤器之前对其进行解码:

    StringSource ss(input + out, true,
        new HashVerificationFilter(sha, NULL , flags)
    );
    

    或者,删除编码过滤器:

    std::string out;
    SHA256 sha;
    StringSource ss(input,true,
        new HashFilter(sha,
            new StringSink(out)
    ));
    

    【讨论】:

    • 移除十六进制编码器有效。一开始我并不需要它。
    猜你喜欢
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多