You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Botan生成Blake2b哈希时崩溃问题求助

Fixing BLAKE2b Crash in Botan-Based Hash Function

Yep, your hunch is totally right! That access violation crash happens because BLAKE2b doesn’t have a default output length (unlike fixed-length hashes like SHA-512), and calling Botan::HashFunction::create("BLAKE2b") returns a null pointer since it can’t initialize the function without knowing how long the hash should be. Let’s break down the fix.

Why the Crash Happens

Most standard hashes (SHA-1, SHA-2 family, etc.) have fixed output sizes, so Botan can create them with just the algorithm name. But BLAKE2b is designed to support variable output lengths (1 to 64 bytes). Without explicitly specifying this length, the create() method can’t construct a valid instance—hence the null pointer that causes your access violation.

Also, double-check that you’ve defined BOTAN_HAS_BLAKE2B during compilation (as the manual mentions); if this macro isn’t set, Botan won’t include BLAKE2b’s implementation at all, which would also lead to a null pointer.

Two Ways to Fix It

1. Directly Construct a Blake2b Instance

Instead of relying on the generic create() method, explicitly instantiate Botan::Blake2b with your desired output length. This gives you more control and avoids the ambiguity of the algorithm string:

#include <Botan/Blake2b.h>
#include <stdexcept>

std::string hex_hash(std::string &in, const std::string &HASH, size_t blake2_output_len = 64) {
    std::unique_ptr<Botan::HashFunction> hash;

    if (HASH == "BLAKE2b") {
        // Validate length (BLAKE2b requires 1-64 bytes)
        if (blake2_output_len < 1 || blake2_output_len > 64) {
            throw std::invalid_argument("BLAKE2b output length must be between 1 and 64 bytes");
        }
        // Directly create Blake2b with specified length
        hash = std::make_unique<Botan::Blake2b>(blake2_output_len);
    } else {
        // Handle other hash algorithms as before
        hash = Botan::HashFunction::create(HASH);
        if (!hash) {
            throw std::invalid_argument("Unsupported hash algorithm: " + HASH);
        }
    }

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

2. Embed Length in the Algorithm Name String

If you prefer to keep using the generic create() method, you can append the desired length to the algorithm name in parentheses. For example, to get a 64-byte (512-bit) BLAKE2b hash, use "BLAKE2b(64)" instead of just "BLAKE2b":

// Call the function with length-specified algorithm name
std::string my_input = "test data";
std::string blake2_hash = hex_hash(my_input, "BLAKE2b(64)");

This works because Botan’s create() method parses the length parameter from the string and initializes BLAKE2b correctly. Just make sure the number inside parentheses is between 1 and 64.

Key Notes

  • Always validate the output length for BLAKE2b—values outside 1-64 bytes will throw an exception.
  • Confirm BOTAN_HAS_BLAKE2B is defined in your build configuration (e.g., via compiler flags like -DBOTAN_HAS_BLAKE2B or in your project settings).

内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:10:26