Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the SHA-1/MD5 checksum of a file with Qt?

Is there a way to get the MD5 or SHA-1 checksum/hash of a file on disk in Qt?

For example, I have the file path and I might need to verify that the contents of that file matches a certain hash value.

like image 303
user2282405 Avatar asked May 05 '13 10:05

user2282405


People also ask

How do I get an MD5 checksum?

Open a terminal window. Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file.

How do I find the MD5 checksum of a file in Python?

# Import hashlib library (md5 method is part of it) import hashlib # File to check file_name = 'filename.exe' # Correct original md5 goes here original_md5 = '5d41402abc4b2a76b9719d911017c592' # Open,close, read file and calculate MD5 on its contents with open(file_name, 'rb') as file_to_check: # read contents of the ...


1 Answers

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(); } 
like image 58
cmannett85 Avatar answered Sep 24 '22 12:09

cmannett85