从 QString 获取 Sha1 哈希

km2*_*442 1 c++ hash qstring qt

在我的 Qt5.6.1 程序中,我必须从 QString 获取 Sha-1 哈希值,但我得到的结果不正确。我正在尝试使用 QCryptographicHash 库。

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

在这种情况下我应该改变什么?

ahm*_*n86 5

我认为这个答案对你有用,它适用于 md5 如何在 Qt 中创建 MD5 哈希?

而不是 str.toStdString().c_str() 尝试使用 str.toUtf8() 像这样形成以前的答案

QString hash = QString("%1").arg(QString(QCryptographicHash::hash(str.toUtf8(),QCryptographicHash::Sha1).toHex()))
Run Code Online (Sandbox Code Playgroud)