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 <thiago.macieira@intel.com>
This commit is contained in:
Sune Vuorela 2011-12-20 19:46:28 +01:00 committed by Qt by Nokia
parent eff09a2794
commit ef03396072
6 changed files with 69 additions and 0 deletions

View File

@ -50,6 +50,7 @@
#include "../../3rdparty/md4/md4.h"
#include "../../3rdparty/md4/md4.cpp"
#include "../../3rdparty/sha1/sha1.cpp"
#include <qiodevice.h>
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.

View File

@ -51,6 +51,7 @@ QT_BEGIN_NAMESPACE
QT_MODULE(Core)
class QCryptographicHashPrivate;
class QIODevice;
class Q_CORE_EXPORT QCryptographicHash
{
@ -68,6 +69,7 @@ public:
void addData(const char *data, int length);
void addData(const QByteArray &data);
bool addData(QIODevice* device);
QByteArray result() const;

View File

@ -2,3 +2,15 @@ CONFIG += testcase parallel_test
TARGET = tst_qcryptographichash
QT = core testlib
SOURCES = tst_qcryptographichash.cpp
wince* {
addFiles.files = data/*
addFiles.path = data/
DEPLOYMENT += addFiles
DEFINES += SRCDIR=\\\".\\\"
}
else {
DEFINES += SRCDIR=\\\"$$PWD/\\\"
}

View File

@ -51,6 +51,8 @@ private slots:
void intermediary_result_data();
void intermediary_result();
void sha1();
void files_data();
void files();
};
#include <QtCore>
@ -150,5 +152,35 @@ void tst_QCryptographicHash::sha1()
}
Q_DECLARE_METATYPE(QCryptographicHash::Algorithm);
void tst_QCryptographicHash::files_data() {
QTest::addColumn<QString>("filename");
QTest::addColumn<QCryptographicHash::Algorithm>("algorithm");
QTest::addColumn<QByteArray>("md5sum");
QTest::newRow("Line") << QString::fromAscii("data/2c1517dad3678f03917f15849b052fd5.md5") << QCryptographicHash::Md5 << QByteArray("2c1517dad3678f03917f15849b052fd5");
QTest::newRow("Line") << QString::fromAscii("data/d41d8cd98f00b204e9800998ecf8427e.md5") << QCryptographicHash::Md5 << QByteArray("d41d8cd98f00b204e9800998ecf8427e");
}
void tst_QCryptographicHash::files()
{
QFETCH(QString, filename);
QFETCH(QCryptographicHash::Algorithm, algorithm);
QFETCH(QByteArray, md5sum);
{
QFile f(QString::fromLocal8Bit(SRCDIR) + filename);
QCryptographicHash hash(algorithm);
QVERIFY(! hash.addData(&f)); // file is not open for reading;
if (f.open(QIODevice::ReadOnly)) {
QVERIFY(hash.addData(&f));
QCOMPARE(hash.result().toHex(),md5sum);
} else {
QFAIL("Failed to open file for testing. should not happen");
}
}
}
QTEST_MAIN(tst_QCryptographicHash)
#include "tst_qcryptographichash.moc"