Results 1 to 4 of 4

Thread: QByteArray comparison

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QByteArray comparison

    Maybe I lost my intelligence, but I really do not why this code do not behave correctly.

    Qt Code:
    1. bool loginDialog::check()
    2. {
    3. qDebug()<<"LD check"<<usernames<<passwords;
    4. qDebug()<<"LD input"<<username<<password<<usernames.contains(username.trimmed());
    5. if (usernames.contains(username.trimmed()))
    6. {
    7. p = passwords.at(usernames.indexOf(username));
    8. qDebug()<<"LD password"
    9. "input"<<p<<QCryptographicHash::hash(password.toLocal8Bit(),QCryptographicHash::Md5);
    10. in = QCryptographicHash::hash(password.toLocal8Bit(),QCryptographicHash::Md5);
    11.  
    12. if ( in == p)
    13. {
    14. qDebug()<<"ACCEPTED"<< p.toLocal8Bit() << in;
    15. return true;
    16. }
    17. else
    18. {
    19. qDebug()<<"NO ACCEPTED"<<in<<p;
    20. return false;
    21. SleeperThread::msleep(900);
    22. }
    23.  
    24. }
    25. return false;
    26. }
    To copy to clipboard, switch view to plain text mode 

    The output at run is as follow
    Qt Code:
    1. LD check ("administrator") ("!#/)zW¥§C?JJ?Ã")
    2. LD input "administrator" "admin" true
    3. LD passwordinput "!#/)zW¥§C?JJ?Ã" "!#/)zW¥§C?JJ?Ã"
    4. NO ACCEPTED "!#/)zW¥§C?JJ?Ã" "!#/)zW¥§C?JJ?Ã"
    To copy to clipboard, switch view to plain text mode 

    As you can see the variables "in" and "p" have the same value, but the "if" lands always in the NO ACCEPTED state.
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray comparison

    As you can see the variables "in" and "p" have the same value,
    No necessarily.
    They both have the same visible string, but probably one of them either has a non visble char value, or maybe the '\0' is missing or similar.
    Check if the length is the same too.

    also take note of the docs:
    Returns true if this byte array is equal to string str; otherwise returns false.

    The Unicode data is converted into 8-bit characters using QString::toAscii().

    The comparison is case sensitive.

    You can disable this operator by defining QT_NO_CAST_FROM_ASCII when you compile your applications. You then need to call QString::fromAscii(), QString::fromLatin1(), QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if you want to convert the byte array to a QString before doing the comparison.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray comparison

    "in" is a byte array, "p" is a string. To compare the two one has to be encoded to match the format of the other. After encoding the content may differ from the initial one. I'm pretty sure "Ã" can't be encoded using ASCII character set, same with most of the other characters you have in your password.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray comparison

    I always said myself to not work at night...
    However, I did it with the following code
    Qt Code:
    1. if (usernames.contains(username.trimmed()))
    2. {
    3. p = passwords.at(usernames.indexOf(username));
    4. qDebug()<<"LD password"
    5. "input"<<p<<QCryptographicHash::hash(password.toLocal8Bit(),QCryptographicHash::Md4).toHex();
    6. in = QString::fromLocal8Bit(QCryptographicHash::hash(password.toLocal8Bit(),QCryptographicHash::Md4).toHex());
    7.  
    8. if ( in == p)
    9. {
    10. qDebug()<<"ACCEPTED"<< p.toLocal8Bit() << in;
    11. return true;
    12. }
    13. else
    14. {
    15. qDebug()<<"NO ACCEPTED"<<in<<p<<in.size()<<in.size();
    16. return false;
    17. SleeperThread::msleep(900);
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    where the password is stored in a file as heX.
    Regards

Similar Threads

  1. file name comparison
    By mentalmushroom in forum Qt Programming
    Replies: 10
    Last Post: 15th March 2011, 22:54
  2. QHash add comparison function, how?
    By holst in forum Qt Programming
    Replies: 9
    Last Post: 30th March 2010, 19:49
  3. Replies: 9
    Last Post: 25th July 2009, 13:27
  4. String comparison
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2006, 12:31
  5. case insensitive comparison.
    By munna in forum Newbie
    Replies: 1
    Last Post: 7th May 2006, 16:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.