Windows C SHA256 无效生成问题
嘿。我在创建 sha256 哈希值时遇到了一个非常奇怪的问题。我制作了一个简单的 C 控制台程序,它采用文件路径作为参数,并使用可以在 此处。我在 Windows 7 x64 上使用 MinGW 5.1.6 编译了该程序。
在文件上测试程序时,生成的哈希值是错误的。我通过在文件上使用 md5deep ,然后在 Linux 下的文件上使用 sha256sum 来确保这一点。 我还通过在我的 Linux 机器上使用相同的文件编译并运行相同的代码来验证这不是代码;它生成的哈希值与 md5deep 和 sha256sum 生成的哈希值相同。
我还将 Aaron Gifford 的 sha256 实现改编为我的简单程序的不同版本,并在 Windows 和 Linux 上再次执行测试,最终得到相同的结果。
该问题是否可能是由尚未打开的编译器标志引起的?
我对 C 的了解并不令人惊奇,而我对编译器选项的了解甚至更糟,因此我们将不胜感激。
简单程序的代码如下:
#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;
}
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.
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.
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.
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.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
二进制模式在 Linux 上会被忽略,但它适用于 Windows。有关其功能的参考,请参阅 http://msdn .microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx。简而言之,\r\n 在非二进制模式下被转换为 \n。
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.