PDA

View Full Version : Hash a file with QT



manekineko
21st April 2010, 04:52
What's the best way to hash a file with QT?

I'm looking at QCryptographicHash, but the only inputs it takes are either char* or QByteArray.

Someone in another thread claims they managed to get this to calculate an MD5 on a file by passing in a path, but I'm not seeing that behavior:
http://www.qtcentre.org/threads/22665-Are-there-functions-to-make-md5-hash

I could read in a QFile, and convert it to a QByteArray with readAll(), but that takes up as much memory as the size of the file. Is there a more memory efficient way to do this?

Lykurg
21st April 2010, 06:54
What's about qHash()?

borisbn
21st April 2010, 07:07
You can do it in two steps: read a block of file in bytearray, hash it, and append the result to another bytearray. When file reaches the end do hash over second bytearray

manekineko
21st April 2010, 07:35
Lykurg: I'm looking at QHash, and I'm not exactly sure how to go with this one. Is there a good way to hash a file with it?

Borisbn: So is that the same MD5 that would be calculated in one step? So "abcdef" hash is the same as hash(hash("abc") + hash("def"))?

borisbn
21st April 2010, 07:48
You said:

I could read in a QFile, and convert it to a QByteArray with readAll(), but that takes up as much memory as the size of the file. Is there a more memory efficient way to do this?
that's why I suggested to read file by small buffer. However hash( AB ) is NOT equal to hash( A ) + hash( B ). If it matter to you, don't do this way, but if not - IMHO it's quick decision.

Lykurg
21st April 2010, 07:55
Lykurg: I'm looking at QHash, and I'm not exactly sure how to go with this one. Is there a good way to hash a file with it?
I didn't mean QHash. I said qHash (! case sensitive) wich is documented at the bottom of QSourceLocation.