生成 blake2b 哈希时崩溃
Crash when generating blake2b hash
我正在尝试以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
,是所请求哈希的名称。 "SHA-512"
适用于 SHA512,"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) 的手册中注意到
布莱克2b
如果定义了BOTAN_HAS_BLAKE2B则可用。
最近设计的哈希函数。在 64 位处理器上非常快。可以输出 1 到 64 字节之间的任何长度的哈希,这是通过将值传递给具有所需长度的构造函数来指定的。
是否有可能没有默认设置?我将如何设置它?我的名字可能错了吗?
问题可能是名称的大写。我正在使用 Botan 2.9.0 和"Blake2b"工作,而"BLAKE2b"崩溃。博坦的主分支也接受"BLAKE2b",但这只是最近才添加的。默认输出长度设置为 512 位,您可以通过传递例如 "Blake2b(256)" 或直接调用 Blake2b 构造函数(如在链接提交中)来调整它。
小型工作示例(主要.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
相关文章:
- 自定义哈希表实现-将字符串映射到整数时出现内存错误
- 在构建时执行execute_process以在构建时获取git提交哈希
- 将 Git 提交哈希读取到C++时出错
- boost SHA1 哈希与 md5sum/sha1sum 的运行时比较
- 用于对编译时字符串进行哈希处理的延迟指针
- 是否可以将哈希值作为编译时常数
- 测试完美哈希函数时超出范围错误
- C++ 如何在使用哈希函数时计算冲突次数
- 生成 blake2b 哈希时崩溃
- 编译哈希算法时出现内存访问冲突错误
- std::string_view编译时哈希
- 是我们迭代时更改哈希映射的行为,已定义
- 删除元素时,如何防止std::unordereded_map的重新哈希
- C++:将自定义哈希键值从 unordered_map 输出到 std::cout 时出错
- 创建自定义哈希函数unordered_map时出现问题
- 编译时哈希C++0x
- 当用户自定义哈希函数在 unordered_map 中时,无法解码 g++ 中模糊的编译器错误
- 编译字符类型的时一致性哈希
- 逐行散列文件时错误的 md5 哈希值
- 为什么我在为矢量实现这个简单的哈希时出现错误<int>?