From ef03396072e3959e7d03f55d91c2ffa1faa51d8d Mon Sep 17 00:00:00 2001 From: Sune Vuorela Date: Tue, 20 Dec 2011 19:46:28 +0100 Subject: QCryptographicHash: allow to hash the content of a QIODevice This adds a new function (and tests) to give the possibility of doing a QCryptographicHash of a QIODevice, like a QFile or whatever people needs. It is a quite handy overload in many cases. Change-Id: I22fd272f05571844641b3daefcc6746be4e5c7c3 Reviewed-by: Thiago Macieira --- src/corelib/tools/qcryptographichash.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/corelib/tools/qcryptographichash.cpp') diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 89b3bab7c6..e5ac73c3f5 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -50,6 +50,7 @@ #include "../../3rdparty/md4/md4.h" #include "../../3rdparty/md4/md4.cpp" #include "../../3rdparty/sha1/sha1.cpp" +#include QT_BEGIN_NAMESPACE @@ -154,6 +155,28 @@ void QCryptographicHash::addData(const QByteArray &data) addData(data.constData(), data.length()); } +/*! + Reads the data from the open QIODevice \a device until it ends + and hashes it. Returns true if reading was successful. + */ +bool QCryptographicHash::addData(QIODevice* device) +{ + if (!device->isReadable()) + return false; + + if (!device->isOpen()) + return false; + + char buffer[1024]; + int length; + + while ((length = device->read(buffer,sizeof(buffer))) > 0) + addData(buffer,length); + + return device->atEnd(); +} + + /*! Returns the final hash value. -- cgit v1.2.3