位操作-从安全哈希函数中对无符号字符进行建模



我有一个20字节的unsigned char test_SHA1[20],这是一个从哈希函数返回的值。使用下面的代码,我得到这个输出

unsigned char test_SHA1[20];
char hex_output[41];
for(int di = 0; di < 20; di++)
{
    sprintf(hex_output + di*2, "%02x", test_SHA1[di]);
}
printf("SHA1 = %sn", hex_output);
50 b9e78177f37e3c747f67abcc8af36a44f218f5

这个数字的最后9位是0x0f5(= 245十进制),我将通过对test_SHA1进行mod 512来获得。要对test_SHA1进行mod 512,我执行

int x = (unsigned int)test_SHA1 % 512;
printf("x = %dn", x);

但是x是158而不是245。

我建议按位使用0x1ff而不是使用%操作符。

最新更新