【问题标题】:Blake2b hash native C library usageBlake2b 哈希原生 C 库使用
【发布时间】:2020-08-27 03:15:33
【问题描述】:

我正在尝试在我的项目中使用本机 C 库(这个 https://github.com/BLAKE2/BLAKE2),但我不清楚如何使用它:似乎可以使用分步版本(blake2b_init, blake2b_update 和 blake2b_final) 和更简单的一个 (blake2b)。

在这里 (https://www.rfc-editor.org/rfc/rfc7693#appendix-A) 我找到了关于它的完整解释和一个使用示例(第 26 页),他们只是这样做(我也不想使用密钥):

blake2b(md, outlen, NULL, 0, in, inlen);

我的一个问题是,在库源代码 (blake2.h) 中,我找到了一个“简单 API”,如下所示

int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );

所以语法是不同的:输入和键的位置是交换的。

此外,在上一个问题 (BLAKE2 input parameters) 中,据说应该检查结果(正确的是当它为 0 时)。我知道他们在这里使用 blake2s,但结果的含义对于 blake2b 也应该是相同的。

我的主要问题是我尝试了两种用法,但结果总是-1。所以,我认为我没有正确使用该库。

这是我的代码。

const char* pInput = "test text";
char* pOutput = new char[128];
int nRes = blake2b(pOutput, 128, pInput, strlen(pInput), NULL, 0);

const char* pInput = "test text";
char* pOutput = new char[128];
int nRes = blake2b(pOutput, 128, NULL, 0, pInput, strlen(pInput));

结果总是-1,在输出中我没有我的哈希。

有没有人使用过这个库和/或有关于它的更新文档来显示正确的用法?

【问题讨论】:

    标签: c++ c hash


    【解决方案1】:

    问题是您要求blake2b() 产生 128 字节的输出,而它最多只能产生 32 字节。在头文件中有一个enumBLAKE2B_OUTBYTES。所以写:

    const char *pInput = "test text";
    char *pOutput = new char[BLAKE2B_OUTBYTES];
    int nRes = blake2b(pOutput, BLAKE2B_OUTBYTES, pInput, strlen(pInput), NULL, 0);
    

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      相关资源
      最近更新 更多