QCryptographicHash results mismatches
-
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
ormd5sum
. 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?
-
@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?
- Check the return result from
-
@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)
-
@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.
- Check the return result from
-
@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? -
MacOS is using
\r
I think. -
@Christian-Ehrlicher
Just, wow! Then so far as I'm concerned, it's not Unix! People have lied to me.... -
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:
-
I used the
Text
flag, without knowing what it does (my bad) to the data. -
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.
-
-
So you checked two different files and wondered why the hash did not match.
-
@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
-
@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...
-
@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.