Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. String compare mismatch

String compare mismatch

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 873 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nerd0
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • N Nerd0

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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.

        N 1 Reply Last reply
        0
        • JonBJ JonB

          @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.

          N Offline
          N Offline
          Nerd0
          wrote on last edited by
          #4

          @JonB It's an "nvarchar"

          JonBJ 1 Reply Last reply
          0
          • N Nerd0

            @JonB It's an "nvarchar"

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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?

            N 1 Reply Last reply
            0
            • JonBJ JonB

              @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?

              N Offline
              N Offline
              Nerd0
              wrote on last edited by
              #6

              @JonB Converting to hex fixed the issue :)

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved