Keccak SHA-3-512:错过了一个细节

时间:2018-03-03 03:03:12

标签: c sha-3 keccak

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

我阅读specification并使用它们。要检查我的结果,请使用以下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);

可以找到完整的代码here

打印值的代码是:

    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");
}

1 个答案:

答案 0 :(得分:1)

问题在于你打印价值的方式。

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

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