生成blake2b哈希时崩溃

问题描述 投票:0回答:1

我正在尝试用Botan编写哈希函数作为我的后端,即:

std::string hex_hash(std::string &in, const std::string &HASH)
{
std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(HASH));

return Botan::hex_encode(hash->process(in));
}

HASH是一个std::string,它是所请求哈希的名称。即用于SHA512的"SHA-512"和用于BLAKE2b的"BLAKE2b"

botan支持的任何其他哈希都被处理并输出,但BLAKE2b没有并抛出异常:

Exception thrown at 0x00007FF661382C5A in test.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

然后崩溃。

我在botan(https://botan.randombit.net/manual/hash.html)手册中注意到了这一点

BLAKE2b

如果定义了BOTAN_HAS_BLAKE2B,则可用。

最近设计的哈希函数。在64位处理器上非常快。可以输出1到64个字节之间的任何长度的散列,这是通过将值传递给具有所需长度的构造函数来指定的。

是否可能没有默认设置?我该怎么设置它?有可能我的名字错了吗?

c++ hash botan
1个回答
1
投票

问题可能是名称的大写。我正在使用Botan 2.9.0和“Blake2b”工作,而“BLAKE2b”崩溃。 Botan的主分支也接受“BLAKE2b”,但这只是添加了recently。默认输出长度设置为512位,您可以通过传递它来调整它“Blake2b(256)”或直接调用Blake2b构造函数(如在链接提交中)。

小工作示例(main.cpp):


#include <iostream>
#include <memory>
#include <string>

#include "botan/hash.h"
#include "botan/hex.h"

std::string hex_hash(const std::string& in, const std::string& HASH) {
    std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(HASH));
    return Botan::hex_encode(hash->process(in));
}

int main(int argc, char** argv) {
    std::cout << "Blake2b " << hex_hash("abc", "Blake2b") << std::endl;
    std::cout << "Blake2b(256) " << hex_hash("abc", "Blake2b(256)") << std::endl;
    return 0;
}
$ g++ `pkgconf --cflags --libs botan-2` main.cpp && ./a.out 
Blake2b BA80A53F981C4D0D6A2797B69F12F6E94C212F14685AC4B74B12BB6FDBFFA2D17D87C5392AAB792DC252D5DE4533CC9518D38AA8DBF1925AB92386EDD4009923
Blake2b(256) BDDD813C634239723171EF3FEE98579B94964E3BB1CB3E427262C8C068D52319

如果这对您不起作用,还要确保您的Botan是使用Blake2b支持编译的,例如:

$ grep BLAKE /usr/include/botan-2/botan/build.h 
#define BOTAN_HAS_BLAKE2B 20130131
© www.soinside.com 2019 - 2024. All rights reserved.