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
when i qDebug().noquotes() both strings they appear the same
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());
Apart from what @Christian-Ehrlicher just wrote, which looks right, what type is the column in the database (i.e.
query.value(2)) which currently holds your sha-encrypted string? Or are you developing that yourself? I'm interested.
@JonB It's an "nvarchar"
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
blobwould 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?
@JonB Converting to hex fixed the issue :)