加密hmac结果在C中比C#更短

时间:2013-01-09 14:37:55

标签: c hmac sha256

有时你必须实现C代码和加密,所以我的hmac256或sha2_hmac实现的C代码实现有问题:

unsigned char key[19] = { "abcdef" };
size_t keylen = 18;
unsigned char input[64] = {"123456789"}; 
size_t ilen = 63;         
unsigned char output[32];
int is224 = 0;


sha2_hmac(key, keylen, input, ilen, output, is224);

char result[64] = {};
char result2[64] = {};

for(int j = 0; j < 32; j++) {
        sprintf(result, "%X", output[j]);
        strcat(result2, result);
}

但是当我将它与C#代码进行比较时,结果太短了。

1 个答案:

答案 0 :(得分:2)

您必须像这样调整sprintf()函数调用:

sprintf(result, "%02X", output[j]);