String compare mismatch
-
I'm trying to get a string from my Azure SQL server to my client and compare it with a string thats in the program, these strings are encrypted with sha256.
I encrypt like this (the string that is in the program is encrypted the same way)
QCryptographicHash hasher(QCryptographicHash::Sha256); QByteArray string_bytes= string.toLocal8Bit(); hasher.addData(string_bytes); QByteArray array_encrypted = hasher.result(); QString string_encrypted = QString(array_encrypted);
I print the string I get from the SQL server like this
database_encrypted_string= query.value(2).toString();
when i qDebug().noquotes() both strings they appear the same
h????LP,\??,n?y??0L&?? h????LP,\??,n?y??0L&??
However qstring.compare == 0 doesn't seem to work and it says they are different. I know this has something to do with escape characters, but I'm not sure how to fix it.
-
You should convert and store the data hex encoded since the QByteArray does not contain printable characters but ordinary bytes
QString string_encrypted = QString::fromLatin1(array_encrypted.toHex());
-
-
It's an "nvarchar"
Is that right for a sha256? @Christian-Ehrlicher describes that as "ordinary bytes". So I would have thought some kind of
blob
would be needed?Anyway, I totally agree with him that the encrypted password should be stored
toHex()
anyway. Then it isn't an issue. So, are you in charge of what's stored in the database, or is that external to you?