Results 1 to 5 of 5

Thread: Storing A hex value as a string?

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Storing A hex value as a string?

    Hi this is the code i have:

    Qt Code:
    1. QByteArray hash = QCryptographicHash::hash((ui->PasswordTextbox->text()).toUtf8(), QCryptographicHash::Md5);
    2. qDebug() << hash.toHex();
    3. string password = // The code I need here
    To copy to clipboard, switch view to plain text mode 

    What I am trying to do is when the user enters the password, it gets encrypted with Md5. Then I want this md5 encrypted version to get stored as a string.

    For example if the user enters '1234' as their password it gets converted to '81dc9bdb52d04dc20036dbd8313ed055'. This is outputted to the debugger using hash.toHex(); at the moment. I want this value to then be stored to a string so i can compare it against a password stored in a database.

    how do i store the value '81dc9bdb52d04dc20036dbd8313ed055' to a string?

    Thanks for your time and trouble

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Storing A hex value as a string?

    Hi there!

    How about using the constructor of QString, that takes a QByteArray as parameter?

    Qt Code:
    1. QByteArray hash = QCryptographicHash::hash(QString("1234").toUtf8(), QCryptographicHash::Md5);
    2. QString password(hash.toHex());
    3. qDebug() << password;
    To copy to clipboard, switch view to plain text mode 
    HIH

    Joh

  3. The following user says thank you to JohannesMunk for this useful post:

    Splatify (7th February 2011)

  4. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Storing A hex value as a string?

    I would also suggest you salt your password before running it through md5.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Storing A hex value as a string?

    Qt Code:
    1. QString password = QString(hash.toHex());
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lesiok for this useful post:

    Splatify (7th February 2011)

  7. #5
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Storing A hex value as a string?

    Thank you this is now working great. I will salt my password. I just wanted to get this to work before i added anymore code which could have further confused me!!

    Once again thanks

Similar Threads

  1. Flag storing
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 05:26
  2. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 08:51
  3. Storing and Retrieving a QFlags
    By darkadept in forum Qt Programming
    Replies: 3
    Last Post: 4th October 2007, 18:53
  4. QVariant - storing QIcons[]
    By moowy in forum Qt Programming
    Replies: 7
    Last Post: 25th August 2006, 00:41
  5. storing captions
    By vijay anandh in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 07:54

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.