所以我正在尝试编写自己的“keccaksum”程序,除了运行
for i in {1..50}; do ./keccaksum 256 test.o; done
输出
4d4cc035e544cd4837b45550094dd3c419e380af3b0c74109c00053c7ed82040 test.o
大多数时候和
b19d21947b7228da366b4d26f232b87e21999ff1a220c37c9bed6553260068c0 test.o
一些时间。
这里的相关功能是
void printsum(const char *_fname, FILE *_f, size_t size){
uint64_t state[25];
uint8_t *hash = malloc(size * sizeof(uint8_t));
uint8_t buf[25];
uint8_t tmp[144];
register int i, rsize, rsizew;
register size_t j;
rsize = 200 - 2 * size;
rsizew = rsize / 8;
//Clear the state
memset(state, 0, sizeof(state));
while(1) {
//read up to rsize bytes, then do work
j = fread(buf, 1, rsize, _f);
//check some stuff
if(feof(_f)){
break;
} else if(ferror(_f)){
fprintf(stderr, "Error when reading %s.\n", _fname);
goto fin;
} else {
//First few blocks (i.e. not last block)
for(i = 0; i < rsizew; i++)
state[i] ^= ((uint64_t *)buf)[i];
keccakf(state, KECCAK_ROUNDS);
}
}
//Last block + padding
memcpy(tmp, buf, j);
tmp[j++] = 1;
memset(tmp + j, 0, rsize - j);
tmp[rsize - 1] |= 0x80;
for(i = 0; i < rsizew; i++)
state[i] ^= ((uint64_t *)tmp)[i];
keccakf(state, KECCAK_ROUNDS);
//copy hash
memcpy(hash, state, size);
//print
for(i = 0; i < size; i++) printf("%02x", hash[i]);
printf(" %s\n", _fname);
fin:
if(_f != stdin) fclose(_f);
free(hash);
}
这让我相信这段代码的某些部分是未定义的,我不知道什么可能是未定义的。