Important: Please read the Qt Code of Conduct -

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();
    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


    However == 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.

  • Lifetime Qt Champion

    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());

  • @Nerd0
    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"

  • @Nerd0

    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?

  • @JonB Converting to hex fixed the issue :)

Log in to reply