QT计算md5校验值

via: https://www.jianshu.com/p/fe774becf239

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
QString fileMd5(const QString &sourceFilePath) {

QFile sourceFile(sourceFilePath);
qint64 fileSize = sourceFile.size();
const qint64 bufferSize = 10240;

if (sourceFile.open(QIODevice::ReadOnly)) {
char buffer[bufferSize];
int bytesRead;
int readSize = qMin(fileSize, bufferSize);

QCryptographicHash hash(QCryptographicHash::Md5);

while (readSize > 0 && (bytesRead = sourceFile.read(buffer, readSize)) > 0) {
fileSize -= bytesRead;
hash.addData(buffer, bytesRead);
readSize = qMin(fileSize, bufferSize);
}

sourceFile.close();
return QString(hash.result().toHex());
}
return QString();
}