PDA

View Full Version : QByteArray comparison



giusepped
28th September 2011, 16:10
Maybe I lost my intelligence, but I really do not why this code do not behave correctly.



bool loginDialog::check()
{
qDebug()<<"LD check"<<usernames<<passwords;
qDebug()<<"LD input"<<username<<password<<usernames.contains(username.trimmed());
QString p;
QByteArray in;
if (usernames.contains(username.trimmed()))
{
p = passwords.at(usernames.indexOf(username));
qDebug()<<"LD password"
"input"<<p<<QCryptographicHash::hash(password.toLocal8Bit(),QC ryptographicHash::Md5);
in = QCryptographicHash::hash(password.toLocal8Bit(),QC ryptographicHash::Md5);

if ( in == p)
{
qDebug()<<"ACCEPTED"<< p.toLocal8Bit() << in;
return true;
}
else
{
qDebug()<<"NO ACCEPTED"<<in<<p;
return false;
SleeperThread::msleep(900);
}

}
return false;
}


The output at run is as follow


LD check ("administrator") ("!#/)zW¥§C?JJ?Ã")
LD input "administrator" "admin" true
LD passwordinput "!#/)zW¥§C?JJ?Ã" "!#/)zW¥§C?JJ?Ã"
NO ACCEPTED "!#/)zW¥§C?JJ?Ã" "!#/)zW¥§C?JJ?Ã"



As you can see the variables "in" and "p" have the same value, but the "if" lands always in the NO ACCEPTED state.
Regards

high_flyer
28th September 2011, 16:30
As you can see the variables "in" and "p" have the same value,
No necessarily.
They both have the same visible string, but probably one of them either has a non visble char value, or maybe the '\0' is missing or similar.
Check if the length is the same too.

also take note of the docs:

Returns true if this byte array is equal to string str; otherwise returns false.

The Unicode data is converted into 8-bit characters using QString::toAscii().

The comparison is case sensitive.

You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. You then need to call QString::fromAscii(), QString::fromLatin1(), QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if you want to convert the byte array to a QString before doing the comparison.

wysota
28th September 2011, 19:50
"in" is a byte array, "p" is a string. To compare the two one has to be encoded to match the format of the other. After encoding the content may differ from the initial one. I'm pretty sure "Ã" can't be encoded using ASCII character set, same with most of the other characters you have in your password.

giusepped
29th September 2011, 10:02
I always said myself to not work at night...
However, I did it with the following code


if (usernames.contains(username.trimmed()))
{
p = passwords.at(usernames.indexOf(username));
qDebug()<<"LD password"
"input"<<p<<QCryptographicHash::hash(password.toLocal8Bit(),QC ryptographicHash::Md4).toHex();
in = QString::fromLocal8Bit(QCryptographicHash::hash(pa ssword.toLocal8Bit(),QCryptographicHash::Md4).toHe x());

if ( in == p)
{
qDebug()<<"ACCEPTED"<< p.toLocal8Bit() << in;
return true;
}
else
{
qDebug()<<"NO ACCEPTED"<<in<<p<<in.size()<<in.size();
return false;
SleeperThread::msleep(900);
}

}

where the password is stored in a file as heX.
Regards