QCryptographicHash::Md5 VS md5sum [Solved]



  • Hi all,
    I need to generate an MD5 sum in Qt 5.

    This is my example code:
    @
    QCryptographicHash md5_generator(QCryptographicHash::Md5);
    md5_generator.addData("123456789");
    qDebug() << md5_generator.result().toHex();
    @
    and I get:
    @
    25f9e794323b453885f5181f1b624d0b
    @

    Now I create a file with the same content:
    @
    123456789
    @
    and I try md5sum linux command I get:
    @
    $ md5sum test
    b2cfa4183267af678ea06c7407d4d6d8
    @

    Where is the problem?


  • Moderators

    I checked it with another tool and it's the same as Qt result.
    Are you sure you didn't put a newline at the end of the file? It will also count as content.


  • Moderators

    Maybe the Linux tool also takes some file metadata (encoding, maybe BOM is present?) into account? In your Qt code you pass only the raw data. For Linux, you pass a whole file.


  • Moderators

    I just checked for a string "123456789\n" and the checksum is indeed b2cfa4183267af678ea06c7407d4d6d8
    So just fix your file.



  • Thanks for the answers!

    The file was created with "vi" and I didn't put new line at the end of the text.

    There probably is in the file a kind of EOF that I don't see.

    Thanks again!



  • By default vi will ensure the last line of the file has a line feed. Your current file will be 10 bytes (11 on Windows). If you are actually using vim, instead of generic vi, you can turn that off:
    http://vim.wikia.com/wiki/File_format#Terminator_after_last_line

    To create your test file without the line feed (UNIX):
    @
    $ echo -n '123456789' > test.txt
    $ ls -l test.txt
    -rw-r--r-- 1 chrisw users 9 Apr 4 11:29 test.txt
    @



  • Very interesting.
    Now it's more clear.

    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.