C++ 中文件的 MD5 哈希值
如何在C++中获取文件的MD5哈希值?
How to get the MD5 hash of a file in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在C++中获取文件的MD5哈希值?
How to get the MD5 hash of a file in C++?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
下面是
md5sum
命令的直接实现,该命令计算并显示命令行上指定的文件的 MD5。 它需要链接到 OpenSSL 库 (gcc md5.c -o md5 -lssl
) 才能工作。 它是纯 C 语言,但您应该能够轻松地将其适应您的 C++ 应用程序。Here's a straight forward implementation of the
md5sum
command that computes and displays the MD5 of the file specified on the command-line. It needs to be linked against the OpenSSL library (gcc md5.c -o md5 -lssl
) to work. It's pure C, but you should be able to adapt it to your C++ application easily enough.您可以自己实现 MD5 算法(示例遍布网络),或者您可以链接到 OpenSSL 库并使用 OpenSSL 的摘要函数。
下面是获取字节数组 MD5 的示例:
You can implement the MD5 algorithm yourself (examples are all over the web), or you can link against the OpenSSL libs and use OpenSSL's digest functions.
here's an example to get the MD5 of a byte array:
对于从“https://stackoverflow.com/questions/4393017/md5-implementation-in-c”重定向的任何人”,因为它被错误地标记为重复项。
此处的示例有效:
http://www.zedwood.com/article/cpp-md5 -function
如果你在 VC++2010 中编译,那么你需要将他的 main.cpp 更改为:
如果你要读取 char * 数组而不是 a,则必须稍微更改 MD5 类字符串来回答此页面上的问题。
编辑:
显然修改 MD5 库尚不清楚,这里有一个完整的 VC++2010 解决方案,以方便您包含 char * 的:
https://github.com/alm4096/MD5-Hash-Example-VS
这里有一些解释:
我只是将以下内容添加到 MD5 库中:
MD5.cpp:
MD5.h:
For anyone redirected from "https://stackoverflow.com/questions/4393017/md5-implementation-in-c" because it's been incorrectly labelled a duplicate.
The example located here works:
http://www.zedwood.com/article/cpp-md5-function
If you are compiling in VC++2010 then you will need to change his main.cpp to this:
You will have to change the MD5 class slightly if you are to read in a char * array instead of a string to answer the question on this page here.
EDIT:
Apparently modifying the MD5 library isn't clear, well a Full VC++2010 solution is here for your convenience to include char *'s:
https://github.com/alm4096/MD5-Hash-Example-VS
A bit of an explanation is here:
I have simply added the following into the MD5 library:
MD5.cpp:
MD5.h:
我现在需要这样做,需要一个适合 c++11、boost 和 openssl 的跨平台解决方案。 我以 D'Nabre 的 解决方案为起点,并将其归结为以下内容:
快速测试可执行文件演示:
一些链接笔记:
Linux:
-lcrypto -lboost_iostreams
Windows:
-DBOOST_ALL_DYN_LINK libeay32.lib ssleay32.lib
I needed to do this just now and required a cross-platform solution that was suitable for c++11, boost and openssl. I took D'Nabre's solution as a starting point and boiled it down to the following:
A quick test executable demonstrates:
Some linking notes:
Linux:
-lcrypto -lboost_iostreams
Windows:
-DBOOST_ALL_DYN_LINK libeay32.lib ssleay32.lib
md5.h
还具有对于大文件非常有用的MD5_*
函数非常简单,不是吗? 转换为字符串也非常简单:
md5.h
also haveMD5_*
functions very useful for big fileVery simple, isn`t it? Convertion to string also very simple:
使用 Crypto++,您可以执行以下操作:
我需要非常类似的东西,因为我无法读取数 GB 文件只是为了计算哈希值。 理论上我可以对它们进行内存映射,但我必须支持 32 位平台 - 这对于大文件来说仍然是个问题。
Using Crypto++, you could do the following:
I have a need for something very similar, because I can't read in multi-gigabyte files just to compute a hash. In theory I could memory map them, but I have to support 32bit platforms - that's still problematic for large files.
@D'Nabre 对 C++ 的实现进行了修改。 不要忘记在最后使用 -lcrypto 进行编译:
gcc md5.c -o md5 -lcrypto
。A rework of impementation by @D'Nabre for C++. Don't forget to compile with -lcrypto at the end:
gcc md5.c -o md5 -lcrypto
.