OSDN Git Service

klipper: use Katie's hash algorithm for history verification
authorIvailo Monev <xakepa10@gmail.com>
Tue, 22 Mar 2022 17:59:17 +0000 (17:59 +0000)
committerIvailo Monev <xakepa10@gmail.com>
Tue, 22 Mar 2022 17:59:17 +0000 (17:59 +0000)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
klipper/CMakeLists.txt
klipper/klipper.cpp

index 8e9717e..dc4bfbf 100644 (file)
@@ -28,7 +28,7 @@ set(klipper_SRCS ${libklipper_common_SRCS} main.cpp tray.cpp)
 
 add_executable(klipper ${klipper_SRCS})
 
-target_link_libraries(klipper ${KDE4_KDEUI_LIBS} ${X11_LIBRARIES} ${ZLIB_LIBRARY})
+target_link_libraries(klipper ${KDE4_KDEUI_LIBS} ${X11_LIBRARIES} ${QT_QTNETWORK_LIBRARY})
 if (X11_Xfixes_FOUND)
   target_link_libraries(klipper ${X11_Xfixes_LIB})
 endif (X11_Xfixes_FOUND)
index 11664c0..ae2356e 100644 (file)
 
 #include "klipper.h"
 
-#include <zlib.h>
-
 #include <QtGui/QMenu>
 #include <QtDBus/QDBusConnection>
+#include <QtNetwork/QCryptographicHash>
 
 #include <KAboutData>
 #include <KLocale>
 
 //#define NOISY_KLIPPER
 
+#if QT_VERSION >= 0x041200
+static const QCryptographicHash::Algorithm KlipperHashAlhorithm = QCryptographicHash::KAT;
+#else
+static const QCryptographicHash::Algorithm KlipperHashAlhorithm = QCryptographicHash::Sha1;
+#endif
+
 namespace {
     /**
      * Use this when manipulating the clipboard
@@ -368,7 +373,7 @@ bool Klipper::loadHistory() {
     static const char* const failed_load_warning =
         "Failed to load history resource. Clipboard history cannot be read.";
     // don't use "appdata", klipper is also a kicker applet
-    QString history_file_name = KStandardDirs::locateLocal( "data", "klipper/history2.lst" );
+    QString history_file_name = KStandardDirs::locateLocal( "data", "klipper/history3.lst" );
     QFile history_file( history_file_name );
     if ( !history_file.exists() ) {
         kWarning() << failed_load_warning << ": " << "History file does not exist" ;
@@ -384,10 +389,10 @@ bool Klipper::loadHistory() {
         return false;
     }
     QByteArray data;
-    quint32 crc;
-    file_stream >> crc >> data;
-    if( crc32( 0, reinterpret_cast<unsigned char *>( data.data() ), data.size() ) != crc ) {
-        kWarning() << failed_load_warning << ": " << "CRC checksum does not match" ;
+    QByteArray hash;
+    file_stream >> hash >> data;
+    if( QCryptographicHash::hash( data, KlipperHashAlhorithm ).toHex() != hash ) {
+        kWarning() << failed_load_warning << ": " << "Hash does not match" ;
         return false;
     }
     QDataStream history_stream( &data, QIODevice::ReadOnly );
@@ -428,7 +433,7 @@ void Klipper::saveHistory(bool empty) {
     static const char* const failed_save_warning =
         "Failed to save history. Clipboard history cannot be saved.";
     // don't use "appdata", klipper is also a kicker applet
-    QString history_file_name( KStandardDirs::locateLocal( "data", "klipper/history2.lst" ) );
+    QString history_file_name( KStandardDirs::locateLocal( "data", "klipper/history3.lst" ) );
     if ( history_file_name.isNull() || history_file_name.isEmpty() ) {
         kWarning() << failed_save_warning ;
         return;
@@ -452,9 +457,9 @@ void Klipper::saveHistory(bool empty) {
         }
     }
 
-    quint32 crc = crc32( 0, reinterpret_cast<unsigned char *>( data.data() ), data.size() );
+    QByteArray hash = QCryptographicHash::hash( data, KlipperHashAlhorithm ).toHex();
     QDataStream ds ( &history_file );
-    ds << crc << data;
+    ds << hash << data;
 }
 
 // save session on shutdown. Don't simply use the c'tor, as that may not be called.