Search code examples
c++qthashqstring

Getting Sha1 hash from QString


In my Qt5.6.1 program, I have to get a Sha-1 hash from QString, but I get incorrect result. I'm trying to use QCryptographicHash library.

QString str = "ABCDEFGH";    
QString hash = QString::fromStdString(QCryptographicHash::hash(str.toStdString().c_str(), QCryptographicHash::Sha1).toStdString());
// hash == "?^??[?\u0000??v??\u0015??.b??v"

What should I change in that case?


Solution

  • I think this answer will be useful for you it is for md5 How to create MD5 hash in Qt?

    instead of str.toStdString().c_str() try using str.toUtf8() like this form the previous answer

    QString hash = QString("%1").arg(QString(QCryptographicHash::hash(str.toUtf8(),QCryptographicHash::Sha1).toHex()))