1

I am developing a Qt application on Windows 8 64 bit OS. I have encountered this weird case where MD5 message digest is just 4 chars long(=32 bit only.) . Except this exceptional input I get 16 chars(=128 bit) message digest string.

MD5 message digest should be fixed length

MD5- Wikipedia

my code snippet

qDebug()<<"Case 1:=> ";

message1="HYQPTPORKTWKJSVIVXHS1.130hello world!";
input.append(message1);
output=QCryptographicHash::hash(input,QCryptographicHash::Md5);
QString digest1(QString(output).toAscii());
qDebug()<<"md5  string: "<<digest1;
qDebug()<<"length :"<<digest1.length();

qDebug()<<"Case 2:=>";
input=""; // clears previous input
message2="HYQPTPORKTWKJSVIVXHS1.131hello world!";  // put anything else than message1
input.append(message2);
output=QCryptographicHash::hash(input,QCryptographicHash::Md5);
QString digest2(QString(output).toAscii());
qDebug()<<"md5  string: "<<digest2;
qDebug()<<"length :"<<digest2.length();

Output

 Case 1:=>  
md5  string:  ")QÄ" 
length : 4 // here I'm expecting 16
Case 2:=> 
md5  string:  "X,öéö< Ú4Îu" 
length : 16 

Am I doing something wrong?

Community
  • 1
  • 1
stackOverflow
  • 179
  • 1
  • 2
  • 18
  • The best waqy to look at something like this is that the library code is correct and the meitake is in the way it is used. – zaph Mar 15 '16 at 18:26

1 Answers1

6

You're interpreting the 128-bit binary MD5 as ASCII. If you have a single byte (eight bits) with '0' as the value, in ASCII on C++, that is considered the "end of string" marker.

You need to represent the MD5 as hexadecimal in ASCII, not try to read it as-is in ASCII.

I don't do Qt, but a quick search says something like this will do what you want:

QString digest2(output.toHex()));

You should always make sure (especially when dealing with languages like C++) that you're fully cognizant of what's actually going on under the hood, and in this particular case, what is the actual underlying datatype of all your operations. Everything is just bytes in the memory, and C++ gives you the ability to "read" those bytes as whatever you want - even if it's not ready or meant to be.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • @stackOverflow Also depending on the print functions some bytes may not print and make you think the output is shorter than it is if you are trying to treat 8-bit data bytes as string characters. – zaph Mar 15 '16 at 18:22