Generate SHA-1 Hash using C++

Qt Core module

  1. Add linking against the module library:

If building project with CMake then use the find_package() command to locate the needed module:

find_package(Qt5 COMPONENTS Core REQUIRED)
target_link_libraries(myapp Qt5::Core)
  1. Generate SHA-1 hash:
#include <iostream>
#include <QCoreApplication>
#include <QCryptographicHash>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QString text = "Hello";
    QByteArray bytes = QCryptographicHash::hash(text.toUtf8(), QCryptographicHash::Sha1);
    QString digest = QString(bytes.toHex());

    std::cout << digest.toStdString() << std::endl;

    return app.exec();
}

Leave a Comment

Cancel reply

Your email address will not be published.