C 中 64 位架构上的 128 位值
我正在 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 int
s 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望“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.
GCC 中的 128 位整数
128-bits integers in GCC