OSDN Git Service

generic: use QCryptographicHash instead of KMD5
authorIvailo Monev <xakepa10@gmail.com>
Fri, 1 Apr 2016 06:06:57 +0000 (06:06 +0000)
committerIvailo Monev <xakepa10@gmail.com>
Fri, 1 Apr 2016 06:06:57 +0000 (06:06 +0000)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
13 files changed:
gwenview/lib/thumbnailprovider/thumbnailprovider.cpp
kget/CMakeLists.txt
kget/core/verificationthread.cpp
kget/core/verifier.cpp
kget/core/verifier.h
kget/core/verifier_p.h
kget/mainwindow.h
kget/tests/CMakeLists.txt
kget/tests/verifiertest.cpp
kget/tests/verifiertest.h
kget/ui/metalinkcreator/filehandler.cpp
kgpg/editor/kgpgmd5widget.cpp
syndication/tools.cpp

index 974bb1c..baa994c 100644 (file)
 #include <QFile>
 #include <QImage>
 #include <QPixmap>
+#include <QCryptographicHash>
 
 // KDE
 #include <KApplication>
-#include <KCodecs>
 #include <KDebug>
 #include <kde_file.h>
 #include <KFileItem>
@@ -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";
 }
 
 //------------------------------------------------------------------------
index 6e32be8..e1385ed 100644 (file)
@@ -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})
index 0f5b9e7..ef2ab01 100644 (file)
@@ -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!";
index 31378e7..87fd060 100644 (file)
 
 #include <QtCore/QFile>
 #include <QtCore/QScopedPointer>
+#include <QtCore/QCryptographicHash>
 #include <QtXml/qdom.h>
 
-#include <KCodecs>
 #include <KDebug>
 
-#ifdef HAVE_QCA2
-#include <QtCrypto>
-#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<char*>(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();
index 7ac65e0..cec69e2 100644 (file)
@@ -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
index b86c536..e73e621 100644 (file)
@@ -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;
 };
 
index 31f7294..5e8868b 100644 (file)
 #include "core/transfer.h"
 #include "core/transfergroup.h"
 
-#ifdef HAVE_QCA2
-#include <QtCrypto>
-#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
index c5ca038..e548895 100644 (file)
@@ -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===========
index ce052e3..30efd71 100644 (file)
@@ -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);
 }
 
index ac313de..2b45999 100644 (file)
 #include <QStringList>
 #include <KUrl>
 
-#ifdef HAVE_QCA2
-#include <QtCrypto>
-#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<KTempDir> m_tempDir;
         KUrl m_file;
         const QStringList m_supported;
index 61d125f..b4baad3 100644 (file)
@@ -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;
index 4ce1ed9..d3955cb 100644 (file)
 #include <QClipboard>
 #include <QLabel>
 #include <QFile>
+#include <QCryptographicHash>
 
 #include <KApplication>
 #include <KMessageBox>
 #include <KLineEdit>
 #include <KLocale>
-#include <KCodecs>
 #include <KLed>
 
 
@@ -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);
 
index 84deea2..eae7bbf 100644 (file)
 #include "personimpl.h"
 
 #include <kcharsets.h>
-#include <kcodecs.h>
 #include <kdatetime.h>
 
 #include <QtCore/QByteArray>
 #include <QtCore/QDateTime>
 #include <QtCore/QRegExp>
 #include <QtCore/QString>
+#include <QtCore/QCryptographicHash>
 
 #include <kdebug.h>
 
 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)