使用C ++计算字符串的MD5

编程入门 行业动态 更新时间:2024-10-28 02:34:57
本文介绍了使用C ++计算字符串的MD5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个很好的例子,内存映射文件计算文件的MD5哈希值。

我想更改它来计算字符串的MD5哈希值。

例如:

(include #include< openssl / md5.h>

无符号字符结果[MD5_DIGEST_LENGTH];如果你想运行一个文件, boost :: iostreams :: mapped_file_source src(path); MD5((unsigned char *)src.data(),src.size(),result); std :: ostringstream sout; sout<< std :: hex<< std :: setfill('0'); for(long long c:result) { sout<< std :: setw(2)<< } return sout.str();

我所做的更改是:

std :: string str(Hello); unsigned char result [MD5_DIGEST_LENGTH]; MD5((unsigned char *)str.c_str(),str.size(),result); std :: ostringstream sout; sout<< std :: hex<< std :: setfill('0'); for(long long c:result) { sout<< std :: setw(2)<< } return sout.str();

但这会产生结果:

8b1a9953c4611296a827abf8c47804d7

命令 $ md5sum <<

$

pre>

为什么结果不一致?哪一个错了?

感谢。

所以我得到了正确答案。从终端调用 md5sum 的正确方法是:

$ printf' %s'Hello| md5sum

避免包含新行。

$ b $

$

$ b $ m b

您可以看到bash <<< 操作符添加换行符:

$ od -ta<<< Hello 0000000 H ello nl 0000006

要避免这种情况,请使用 printf :

$ printf'%s'Hello | od -ta 0000000 H e l l o 0000005 $ printf'%s'您好| md5sum 8b1a9953c4611296a827abf8c47804d7 -

I have a nice example of memory mapped files that calculates the MD5 hash of a file. That works fine with no problems.

I would like to change it to calculate the MD5 hash of a string.

So the example is:

(include #include <openssl/md5.h> to run this code, and also boost stuff if you want to run the one with the file)

unsigned char result[MD5_DIGEST_LENGTH]; boost::iostreams::mapped_file_source src(path); MD5((unsigned char*)src.data(), src.size(), result); std::ostringstream sout; sout<<std::hex<<std::setfill('0'); for(long long c: result) { sout<<std::setw(2)<<(long long)c; } return sout.str();

The change I made is:

std::string str("Hello"); unsigned char result[MD5_DIGEST_LENGTH]; MD5((unsigned char*)str.c_str(), str.size(), result); std::ostringstream sout; sout<<std::hex<<std::setfill('0'); for(long long c: result) { sout<<std::setw(2)<<(long long)c; } return sout.str();

But this produces the result:

8b1a9953c4611296a827abf8c47804d7

While the command $ md5sum <<< Hello gives the result:

09f7e02f1290be211da707a266f153b3

Why don't the results agree? Which one is wrong?

Thanks.

EDIT:

So I got the right answer which is ticked down there. The correct way to call md5sum from terminal is:

$ printf '%s' "Hello" | md5sum

To avoid the new line being included.

解决方案

You are passing a final newline to the md5sum program, but not to your code.

You can see that the bash<<< operator adds a newline:

$ od -ta <<<Hello 0000000 H e l l o nl 0000006

To avoid this, use printf:

$ printf '%s' Hello | od -ta 0000000 H e l l o 0000005 $ printf '%s' Hello | md5sum 8b1a9953c4611296a827abf8c47804d7 -

更多推荐

使用C ++计算字符串的MD5

本文发布于:2023-10-22 22:46:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!