C 中 64 位架构上的 128 位值

发布于 2024-12-28 17:35:43 字数 314 浏览 5 评论 0原文

我正在 64 位 Linux 上用 C 实现 SHA-512。SHA-512 接受 128 位作为要散列的数据长度。目前,我使用两个 64 位 long int 来组成 128 位值,但我故意将零放入前 64 位,并将长度存储在剩余的低 64 位中。我为什么要这样做?

好吧,我认为在 64 位架构上,我可能永远不会获得超过 264 的值,或者可以吗?这是在 64 位架构上实现 SHA-512 的正确方法吗?还是有什么方法可以在 64 位架构上满足我不知道的 128 位值?需要注意的是,我根据 FIPS 180-3 实施 SHA-512。

任何帮助将不胜感激。

I am implementing SHA-512 on 64-bit Linux in C. SHA-512 accepts 128 bits as the length of data to be hashed. Currently I am taking two 64-bit long ints to comprise the 128-bit value but I am puttingg zeros intentionally to the first 64 bits and the length is stored in the remaining lower 64 bits. Why am I doing this?

Well I think on 64-bit architectures, I probably won't ever get a value more than 264 or can I? Is this the right way to implement SHA-512 on 64-bit architecture or is there any way to cater 128-bit values on 64-bit architectures that I don't know? The point to note is that I am implementing SHA-512 according to FIPS 180-3.

Any help would highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不打扰别人 2025-01-04 17:35:43

我希望“64 位长整型”实际上是指无符号长整型...说实话,在您完全理解如何使用较小的整数完成 bigint 算术之前,不要执行自己的 SHA-512 实现。

如果您想实现符合要求的内容,请使用完整的 128 位。如果您使用单个函数调用来散列整个块,则几乎不需要完整的 128 位,64 位就可以了。但为了保持一致,您应该允许对数据包中的完整块进行哈希处理。因此,您有 3 个如下函数:

Init - 初始化下一个哈希的内部状态。

添加 - 将数据块添加到当前散列,具有不完整块的内部缓冲和 128 位大小的内部计数。它需要一块内存,因此干净的 C 风格接口将使用 size_t 来表示字节数。然而,64 位无符号整数足以满足预期目的并简化计数。

完成 - 进行填充,完成哈希并返回最终哈希值。

显然,这种方法需要一个额外的外部可见结构来保存当前状态。

I hope that by "64 bit long ints", you really mean unsigned long ints... Seriously, don't do your own SHA-512 implementation until you fully understand how bigint arithmetic is done using smaller integers.

If you want to implement something conforming, then use the full 128 bit. If you use one single function call to hash the complete block, you will hardly need the full 128 bit, 64 will be ok. But to be conforming, you should allow to hash a complete block in packets. Thus you have 3 functions like these:

Init - Initializes the internal state for the next hashing.

Add - Adds a block of data to the current hashing, with internal buffering of incomplete blocks and internal count of the 128 bit size. It takes a block of memory, so a clean C-style interface would use size_t for the number of bytes. However, a 64 bit unsigned integer is good enough for the intended purpose and simplifies the counting.

Finish - Does the padding, completes the hashing and returns the final hash value.

Obviously, this approach needs an additional externally visible struct to hold the current state.

温柔一刀 2025-01-04 17:35:43

GCC 中的 128 位整数

作为扩展,整数标量类型 __int128 支持整数模式宽度足以容纳 128 位的目标。只需编写 __int128 表示有符号 128 位整数,或 unsigned __int128 表示无符号 128 位整数。对于具有小于 128 位宽度的 long long 整数的目标,GCC 不支持表示类型 __int128 的整数常量。

128-bits integers in GCC

As an extension the integer scalar type __int128 is supported for targets having an integer mode wide enough to hold 128-bit. Simply write __int128 for a signed 128-bit integer, or unsigned __int128 for an unsigned 128-bit integer. There is no support in GCC to express an integer constant of type __int128 for targets having long long integer with less then 128 bit width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文