Hey there. I'm having a very strange problem with creating sha256 hashes. I made a simple C console program that takes a file path as an argument and uses the standalone sha256 code that can be found here. I compiled the program using MinGW 5.1.6 on Windows 7 x64.
嘿。我在创建sha256哈希时遇到了一个非常奇怪的问题。我制作了一个简单的C控制台程序,它将文件路径作为参数,并使用可在此处找到的独立sha256代码。我在Windows 7 x64上使用MinGW 5.1.6编译了程序。
When testing the program on a file, the resultant hash is wrong. I made sure of this by using md5deep on the file, and then by using sha256sum on the file under Linux. I also verified it was not the code by compiling and running the same code on my Linux box with the same file; the hash it produced was identical to the ones produced by md5deep and sha256sum.
在文件上测试程序时,生成的哈希是错误的。我通过在文件上使用md5deep,然后在Linux下的文件上使用sha256sum来确保这一点。我还通过使用相同的文件在我的Linux机器上编译和运行相同的代码来验证它不是代码;它产生的哈希与md5deep和sha256sum产生的哈希相同。
I also adapted Aaron Gifford's sha256 implementation into a different version of my simple program and performed the test again on both Windows and Linux and ended up with the same result.
我还将Aaron Gifford的sha256实现改编为我的简单程序的不同版本,并在Windows和Linux上再次执行测试,最终得到了相同的结果。
Could it be possible that the issue is being caused by compiler flags that have not been switched on?
是否有可能该问题是由尚未打开的编译器标志引起的?
My knowledge of C isn't amazing and my knowledge of compiler options is even worse, so any help would be kindly appreciated.
我对C的了解并不令人惊讶,而且我对编译器选项的了解甚至更糟,所以任何帮助都会受到赞赏。
The code for the simple program is below:
简单程序的代码如下:
#include <stdio.h>
#include "sha256.h"
#define BUFLEN 16384
int main(int argc, char *argv[]) {
sha256_context ctx256;
sha256_starts(&ctx256);
int kl, l, fd;
unsigned char buf[BUFLEN];
FILE *file = (FILE*) 0;
char *filepath;
fd = fileno(stdin);
filepath = argv[1];
file = fopen(filepath, "r");
fd = fileno(file);
while ((l = read(fd, buf, BUFLEN)) > 0) {
kl += l;
sha256_update(&ctx256, buf, l);
}
fclose(file);
uint8 sha256sum[32];
sha256_finish(&ctx256, sha256sum);
int i;
for (i = 0; i < 32; i++) {
printf("%02x", sha256sum[i]);
}
printf("\n");
return 0;
}
1 个解决方案
#1
0
Binary mode gets ignored on Linux, but it applies in Windows. For reference on what it does, see http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx. In short, \r\n gets translated to \n in non-binary mode.
二进制模式在Linux上被忽略,但它适用于Windows。有关它的作用的参考,请参阅http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx。简而言之,\ r \ n在非二进制模式下转换为\ n。
#1
0
Binary mode gets ignored on Linux, but it applies in Windows. For reference on what it does, see http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx. In short, \r\n gets translated to \n in non-binary mode.
二进制模式在Linux上被忽略,但它适用于Windows。有关它的作用的参考,请参阅http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx。简而言之,\ r \ n在非二进制模式下转换为\ n。