Open the file with QFile
, and call readAll()
to pull it's contents into a QByteArray
. Then use that for the QCryptographicHash::hash(const QByteArray& data, Algorithm method)
call.
In Qt5 you can use addData()
:
// Returns empty QByteArray() on failure.
QByteArray fileChecksum(const QString &fileName,
QCryptographicHash::Algorithm hashAlgorithm)
{
QFile f(fileName);
if (f.open(QFile::ReadOnly)) {
QCryptographicHash hash(hashAlgorithm);
if (hash.addData(&f)) {
return hash.result();
}
}
return QByteArray();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…