我需要在主函数中打印一个字符串(名为十六进制)。该字符串是digestInHex函数的参数,该函数用于keccak函数。所以这是函数:
void digestInHex(unsigned long long state[][5], unsigned char *buffer,
unsigned char *bufferLocation, int bufferSize, int size, char *hex)
{
unsigned char *byte;
int i, j;
unsigned long long *x;
const char *hexValues = "0123456789abcdef";
byte = (unsigned char *)malloc(size * sizeof(char));
hex = (char *)malloc(((size << 1) + 1) * sizeof(char));
hex[size << 1] = '\0';
/* Padding */
bufferLocation[0] = 1;
++bufferLocation;
while (bufferLocation != &buffer[bufferSize / 8])
{
bufferLocation[0] = 0;
++bufferLocation;
}
buffer[(bufferSize >> 3) - 1] |= 0x80;
bufferLocation = buffer;
x = (unsigned long long *)buffer;
for (j = 0; j * 64 < bufferSize; ++j)
{
state[j / 5][j % 5] |= x[j];
}
round(state);
/* Squeezing */
memcpy(byte, state, size);
reset(state);
bufferLocation = buffer;
for (i = 0; i < size; ++i)
{
hex[i << 1] = hexValues[byte[i] >> 4];
hex[(i << 1) + 1] = hexValues[byte[i] & 15];
}
free(byte);
// printf("%s\n", hex);
free(hex);
}
void keccak(const char *str, enum bitLength hashValueBitLength, char *hex)
{
int i = 0;
int j;
unsigned char *buffer;
unsigned char *bufferLocation;
const int bufferSize = 1600 - (hashValueBitLength * 2);
unsigned long long *x;
unsigned long long state[5][5];
buffer = (unsigned char *)malloc(bufferSize * sizeof(char));
bufferLocation = buffer;
reset(state);
while (str[i] != '\0')
{
bufferLocation[0] = (unsigned char)str[i];
++bufferLocation;
if (bufferLocation == &buffer[bufferSize / 8])
{
bufferLocation = buffer;
x = (unsigned long long *)buffer;
for (j = 0; j * 64 < bufferSize; ++j)
{
state[j / 5][j % 5] |= x[j];
}
round(state);
}
++i;
}
digestInHex(state, buffer, bufferLocation, bufferSize, hashValueBitLength / 8, hex);
free(buffer);
}
如您所见,keccak函数最后使用了digestInHex函数。 digestInHex中的十六进制字符串保留给定输入的哈希输出。
总的来说,我需要使用切换案例比较新旧项目的时间值。为此,我需要运行keccak 1百万次才能看到更清晰的时差。没有看到哈希输出一百万次,我无法直接在digestInHex中打印十六进制字符串,这就是为什么我在digestInHex中进行了十六进制注释的printf的原因。
另外,我也想在切换情况下显示哈希输出。但是当我这样做时它会打印null。因此,如何打印哈希输出,如“ 4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15”?这是主要的:
int main()
{
int i;
clock_t begin, end;
double timeSpend;
int n;
printf("Enter 1 to see Old Project's time value\n");
printf("Enter 2 to see New Project's time value\n\n");
printf("Enter 3 to see Old Project's hash output\n");
printf("Enter 4 to see New Project's hash output\n\n");
printf("Please enter a value according to above: ");
iterator:
scanf_s("%d", &n);
switch (n)
{
case 1:
begin = clock();
for (i = 0; i < 1000000; ++i)
keccakOld("The quick brown fox jumps over the lazy dog", KECCAK_256, hexOld);
end = clock();
timeSpend = (double)(end - begin) / CLOCKS_PER_SEC;
printf("%f sec.\n", timeSpend);
break;
case 2:
begin = clock();
for (i = 0; i < 1000000; ++i)
keccak("The quick brown fox jumps over the lazy dog", KECCAK_256, hex);
end = clock();
timeSpend = (double)(end - begin) / CLOCKS_PER_SEC;
printf("%f sec.\n", timeSpend);
break;
case 3:
keccakOld("The quick brown fox jumps over the lazy dog", KECCAK_256, hexOld);
printf("%s\n", hexOld);
break;
case 4:
keccak("The quick brown fox jumps over the lazy dog", KECCAK_256, hex);
printf("%s\n", hex);
break;
default:
printf("Please re-enter a correct value: ");
goto iterator;
break;
}
return 0;
}
最佳答案
要获得对分配的内存的引用,并在下面的digestInHex()
中将其设置为main()
,请传递对指针hex
的引用。
在(重新)分配它但在打印之后,还要调整它使其释放。
为此,请如下调整代码:
更改
void digestInHex(unsigned long long state[][5], unsigned char* buffer,
unsigned char* bufferLocation, int bufferSize, int size, char* hex)
{
成为
void digestInHex(unsigned long long state[][5], unsigned char* buffer,
unsigned char* bufferLocation, int bufferSize, int size, char ** hex)
{
free(*hex);
在
free()
末尾删除对free(hex)
的呼叫。在
digestInHex()
中,将所有digestInHex()
更改为hex
。更改
void keccak(const char* str, enum bitLength hashValueBitLength, char* hex)
{
成为
void keccak(const char* str, enum bitLength hashValueBitLength, char** hex)
{
仅使用
(*hex)
保留呼叫digestInHex()
。在
hex
中定义和初始化:char * hex = NULL;
将所有呼叫更改为
main()
和keccak()
以接听digestInHex)
但接听&hex
。同样,在
hex
中添加最后一个main()
后再返回。