PDA

View Full Version : Storing A hex value as a string?



Splatify
6th February 2011, 14:55
Hi this is the code i have:


QByteArray hash = QCryptographicHash::hash((ui->PasswordTextbox->text()).toUtf8(), QCryptographicHash::Md5);
qDebug() << hash.toHex();
string password = // The code I need here

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

JohannesMunk
6th February 2011, 16:05
Hi there!

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



QByteArray hash = QCryptographicHash::hash(QString("1234").toUtf8(), QCryptographicHash::Md5);
QString password(hash.toHex());
qDebug() << password;
HIH

Joh

squidge
6th February 2011, 16:30
I would also suggest you salt your password before running it through md5.

Lesiok
6th February 2011, 17:44
QString password = QString(hash.toHex());

Splatify
7th February 2011, 07:26
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 :D