我在Linux内核中使用Crypto API(MD5)。它“正在工作”,但我不确定它是否正常工作。作为测试,我使用此字符串作为示例:
Here is a completely different setup that should produce a different fingerprint. Let's see.
生成的哈希是
534d78034b774b6266f2189576f8c6e3
似乎合法。因此,我查看在线网站http://www.sha1-online.com/以查看其结果
07a90df929448eb67b00a6e01a202519
显然是错的。这是我的代码:
static int fingerprint(void)
{
struct scatterlist sg;
struct crypto_hash *tfm;
struct hash_desc desc;
unsigned char * output;
unsigned char * buf;
int i;
output = kmalloc(sizeof(*output) * 16, GFP_KERNEL);
memset(output, 0x00, 16);
buf = "Here is a completely different setup that should produce a different fingerprint. Let's see.";
tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
desc.tfm = tfm;
desc.flags = 0;
sg_init_one(&sg, buf, 92);
crypto_hash_init(&desc);
crypto_hash_update(&desc, &sg, 92);
crypto_hash_final(&desc, output);
for(i = 0; i < 16; i++)
{
printk("%02x", output[i]);
}
printk("\n");
return 0;
}
这段代码是否正确?它有任何问题(明显或微妙的)吗?