我在 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;
}
这段代码正确吗?有什么问题(明显的或微妙的)吗?