我的目标是读取存储在 bio 中的数据(假设它用于写操作)并计算其 md5 指纹。数据可以是任何东西。我做了几次尝试。这是我最近的尝试:
void fingerprint(struct bio * bio, unsigned char * result)
{
char * place;
char * cpy;
int total;
struct buffer_head * bh;
struct scatterlist sg;
struct hash_desc desc = {
.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC),
.flags = CRYPTO_TFM_REQ_MAY_SLEEP
};
total = 0;
cpy = kmalloc(sizeof(char) * bio->bi_io_vec[i].bv_len, GFP_KERNEL);
bh = (struct buffer_head *)bio->bi_io_vec[i].bv_page->private;
place = bh->b_data + bio->bi_io_vec[i].bv_offset;
DPRINTK("%u", bio->bi_io_vec[i].bv_offset);
DPRINTK("%u", place);
memcpy(cpy, place, bio->bi_io_vec[i].bv_len);
DPRINTK("%x", place);
DPRINTK("%x", cpy);
sg_init_one(&sg, (u8 *)cpy, bio->bi_io_vec[i].bv_len);
crypto_hash_init(&desc);
crypto_hash_update(&desc, &sg, bio->bi_io_vec[i].bv_len);
total += bio->bi_io_vec[i].bv_len;
DPRINTK("%u", bio->bi_vcnt);
DPRINTK("%u", total);
crypto_hash_final(&desc, result);
kfree(cpy);
}
哈希的结果非常相似,如果不完全相同的话,它们总是偶数。我将结果打印为 long long 和十六进制。为什么会这样?