Calculate MD5 of a string in C++

后端 未结 1 551
星月不相逢
星月不相逢 2021-02-18 21:39

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 has

相关标签:
1条回答
  • 2021-02-18 21:55

    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  -
    

    Alternatively, you could include a newline in your program version:

    std::string str("Hello\n");
    
    0 讨论(0)
提交回复
热议问题