Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCryptographicHash results mismatches
Forum Updated to NodeBB v4.3 + New Features

QCryptographicHash results mismatches

Scheduled Pinned Locked Moved Solved General and Desktop
qcryptographichfile
12 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B bogdan_bdg

    There is a simple piece of code that is expected to get the MD5 (or SHA1 or SHA256 or other) hash of a file dropped on to a Qt application's window:

    	if(droppedFile.open(QFile::ReadOnly | QFile::Text))
    	{
    		QCryptographicHash md5hash(QCryptographicHash::Md5);
    		md5hash.addData(&droppedFile);
    		qDebug() << "MD5 hash: " << md5hash.result().toHex();
    	}
    

    When I run this under GNU/Linux the output hash is the same as a result of running command line tool md5 or md5sum. When I run the very same code under MacOS - I get different hashes from the application and command line.

    As the files dropped are actually text-based, I also tried reading them line by line and adding data as QString. The results are exactly the same - they match on Linux and don't match on MacOS.

    Any clues what am I doing wrong?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @bogdan_bdg
    No, but:

    • Check the return result from md5hash.addData().
    • Did you try removing | QFile::Text? Examples I have seen do not open as a text file.
    • You might like to state what version of Qt you are using, and how you built it?
    B 1 Reply Last reply
    1
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @JonB said in QCryptographicHash results mismatches:

      Did you try removing | QFile::Text? Examples I have seen do not open as a text file.

      Because the file must not be treated as text file here - you want a hash over the bytes, not the text (with it's different line endings)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      2
      • JonBJ JonB

        @bogdan_bdg
        No, but:

        • Check the return result from md5hash.addData().
        • Did you try removing | QFile::Text? Examples I have seen do not open as a text file.
        • You might like to state what version of Qt you are using, and how you built it?
        B Offline
        B Offline
        bogdan_bdg
        wrote on last edited by
        #4

        @JonB said in QCryptographicHash results mismatches:

        @bogdan_bdg
        No, but:

        • Check the return result from md5hash.addData().
        • Did you try removing | QFile::Text? Examples I have seen do not open as a text file.
        • You might like to state what version of Qt you are using, and how you built it?

        That's probably the fastest forum-based problem solution I ever experienced :-))

        • Did you try removing | QFile::Text? Examples I have seen do not open as a text file.

        These are text files so I thought it might be good to be explicit about it. Removing the QFile::Text made it work as expected, thanks a lot!

        FWIW - Qt is the latest 5.x. Application built from within Qt Creator.

        1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @JonB said in QCryptographicHash results mismatches:

          Did you try removing | QFile::Text? Examples I have seen do not open as a text file.

          Because the file must not be treated as text file here - you want a hash over the bytes, not the text (with it's different line endings)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #5

          @Christian-Ehrlicher said in QCryptographicHash results mismatches:

          Because the file must not be treated as text file here - you want a hash over the bytes, not the text (with it's different line endings)

          [Now that the OP's question is solved I feel I can ask this.]

          Indeed, and I knew that, hence I guessed that was the problem. But here is what I do not understand. I do not, and have not, used MacOS. I thought that MacOS was Linux-like. But then how does it have any "text mode", it should surely be like Linux? Are you saying it's Linux, but doesn't use \n as line ending in files?? I should really like to understand how MacOS can differ from Linux here, please?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            MacOS is using \r I think.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              MacOS is using \r I think.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @Christian-Ehrlicher
              Just, wow! Then so far as I'm concerned, it's not Unix! People have lied to me....

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bogdan_bdg
                wrote on last edited by bogdan_bdg
                #8

                Erm... no! Current "Macos", the same as previously "OS X" is a BSD derived "unixalike" and it does employ the regular "unix style" LF character (\n) as the EOL mark. It is the Commodore 64 and the so-called "Classic Macos" which employs CR character (\r).

                But your question prompts me to update my response, because I was as confused (if not more) as you. The cause of the discrepancy I observed was twofold:

                1. I used the Text flag, without knowing what it does (my bad) to the data.

                2. The files I tested under GNU/Linux worked fine because they originated on the same machine. The file I tested on the Mac came from a different machine and once I got the clue about Text I inspected the file and found out that it has "Windows style" CRLF combination at the end of every line.

                The two combined caused things to break only on the Mac.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  So you checked two different files and wondered why the hash did not match.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  B 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    So you checked two different files and wondered why the hash did not match.

                    B Offline
                    B Offline
                    bogdan_bdg
                    wrote on last edited by
                    #10

                    @Christian-Ehrlicher said in QCryptographicHash results mismatches:

                    So you checked two different files and wondered why the hash did not match.

                    No - please check the original question

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • B bogdan_bdg

                      @Christian-Ehrlicher said in QCryptographicHash results mismatches:

                      So you checked two different files and wondered why the hash did not match.

                      No - please check the original question

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @bogdan_bdg said in QCryptographicHash results mismatches:

                      No - please check the original question

                      The files I tested under GNU/Linux worked fine because they originated on the same machine. The file I tested on the Mac came from a different machine and once

                      You said it by your own. Even the filesize did not match then...

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      B 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @bogdan_bdg said in QCryptographicHash results mismatches:

                        No - please check the original question

                        The files I tested under GNU/Linux worked fine because they originated on the same machine. The file I tested on the Mac came from a different machine and once

                        You said it by your own. Even the filesize did not match then...

                        B Offline
                        B Offline
                        bogdan_bdg
                        wrote on last edited by bogdan_bdg
                        #12

                        @Christian-Ehrlicher No, and I asked you to check the original question rather than the latest update, which you seem to have trouble understanding without the original context. So here's the quote from the original question for you:

                        When I run this [code in question] under GNU/Linux the output hash is the same as a result of running command line tool md5 or md5sum. When I run the very same code under MacOS - I get different hashes from the application and command line.

                        I never wrote I was trying to compare hashes between two different OSes (and files for that matter). The mismatch was between hash given by the Qt code I quoted and the regular command line tool hashing a file on the same OS.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved