Keccak SHA-3-512:遗漏了一个细节
Keccak SHA-3-512: Missed a detail
我只想使用 SHA-3-512。所以我用了 KeccakCodePackage.
我读了specification and used them.To check my result I use the following Online-Hash-Website。
我的 "Some Data" 结果是:
15d7fb5fcb81cf8f178cd9ea946c298db9d6b3d3509a35d369fc58cbc923fab549df95dffddb371a5ef21745b3bf7f7a15ee7785a0ee81b97e9d87911e
而在线转换器 returns 以下内容:
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");
}
问题在于您打印值的方式。
int i;
for(i = 0; i < hashLength; i++){
printf("%x", *(outputData + i) & 0xff);
}
%x
格式将为小于 16 的值打印单个十六进制数字。您需要使用 %02x
.
打印带有前导 0 的此类值
我只想使用 SHA-3-512。所以我用了 KeccakCodePackage.
我读了specification and used them.To check my result I use the following Online-Hash-Website。
我的 "Some Data" 结果是:
15d7fb5fcb81cf8f178cd9ea946c298db9d6b3d3509a35d369fc58cbc923fab549df95dffddb371a5ef21745b3bf7f7a15ee7785a0ee81b97e9d87911e
而在线转换器 returns 以下内容:
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");
}
问题在于您打印值的方式。
int i;
for(i = 0; i < hashLength; i++){
printf("%x", *(outputData + i) & 0xff);
}
%x
格式将为小于 16 的值打印单个十六进制数字。您需要使用 %02x
.