Qt 计算字符串和文件的md5 值

chatgpt结果

string

#include <QCryptographicHash>
#include <QDebug>

QString string = "Hello, World!";
QByteArray data = string.toUtf8();
QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);

qDebug() << "MD5 hash of the string:" << hash.toHex();

file


#include <QCryptographicHash>
#include <QFile>
#include <QDebug>

QString filePath = "path/to/file";
QFile file(filePath);

if (file.open(QFile::ReadOnly)) {
    QCryptographicHash hash(QCryptographicHash::Md5);
    if (hash.addData(&file)) {
        QByteArray result = hash.result();
        qDebug() << "MD5 hash of the file:" << result.toHex();
    }
    else {
        qWarning() << "Failed to calculate MD5 hash of the file.";
    }
    file.close();
}
else {
    qWarning() << "Failed to open the file.";
}

posted @ 2023-03-20 09:00  simp00  阅读(149)  评论(0编辑  收藏  举报