c++ - Qt如何让应用程序获取自身的md5校验和

标签 c++ qt md5 checksum

正在开发 Qt 应用程序。我试图让 exe 文件在运行时返回其自身的 md5 校验和。我该怎么做?

我试过这个:

QFile theFile("file.exe");
QByteArray thisFile;
if (theFile.open(QIODevice::ReadOnly))
{
    thisFile = theFile.readAll();
}
else
{
    qDebug() << "Can't open";
}

qDebug() << QString("%1").arg(thisFile.length());

fileMd5 = QString(QCryptographicHash::hash((thisFile), QCryptographicHash::Md5).toHex().toUpper());

qDebug() << fileMd5;

但是,这不会返回正确的值。

更新:

我让它与其他文件一起工作。问题似乎是我无法在运行时读取 exe。

最后更新:

这是解决方案:

QFile theFile(QCoreApplication::applicationFilePath());
QByteArray thisFile;
if (theFile.open(QIODevice::ReadOnly))
{
    thisFile = theFile.readAll();
}
else
{
    qDebug() << "Can't open file.";
}

QString fileMd5 = QString(QCryptographicHash::hash((thisFile), QCryptographicHash::Md5).toHex());

qDebug() << fileMd5;

最佳答案

您忘记在 theFile 上调用 open

if (!theFile.open(QIODevice::ReadOnly))
    // Handle error here

此外,您应该使用 QCoreApplication::applicationFilePath() 获取可执行文件的路径。

关于c++ - Qt如何让应用程序获取自身的md5校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406634/

相关文章:

c++ - 欧拉计划 #6 C++

c++ - Qthread 中的运行函数 - 应用程序将挂起

C++ 小字符转十六进制函数?

hash - MD5 哈希值中只能包含数字或只能包含字母吗?

带有自定义比较器的 C++ std::find

c++ - 何时以及为什么需要在 C++ 中使用 cin.ignore()?

c++ - QList 内部函数模板

hash - 每次C#相同的未更改文件的MD5文件哈希均不同

c++ - 如何正确特化与其父类型相同的模板化静态常量成员

c++ - 将组合框信号连接到 Qt 中的 std::function 的问题