Calculate MD5 of a string in C++

23,562

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");
Share:
23,562
The Quantum Physicist
Author by

The Quantum Physicist

I did a PhD of particle physics back in the days, and then left academia to start a professional career with software development, because I love it. I still contribute to science by leading software development efforts for scientific experiments. I love science, but I love programming even more :-)

Updated on July 09, 2022

Comments

  • The Quantum Physicist
    The Quantum Physicist almost 2 years

    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.