0

我只想使用 SHA-3-512。所以我使用了KeccakCodePackage

我阅读了规范并使用了它们。要检查我的结果,我使用以下Online-Hash-Website

我对“一些数据”的结果是:

15d7fb5fcb81cf8f178cd9ea946c298db9d6b3d3509a35d369fc58cbc923fab549df95dffddb371a5ef21745b3bf7f7a15ee7785a0ee81b97e9d87911e

而在线转换器返回以下内容:

15d7fb5fcb081cf80f178cd9ea946c298db9d6b3d3509a35d369fc58cbc923fab549df95dffd0db371a5ef210745b30b0f7f7a15ee7785a0ee81b97e9d87911e

我使用了以下配置:

char* inputData = (char*)malloc(sizeof(char) * 15);
char* outputData = (char*)malloc(sizeof(char) * 1024);

inputData = "Some Data";
unsigned int rate = 576;
unsigned int capacity = 1024;
unsigned char suffix = 0x06;
unsigned int hashLength = 64;

int spongeResult = KeccakWidth1600_Sponge(rate, capacity , inputData, sizeof(inputData)+1, suffix , outputData, hashLength);

完整的代码可以在这里找到。

打印值的代码是:

    int i;
    for(i = 0; i < hashLength; i++){
        printf("%x", *(outputData + i) & 0xff);
    }

我意识到另一个哈希中有更多的零。所以,我的问题是:我的代码有什么问题?

编辑:这是整个程序:

#include <stdio.h>
#include <stdlib.h>
#include "KeccakCodePackage/bin/generic64/libkeccak.a.headers/KeccakSpongeWidth1600.h"

void main(){
    printf("%s\n", "Run Keccak Test");
    char* inputData = (char*)malloc(sizeof(char) * 15);
    char* outputData = (char*)malloc(sizeof(char) * 1024);


    inputData = "Some Data";
    unsigned int rate = 576;
    unsigned int capacity = 1024;
    unsigned char suffix = 0x06;
    unsigned int hashLength = 64;

    printf("%s", "Hash the following data: \n");
    printf("%s\n", inputData);

    int spongeResult = KeccakWidth1600_Sponge(rate, capacity , inputData, sizeof(inputData)+1, suffix , outputData, hashLength);

    if(spongeResult == 1){
        printf("%s", "Sponge was not successful\n");
    }else{
        printf("%s", "Sponge successful\n");

        int i;
        for(i = 0; i < hashLength; i++){
            printf("%x", *(outputData + i) & 0xff);
        }
    }

    printf("%s", "\nFinished Keccak test.\n");
}
4

1 回答 1

1

问题在于您打印值的方式。

    int i;
    for(i = 0; i < hashLength; i++){
        printf("%x", *(outputData + i) & 0xff);
    }

%x格式将为小于 16 的值打印单个十六进制数字。您需要使用前导 0 打印此类值,使用%02x.

于 2018-03-03T03:09:01.610 回答