From: Ivailo Monev Date: Fri, 1 Apr 2016 06:06:57 +0000 (+0000) Subject: generic: use QCryptographicHash instead of KMD5 X-Git-Tag: 4.22.0~893 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=834866b93f897bf96f8a2acadac97d27c936af9c;p=kde%2Fkde-extraapps.git generic: use QCryptographicHash instead of KMD5 Signed-off-by: Ivailo Monev --- diff --git a/gwenview/lib/thumbnailprovider/thumbnailprovider.cpp b/gwenview/lib/thumbnailprovider/thumbnailprovider.cpp index 974bb1c7..baa994ce 100644 --- a/gwenview/lib/thumbnailprovider/thumbnailprovider.cpp +++ b/gwenview/lib/thumbnailprovider/thumbnailprovider.cpp @@ -32,10 +32,10 @@ #include #include #include +#include // KDE #include -#include #include #include #include @@ -74,9 +74,9 @@ static QString generateOriginalUri(const KUrl& url_) static QString generateThumbnailPath(const QString& uri, ThumbnailGroup::Enum group) { - KMD5 md5(QFile::encodeName(uri)); + QByteArray md5 = QCryptographicHash::hash(QFile::encodeName(uri), QCryptographicHash::Md5); QString baseDir = ThumbnailProvider::thumbnailBaseDir(group); - return baseDir + QString(QFile::encodeName(md5.hexDigest())) + ".png"; + return baseDir + QString(QFile::encodeName(md5.toHex())) + ".png"; } //------------------------------------------------------------------------ diff --git a/kget/CMakeLists.txt b/kget/CMakeLists.txt index 6e32be8a..e1385ed7 100644 --- a/kget/CMakeLists.txt +++ b/kget/CMakeLists.txt @@ -16,9 +16,6 @@ include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDE4_INCLUDES}) macro_optional_find_package(Sqlite) macro_log_feature(SQLITE_FOUND "SQLite" "SQLite is a Binary-Database" "" FALSE "" "Needed for the SQLite-Backend of the KGet-History and the Kopete-Statistic-Plugin") -macro_optional_find_package(QCA2) -macro_log_feature(QCA2_FOUND "QCA2" "Qt Cryptographic Architecture" "http://delta.affinix.com/qca" FALSE "2.0.0" "Needed for the KGet bittorrent-plugin and some Kopete plugins") - macro_optional_find_package(QGpgme) macro_log_feature(QGPGME_FOUND "QGpgME" "The QGpgME library" "http://www.kde.org" FALSE "" "QGpgME is required to have signature verifying support in KGet.") @@ -39,11 +36,6 @@ if(CMAKE_BUILD_TYPE MATCHES debugfull) add_definitions(-DDEBUG) endif(CMAKE_BUILD_TYPE MATCHES debugfull) -if(QCA2_FOUND) - add_definitions(-DHAVE_QCA2) - include_directories(${QCA2_INCLUDE_DIR}) -endif(QCA2_FOUND) - if(QGPGME_FOUND) add_definitions(-DHAVE_QGPGME) include_directories(${QGPGME_INCLUDE_DIR}) @@ -141,10 +133,6 @@ if (SQLITE_FOUND) target_link_libraries(kgetcore ${QT_QTSQL_LIBRARY}) endif (SQLITE_FOUND) -if (QCA2_FOUND) - target_link_libraries(kgetcore ${QCA2_LIBRARIES}) -endif (QCA2_FOUND) - if (QGPGME_FOUND) target_link_libraries(kgetcore ${QGPGME_LIBRARIES}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}") @@ -233,9 +221,6 @@ qt4_add_dbus_adaptor(kget_SRCS dbus/org.kde.kget.main.xml dbus/dbuskgetwrapper.h add_executable(kget ${kget_SRCS}) target_link_libraries(kget ${QT_QTTEST_LIBRARY} ${KDE4_KDEUI_LIBRARY} ${KDE4_KIO_LIBRARY} ${KDE4_KCMUTILS_LIBRARY} kgetcore ${KDE4_KNOTIFYCONFIG_LIBS}) -if(QCA2_FOUND) - target_link_libraries(kget ${QCA2_LIBRARIES}) -endif(QCA2_FOUND) if (QGPGME_FOUND) target_link_libraries(kget ${QGPGME_LIBRARIES}) diff --git a/kget/core/verificationthread.cpp b/kget/core/verificationthread.cpp index 0f5b9e7b..ef2ab017 100644 --- a/kget/core/verificationthread.cpp +++ b/kget/core/verificationthread.cpp @@ -111,21 +111,13 @@ void VerificationThread::doVerify() continue; } - const QString hash = Verifier::checksum(url, type, &m_abort); + const QString hash = Verifier::checksum(url, type); kDebug(5001) << "Type:" << type << "Calculated checksum:" << hash << "Entered checksum:" << checksum; const bool fileVerified = (hash == checksum); - if (m_abort) - { - return; - } - m_mutex.lock(); - if (!m_abort) - { - emit verified(type, fileVerified, url); - emit verified(fileVerified); - } + emit verified(type, fileVerified, url); + emit verified(fileVerified); run = m_files.count(); m_mutex.unlock(); } @@ -159,13 +151,7 @@ void VerificationThread::doBrokenPieces() return; } - const QStringList fileChecksums = Verifier::partialChecksums(url, type, length, &m_abort).checksums(); - if (m_abort) - { - emit brokenPieces(broken, length); - return; - } - + const QStringList fileChecksums = Verifier::partialChecksums(url, type, length).checksums(); if (fileChecksums.size() != checksums.size()) { kDebug(5001) << "Number of checksums differs!"; diff --git a/kget/core/verifier.cpp b/kget/core/verifier.cpp index 31378e73..87fd0600 100644 --- a/kget/core/verifier.cpp +++ b/kget/core/verifier.cpp @@ -25,20 +25,19 @@ #include #include +#include #include -#include #include -#ifdef HAVE_QCA2 -#include -#endif - //TODO use mutable to make some methods const? -const QStringList VerifierPrivate::SUPPORTED = (QStringList() << "sha512" << "sha384" << "sha256" << "ripmed160" << "sha1" << "md5" << "md4"); +// TODO: implement md4 support +const QStringList VerifierPrivate::SUPPORTED = (QStringList() << "sha1" << "md5" << "md4"); const QString VerifierPrivate::MD5 = QString("md5"); -const int VerifierPrivate::DIGGESTLENGTH[] = {128, 96, 64, 40, 40, 32, 32}; const int VerifierPrivate::MD5LENGTH = 32; +const QString VerifierPrivate::SHA1 = QString("sha1"); +const int VerifierPrivate::SHA1LENGTH = 40; +const int VerifierPrivate::DIGGESTLENGTH[] = {40, 32, 32}; const int VerifierPrivate::PARTSIZE = 500 * 1024; VerifierPrivate::~VerifierPrivate() @@ -47,7 +46,7 @@ VerifierPrivate::~VerifierPrivate() qDeleteAll(partialSums.begin(), partialSums.end()); } -QString VerifierPrivate::calculatePartialChecksum(QFile *file, const QString &type, KIO::fileoffset_t startOffset, int pieceLength, KIO::filesize_t fileSize, bool *abortPtr) +QString VerifierPrivate::calculatePartialChecksum(QFile *file, const QString &type, KIO::fileoffset_t startOffset, int pieceLength, KIO::filesize_t fileSize) { if (!file) { @@ -64,19 +63,9 @@ QString VerifierPrivate::calculatePartialChecksum(QFile *file, const QString &ty pieceLength = fileSize - startOffset; } -#ifdef HAVE_QCA2 - QCA::Hash hash(type); - - //it can be that QCA2 does not support md5, e.g. when Qt is compiled locally - KMD5 md5Hash; - const bool useMd5 = (type == MD5); -#else //NO QCA2 - if (type != MD5) - { + if (type != MD5 && type != SHA1) { return QString(); } - KMD5 hash; -#endif //HAVE_QCA2 //we only read 512kb each time, to save RAM int numData = pieceLength / PARTSIZE; @@ -84,9 +73,15 @@ QString VerifierPrivate::calculatePartialChecksum(QFile *file, const QString &ty if (!numData && !dataRest) { - QString(); + return QString(); } + QCryptographicHash *hash = 0; + if (type == MD5) { + hash = new QCryptographicHash(QCryptographicHash::Md5); + } else if (type == SHA1) { + hash = new QCryptographicHash(QCryptographicHash::Sha1); + } int k = 0; for (k = 0; k < numData; ++k) { @@ -95,21 +90,8 @@ QString VerifierPrivate::calculatePartialChecksum(QFile *file, const QString &ty return QString(); } - if (abortPtr && *abortPtr) - { - return QString(); - } - QByteArray data = file->read(PARTSIZE); -#ifdef HAVE_QCA2 - if (useMd5) { - md5Hash.update(data); - } else { - hash.update(data); - } -#else //NO QCA2 - hash.update(data); -#endif //HAVE_QCA2 + hash->addData(data); } //now read the rest @@ -121,22 +103,10 @@ QString VerifierPrivate::calculatePartialChecksum(QFile *file, const QString &ty } QByteArray data = file->read(dataRest); -#ifdef HAVE_QCA2 - if (useMd5) { - md5Hash.update(data); - } else { - hash.update(data); - } -#else //NO QCA2 - hash.update(data); -#endif //HAVE_QCA2 + hash->addData(data); } -#ifdef HAVE_QCA2 - return (useMd5 ? QString(md5Hash.hexDigest()) : QString(QCA::arrayToHex(hash.final().toByteArray()))); -#else //NO QCA2 - return QString(hash.hexDigest()); -#endif //HAVE_QCA2 + return QString(hash->result().toHex()); } QStringList VerifierPrivate::orderChecksumTypes(Verifier::ChecksumStrength strength) const @@ -216,20 +186,9 @@ VerificationModel *Verifier::model() QStringList Verifier::supportedVerficationTypes() { QStringList supported; -#ifdef HAVE_QCA2 - QStringList supportedTypes = QCA::Hash::supportedTypes(); - for (int i = 0; i < VerifierPrivate::SUPPORTED.count(); ++i) - { - if (supportedTypes.contains(VerifierPrivate::SUPPORTED.at(i))) - { - supported << VerifierPrivate::SUPPORTED.at(i); - } - } -#endif //HAVE_QCA2 - if (!supported.contains(VerifierPrivate::MD5)) { - supported << VerifierPrivate::MD5; + supported << VerifierPrivate::MD5 << VerifierPrivate::SHA1; } return supported; @@ -238,18 +197,12 @@ QStringList Verifier::supportedVerficationTypes() int Verifier::diggestLength(const QString &type) { - if (type == VerifierPrivate::MD5) - { + if (type == VerifierPrivate::MD5) { return VerifierPrivate::MD5LENGTH; + } else if (type == VerifierPrivate::SHA1) { + return VerifierPrivate::SHA1LENGTH; } -#ifdef HAVE_QCA2 - if (QCA::isSupported(type.toLatin1())) - { - return VerifierPrivate::DIGGESTLENGTH[VerifierPrivate::SUPPORTED.indexOf(type)]; - } -#endif //HAVE_QCA2 - return 0; } @@ -395,7 +348,7 @@ void Verifier::brokenPieces() const d->thread.findBrokenPieces(pair.first, checksums, length, d->dest); } -QString Verifier::checksum(const KUrl &dest, const QString &type, bool *abortPtr) +QString Verifier::checksum(const KUrl &dest, const QString &type) { QStringList supported = supportedVerficationTypes(); if (!supported.contains(type)) @@ -410,42 +363,19 @@ QString Verifier::checksum(const KUrl &dest, const QString &type, bool *abortPtr } if (type == VerifierPrivate::MD5) { - KMD5 hash; - hash.update(file); - QString final = QString(hash.hexDigest()); + QByteArray hash = QCryptographicHash::hash(file.readAll(), QCryptographicHash::Md5); file.close(); - return final; - } - - -#ifdef HAVE_QCA2 - QCA::Hash hash(type); - - //BEGIN taken from qca_basic.h and slightly adopted to allow abort - char buffer[1024]; - int len; - - while ((len=file.read(reinterpret_cast(buffer), sizeof(buffer))) > 0) - { - hash.update(buffer, len); - if (abortPtr && *abortPtr) - { - hash.final(); - file.close(); - return QString(); - } + return hash.toHex(); + } else if (type == VerifierPrivate::SHA1) { + QByteArray hash = QCryptographicHash::hash(file.readAll(), QCryptographicHash::Sha1); + file.close(); + return hash.toHex(); } - //END - - QString final = QString(QCA::arrayToHex(hash.final().toByteArray())); - file.close(); - return final; -#endif //HAVE_QCA2 return QString(); } -PartialChecksums Verifier::partialChecksums(const KUrl &dest, const QString &type, KIO::filesize_t length, bool *abortPtr) +PartialChecksums Verifier::partialChecksums(const KUrl &dest, const QString &type, KIO::filesize_t length) { QStringList checksums; @@ -496,7 +426,7 @@ PartialChecksums Verifier::partialChecksums(const KUrl &dest, const QString &typ //create all the checksums for the pieces for (int i = 0; i < numPieces; ++i) { - QString hash = VerifierPrivate::calculatePartialChecksum(&file, type, length * i, length, fileSize, abortPtr); + QString hash = VerifierPrivate::calculatePartialChecksum(&file, type, length * i, length, fileSize); if (hash.isEmpty()) { file.close(); diff --git a/kget/core/verifier.h b/kget/core/verifier.h index 7ac65e04..cec69e20 100644 --- a/kget/core/verifier.h +++ b/kget/core/verifier.h @@ -126,17 +126,15 @@ class KGET_EXPORT Verifier : public QObject /** * Creates the checksum type of the file dest - * @param abortPtr makes it possible to abort the calculation of the checksum from another thread */ - static QString checksum(const KUrl &dest, const QString &type, bool *abortPtr); + static QString checksum(const KUrl &dest, const QString &type); /** * Create partial checksums of type for file dest - * @param abortPtr makes it possible to abort the calculation of the checksums from another thread * @note the length of the partial checksum (if not defined = 0) is not less than 512 kb * and there won't be more partial checksums than 101 */ - static PartialChecksums partialChecksums(const KUrl &dest, const QString &type, KIO::filesize_t length = 0, bool *abortPtr = 0); + static PartialChecksums partialChecksums(const KUrl &dest, const QString &type, KIO::filesize_t length = 0); /** * @note only call verify() when this function returns true diff --git a/kget/core/verifier_p.h b/kget/core/verifier_p.h index b86c536f..e73e621e 100644 --- a/kget/core/verifier_p.h +++ b/kget/core/verifier_p.h @@ -36,7 +36,7 @@ struct VerifierPrivate ~VerifierPrivate(); - static QString calculatePartialChecksum(QFile *file, const QString &type, KIO::fileoffset_t startOffset, int pieceLength, KIO::filesize_t fileSize = 0, bool *abortPtr = 0); + static QString calculatePartialChecksum(QFile *file, const QString &type, KIO::fileoffset_t startOffset, int pieceLength, KIO::filesize_t fileSize = 0); QStringList orderChecksumTypes(Verifier::ChecksumStrength strength) const; @@ -53,8 +53,10 @@ struct VerifierPrivate static const QStringList SUPPORTED; static const QString MD5; - static const int DIGGESTLENGTH[]; static const int MD5LENGTH; + static const QString SHA1; + static const int SHA1LENGTH; + static const int DIGGESTLENGTH[]; static const int PARTSIZE; }; diff --git a/kget/mainwindow.h b/kget/mainwindow.h index 31f72940..5e8868be 100644 --- a/kget/mainwindow.h +++ b/kget/mainwindow.h @@ -23,10 +23,6 @@ #include "core/transfer.h" #include "core/transfergroup.h" -#ifdef HAVE_QCA2 -#include -#endif - class ViewsContainer; class DropTarget; class DBusKgetWrapper; @@ -156,10 +152,6 @@ private: bool m_doTesting; // UnitTest flag HttpServer *m_webinterface; - -#ifdef HAVE_QCA2 - QCA::Initializer m_qcaInit; -#endif //HAVE_QCA2 }; #endif diff --git a/kget/tests/CMakeLists.txt b/kget/tests/CMakeLists.txt index c5ca0385..e5488953 100644 --- a/kget/tests/CMakeLists.txt +++ b/kget/tests/CMakeLists.txt @@ -18,11 +18,9 @@ target_link_libraries(kget-test_transfers ${QT_QTTEST_LIBRARY} ${KDE4_KIO_LIBS} #===========Verifier=========== -if (QCA2_FOUND) - kde4_add_test(kget-verifiertest verifiertest.cpp) +kde4_add_test(kget-verifiertest verifiertest.cpp) - target_link_libraries(kget-verifiertest ${QT_QTTEST_LIBRARY} ${KDE4_KDECORE_LIBS} ${QCA2_LIBRARIES} kgetcore) -endif (QCA2_FOUND) +target_link_libraries(kget-verifiertest ${QT_QTTEST_LIBRARY} ${KDE4_KDECORE_LIBS} ${QCA2_LIBRARIES} kgetcore) #===========Scheduler=========== diff --git a/kget/tests/verifiertest.cpp b/kget/tests/verifiertest.cpp index ce052e3b..30efd71e 100644 --- a/kget/tests/verifiertest.cpp +++ b/kget/tests/verifiertest.cpp @@ -57,7 +57,7 @@ void VerfierTest::testChecksum() return; } - QCOMPARE(Verifier::checksum(m_file, type, 0), checksum); + QCOMPARE(Verifier::checksum(m_file, type), checksum); } void VerfierTest::testChecksum_data() @@ -79,7 +79,7 @@ void VerfierTest::testPartialChecksums() QFETCH(QStringList, checksums); QFETCH(bool, result); - const PartialChecksums partial = Verifier::partialChecksums(m_file, type, length, 0); + const PartialChecksums partial = Verifier::partialChecksums(m_file, type, length); QCOMPARE((partial.checksums() == checksums), result); } diff --git a/kget/tests/verifiertest.h b/kget/tests/verifiertest.h index ac313def..2b459992 100644 --- a/kget/tests/verifiertest.h +++ b/kget/tests/verifiertest.h @@ -23,10 +23,6 @@ #include #include -#ifdef HAVE_QCA2 -#include -#endif - class KTempDir; class VerfierTest : public QObject @@ -59,9 +55,6 @@ class VerfierTest : public QObject bool expectedResult(bool expected, const QString &type); private: -#ifdef HAVE_QCA2 - QCA::Initializer m_qcaInit; -#endif //HAVE_QCA2 QScopedPointer m_tempDir; KUrl m_file; const QStringList m_supported; diff --git a/kget/ui/metalinkcreator/filehandler.cpp b/kget/ui/metalinkcreator/filehandler.cpp index 61d125ff..b4baad39 100644 --- a/kget/ui/metalinkcreator/filehandler.cpp +++ b/kget/ui/metalinkcreator/filehandler.cpp @@ -92,7 +92,7 @@ void FileHandlerThread::run() return; } - const QString hash = Verifier::checksum(url, type, &abort); + const QString hash = Verifier::checksum(url, type); if (!hash.isEmpty()) { file.verification.hashes[type] = hash; } @@ -104,7 +104,7 @@ void FileHandlerThread::run() return; } - const PartialChecksums partialChecksums = Verifier::partialChecksums(url, type, 0, &abort); + const PartialChecksums partialChecksums = Verifier::partialChecksums(url, type, 0); if (partialChecksums.isValid()) { KGetMetalink::Pieces pieces; pieces.type = type; diff --git a/kgpg/editor/kgpgmd5widget.cpp b/kgpg/editor/kgpgmd5widget.cpp index 4ce1ed90..d3955cb9 100644 --- a/kgpg/editor/kgpgmd5widget.cpp +++ b/kgpg/editor/kgpgmd5widget.cpp @@ -13,12 +13,12 @@ #include #include #include +#include #include #include #include #include -#include #include @@ -31,14 +31,14 @@ Md5Widget::Md5Widget(QWidget *parent, const KUrl &url) setButtonText(Apply, i18n("Compare MD5 with Clipboard")); QFile f(url.path()); - KMD5 checkfile; + QCryptographicHash checkfile(QCryptographicHash::Md5); if (f.open(QIODevice::ReadOnly)) { - checkfile.update(f); - f.close(); + checkfile.addData(f.readAll()); + f.close(); } - m_md5sum = QLatin1String( checkfile.hexDigest().constData() ); + m_md5sum = QLatin1String( checkfile.result().toHex() ); QWidget *page = new QWidget(this); diff --git a/syndication/tools.cpp b/syndication/tools.cpp index 84deea2c..eae7bbfb 100644 --- a/syndication/tools.cpp +++ b/syndication/tools.cpp @@ -24,20 +24,18 @@ #include "personimpl.h" #include -#include #include #include #include #include #include +#include #include namespace Syndication { -KMD5 md5Machine; - unsigned int calcHash(const QString& str) { return calcHash(str.toUtf8()); @@ -114,9 +112,7 @@ QString dateTimeToString(time_t date) QString calcMD5Sum(const QString& str) { - md5Machine.reset(); - md5Machine.update(str.toUtf8()); - return QLatin1String(md5Machine.hexDigest().constData()); + return QLatin1String(QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Md5).toHex()); } QString resolveEntities(const QString& str)