gpt4 book ai didi

c - 哈希函数的 openssl 实现是面向位的还是面向字节的?

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:13 28 4
gpt4 key购买 nike

一切都在标题中。我找不到关于这个具体细节的任何信息,当我去检查实现时(我将在这里发布,你可以在头文件 md32_common.h 中找到它),我真的不能告诉...

这里是 HASH_UPDATE 函数,它被每个哈希函数更新函数调用(即 MD5_Update(...))

int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
{
const unsigned char *data = data_;
unsigned char *p;
HASH_LONG l;
size_t n;

if (len == 0)
return 1;

l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
/*
* 95-05-24 eay Fixed a bug with the overflow handling, thanks to Wei
Dai
* <weidai@eskimo.com> for pointing it out.
*/
if (l < c->Nl) /* overflow */
c->Nh++;
c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
* 16-bit */
c->Nl = l;

n = c->num;
if (n != 0) {
p = (unsigned char *)c->data;

if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
memcpy(p + n, data, HASH_CBLOCK - n);
HASH_BLOCK_DATA_ORDER(c, p, 1);
n = HASH_CBLOCK - n;
data += n;
len -= n;
c->num = 0;
/*
* We use memset rather than OPENSSL_cleanse() here
deliberately.
* Using OPENSSL_cleanse() here could be a performance issue.
It
* will get properly cleansed on finalisation so this isn't a
* security problem.
*/
memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
} else {
memcpy(p + n, data, len);
c->num += (unsigned int)len;
return 1;
}
}

n = len / HASH_CBLOCK;
if (n > 0) {
HASH_BLOCK_DATA_ORDER(c, data, n);
n *= HASH_CBLOCK;
data += n;
len -= n;
}

if (len != 0) {
p = (unsigned char *)c->data;
c->num = (unsigned int)len;
memcpy(p, data, len);
}
return 1;
}

最佳答案

虽然像 MD5 这样的散列函数通常对比特流进行操作,但软件实现通常对字节进行操作,以避免位移带来的速度损失(并且因为数据通常以字节为单位或在任何情况下都以更大的字节为单位进行处理)。在您发布的代码中,行

l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;

len 参数乘以 8(通过向左移动 3 次)将字节计数转换为位计数,所以我认为可以肯定地说这段代码在内部使用位,但期望传递一个字节流。

关于c - 哈希函数的 openssl 实现是面向位的还是面向字节的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43346977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com