简体   繁体   English

C sha1实现在Unix上不起作用

[英]C sha1 implementation not working on Unix

I'm using this implementation of SHA1 in C. Works fine on Windows but it does not output correct hashes on Unix (tried it on Ubuntu and Mac OS 10.8). 我正在C中使用SHA1的此实现 。在Windows上运行正常,但在Unix上无法输出正确的哈希(在Ubuntu和Mac OS 10.8上进行过尝试)。 Furthermore, on Ubuntu it outputs different hash from the same message. 此外,在Ubuntu上,它从同一消息输出不同的哈希值。

I guess I could use another implementation, just curious why that happens. 我想我可以使用另一种实现,只是很好奇为什么会这样。

EDIT 编辑

Thanks, you guys are right. 谢谢,你们是对的。 Changed it to 更改为

typedef unsigned int UINT4;

seems to work fine. 似乎工作正常。

Are these 64bit unix'es? 这些是64位Unix吗?

/* UINT4 defines a four byte word */
typedef unsigned long int UINT4;

will actually be 8 bytes on 64 bit Linux (but 4 bytes on 64 bit Windows) 在64位Linux上实际上为8字节(但在64位Windows上为4字节)

https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models https://zh.wikipedia.org/wiki/64-位_计算#64-位_数据_模型

If any of the platforms is 64-bit and has a 64-bit unsigned long , this code might have issues: 如果任何平台是64位且具有64位unsigned long ,则此代码可能会出现问题:

/* UINT4 defines a four byte word */
typedef unsigned long int UINT4;

Didn't read the code closely enough to see if the way UINT4 is used would break if it's actually 8 bytes, but it sounds fishy. 没有足够仔细地阅读代码,以查看使用UINT4的方式是否实际上是8个字节是否会中断,但这听起来可疑。

typedef unsigned long int UINT4; typedef unsigned long int UINT4;

Maybe your Unix implementation uses 64-bit unsigned longs. 也许您的Unix实现使用64位无符号长型。

Try 尝试

#include <stdint.h>

/* ... */

typedef uint32_t UINT4;

and use a C99 compiler. 并使用C99编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM