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. Converting QByteArray to QString after hashing
Qt 6.11 is out! See what's new in the release blog

Converting QByteArray to QString after hashing

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 3.1k 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.
  • P Offline
    P Offline
    Paul Orasan
    wrote on last edited by
    #1

    Hello everyone!
    I am trying to use the QCryptographicHash library for my application, but in testing it I have a problem. After hashing, when I try to get the hashed QByteArray to QString and show it in the console I get a weird string.
    Code:

    #include <QCoreApplication>
    #include <QCryptographicHash>
    #include <QDebug>
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QString input="helloworld";
        QByteArray result=QCryptographicHash::hash(input.toLocal8Bit(), QCryptographicHash::Md5);
        QString inputHash=QString::fromLocal8Bit(result);
        qDebug() << input << " " << input.length();
        qDebug() << inputHash << " " << inputHash.length();
    }
    

    What am I doing wrong and how can I get the correct string?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The hash function returns a QByteArray which is not printable - it's a hash.
      You can use QByteArray::toHex() to represent the data as hex values.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      8
      • P Offline
        P Offline
        Paul Orasan
        wrote on last edited by
        #3

        I see. But do you have any suggestion about what should I do now?

        I need a user to be able to input a string in a lineEdit and I want to save the converted, hashed string in an online database. I need to obtain strings as I would obtain from this site for example: MD5 Online Hasher

        The algorithm doesn't have to be MD5, I would just want to use one of Qt's in-built features.

        aha_1980A 1 Reply Last reply
        0
        • P Paul Orasan

          I see. But do you have any suggestion about what should I do now?

          I need a user to be able to input a string in a lineEdit and I want to save the converted, hashed string in an online database. I need to obtain strings as I would obtain from this site for example: MD5 Online Hasher

          The algorithm doesn't have to be MD5, I would just want to use one of Qt's in-built features.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Paul-Orasan

          Just like @Christian-Ehrlicher said, with toHex():

          #include <QCryptographicHash>
          #include <QDebug>
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              QString input = "helloworld";
              QByteArray result = QCryptographicHash::hash(input.toUtf8(), QCryptographicHash::Md5);
              QString inputHash = QLatin1String(result.toHex());
              qDebug() << input << " " << input.length();
              qDebug() << inputHash << " " << inputHash.length();
          }
          

          Note that I did two changes to your code:

          1. I use input.toUtf8() to get the same hash always. Otherwise you can get different outputs depending on the users platform or region
          2. The result contains only ASCII after toHex(), so QLatin1String is the fastest conversion to QString

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          5

          • Login

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