File indexing completed on 2025-01-05 04:37:18
0001 /* 0002 SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <QObject> 0008 #include <QtTest> 0009 #include <dht/key.h> 0010 #include <mse/rc4encryptor.h> 0011 #include <time.h> 0012 0013 class RC4EncryptorTest : public QObject 0014 { 0015 Q_OBJECT 0016 public: 0017 bt::SHA1Hash randomKey() 0018 { 0019 bt::Uint32 hash[5]; 0020 for (int i = 0; i < 5; i++) { 0021 hash[i] = QRandomGenerator::global()->generate(); 0022 } 0023 return bt::SHA1Hash(reinterpret_cast<Uint8 *>(hash)); 0024 } 0025 0026 private Q_SLOTS: 0027 void initTestCase() 0028 { 0029 } 0030 0031 void cleanupTestCase() 0032 { 0033 } 0034 0035 void testRC4() 0036 { 0037 bt::SHA1Hash dkey = randomKey(); 0038 bt::SHA1Hash ekey = randomKey(); 0039 mse::RC4Encryptor a(dkey, ekey); 0040 mse::RC4Encryptor b(ekey, dkey); 0041 0042 bt::Uint8 tmp[1024]; 0043 for (int i = 0; i < 1000; i++) { 0044 memset(tmp, 0, 1024); 0045 bt::Uint32 data[256]; 0046 for (int j = 0; j < 256; j++) 0047 data[j] = QRandomGenerator::global()->generate(); 0048 0049 memcpy(tmp, data, 1024); 0050 a.encryptReplace(reinterpret_cast<Uint8 *>(data), 1024); 0051 b.decrypt(reinterpret_cast<Uint8 *>(data), 1024); 0052 QVERIFY(memcmp(tmp, data, 1024) == 0); 0053 } 0054 } 0055 }; 0056 0057 QTEST_MAIN(RC4EncryptorTest) 0058 0059 #include "rc4encryptortest.moc"