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. Qt SQL Server - Hashed password
QtWS25 Last Chance

Qt SQL Server - Hashed password

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 530 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.
  • A Offline
    A Offline
    Arash Mitooie
    wrote on last edited by
    #1

    Hello everyone
    I've got this line of SQL query to check the validity of a username password :

    if exists (select * from Acc where username = :un and userpassword = HASHBYTES('SHA2_256', :pw)) select 1 else select 0
    

    It works correctly when executed directly in SQL Server Management studio but when I bind values and query from Qt it won't work.
    What am I doing wrong? I'd appreciate any help. I'll paste the complete code section in Qt below :

        q->prepare("if exists (select * from Acc where username = :un and userpassword = HASHBYTES('SHA2_256', :pw)) select 1 else select 0");
        q->bindValue(":un", un);
        q->bindValue(":pw", pw);
        if (q->exec())
        {
            q->next();
            if (q->value(0).toInt() == 0)
            {
                QMessageBox::critical(this, "", "User not found", QMessageBox::Ok);
            }
            else
            {
                //execute form . . . 
            }
    
    JonBJ 1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      If pw is a QString it should work, could you try a
      qDebug() << q->executedQuery();
      to see what the bind() calls resolved to.

      A 1 Reply Last reply
      1
      • hskoglundH hskoglund

        If pw is a QString it should work, could you try a
        qDebug() << q->executedQuery();
        to see what the bind() calls resolved to.

        A Offline
        A Offline
        Arash Mitooie
        wrote on last edited by
        #3

        @hskoglund
        Thanks hskoglund. It is a QString in Qt. The query executes but yields wrong result. It returns 0 when executed from Qt, while when directly done in SSMS, selects 1.
        I checked a thousand times whether I'm binding the exact strings I use in SSMS. I get them from line edits in Qt:

            QString un = ui->lineEditUsername->text();
            QString pw = ui->lineEditPassword->text();
        
        1 Reply Last reply
        0
        • hskoglundH Offline
          hskoglundH Offline
          hskoglund
          wrote on last edited by
          #4

          I also have some apps for SQL Server but I don't use bind() calls, instead I compose the query string manually, like this:

          QString s = QString("if exists (select * from Acc where username = '%1' and userpassword = HASHBYTES('SHA2_256', '%2')) select 1 else select 0").arg(un).arg(pw);
          

          just to be able to do a qDebug() or trace the SQL call...

          A 1 Reply Last reply
          1
          • hskoglundH hskoglund

            I also have some apps for SQL Server but I don't use bind() calls, instead I compose the query string manually, like this:

            QString s = QString("if exists (select * from Acc where username = '%1' and userpassword = HASHBYTES('SHA2_256', '%2')) select 1 else select 0").arg(un).arg(pw);
            

            just to be able to do a qDebug() or trace the SQL call...

            A Offline
            A Offline
            Arash Mitooie
            wrote on last edited by
            #5

            @hskoglund
            For heaven sake that worked!
            I am now very very thankfull, hskoglund :-)
            I (being a noobie) also learned a new Technic :-)
            cool . . .

            JonBJ 1 Reply Last reply
            0
            • A Arash Mitooie

              Hello everyone
              I've got this line of SQL query to check the validity of a username password :

              if exists (select * from Acc where username = :un and userpassword = HASHBYTES('SHA2_256', :pw)) select 1 else select 0
              

              It works correctly when executed directly in SQL Server Management studio but when I bind values and query from Qt it won't work.
              What am I doing wrong? I'd appreciate any help. I'll paste the complete code section in Qt below :

                  q->prepare("if exists (select * from Acc where username = :un and userpassword = HASHBYTES('SHA2_256', :pw)) select 1 else select 0");
                  q->bindValue(":un", un);
                  q->bindValue(":pw", pw);
                  if (q->exec())
                  {
                      q->next();
                      if (q->value(0).toInt() == 0)
                      {
                          QMessageBox::critical(this, "", "User not found", QMessageBox::Ok);
                      }
                      else
                      {
                          //execute form . . . 
                      }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Arash-Mitooie
              Start by splitting the statement into two, let's confirm it can find the username but not the userpassword.

              1 Reply Last reply
              0
              • A Arash Mitooie

                @hskoglund
                For heaven sake that worked!
                I am now very very thankfull, hskoglund :-)
                I (being a noobie) also learned a new Technic :-)
                cool . . .

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

                @Arash-Mitooie
                I hate to be mean to my friend @hskoglund , but I don't think you should do it that way. As shown it will error on some passwords, and is open to injection. Binding variables is a much safer technique than putting stings in-line.

                A 1 Reply Last reply
                0
                • JonBJ JonB

                  @Arash-Mitooie
                  I hate to be mean to my friend @hskoglund , but I don't think you should do it that way. As shown it will error on some passwords, and is open to injection. Binding variables is a much safer technique than putting stings in-line.

                  A Offline
                  A Offline
                  Arash Mitooie
                  wrote on last edited by
                  #8

                  @JonB
                  Thanks JonB. I tried the username alone, and it works but the password alone doesn't work via bindValue. I had read in another post that the safe way to do is to use bindValue, that's why I initially tried that. But I really don't know why it doesn't work . . .

                  1 Reply Last reply
                  0

                  • Login

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