c++ - 从 QString 获取 Sha1 哈希

标签 c++ qt hash qstring

在我的 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"

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

最佳答案

我认为这个答案对您有用,它适用于 md5 How to create MD5 hash in Qt?

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

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

关于c++ - 从 QString 获取 Sha1 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37763396/

相关文章:

c# - 如果C++中的DLL不支持二进制封装,那么什么时候才可以使用它们呢?

c++ - 以编程方式在 Windows 上隐藏应用程序

c++ - 获取 "embedded"变量或对象的 getter 是否被允许/普遍使用?

mysql - 如何计算 MySQL 查询中两个哈希之间的差异?

java - 如何将随机整数插入哈希表?

c++ - 预先知道的字符串的完美哈希函数

android - ConvexHull Android ndk 和 Opencv 中的参数无效

c++ - C++0x 中的 disable_if 在哪里?

c++ - Cairo 和 Qt 集成

c++ - 构造函数会影响性能吗?