mirror of
https://github.com/QuasarApp/Qt-Secret.git
synced 2025-05-10 14:09:35 +00:00
version 0.1
This commit is contained in:
parent
2e5ddada3c
commit
bce497357b
4
.gitignore
vendored
4
.gitignore
vendored
@ -5,7 +5,7 @@
|
||||
*.a
|
||||
*.la
|
||||
*.lai
|
||||
*.so
|
||||
*.so*
|
||||
*.dll
|
||||
*.dylib
|
||||
|
||||
@ -42,3 +42,5 @@ target_wrapper.*
|
||||
|
||||
# QtCreator CMake
|
||||
CMakeLists.txt.user*
|
||||
|
||||
deployTests/
|
||||
|
@ -10,11 +10,12 @@ CONFIG += ordered
|
||||
|
||||
SUBDIRS += \
|
||||
src \
|
||||
tests
|
||||
tests \
|
||||
qaesencryption
|
||||
|
||||
include($$PWD/test.pri)
|
||||
|
||||
src.file = src/Qt-Secret.pro
|
||||
tests.file = tests/Qt-SecretTest.pro
|
||||
|
||||
qaesencryption.file = src/Qt-AES/qaesencryption.pro
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit b296606aabc611df5cda4aece14d0f7df1607a24
|
||||
Subproject commit 1b3ddbd2d22ed826758896cfc67825ae75c4ccc0
|
@ -12,18 +12,14 @@ INT eulerFunc(const INT &p, const INT &q) {
|
||||
}
|
||||
|
||||
template<class INT>
|
||||
INT mul(const INT &a, const INT &b, const INT &m) {
|
||||
if (b == 0) {
|
||||
return 0 % m;
|
||||
INT mul(INT a, INT b, const INT &m) {
|
||||
INT res = 0;
|
||||
while (a != 0) {
|
||||
if (a & 1) res = (res + b) % m;
|
||||
a >>= 1;
|
||||
b = (b << 1) % m;
|
||||
}
|
||||
|
||||
if (b == 1)
|
||||
return a;
|
||||
if (b % 2 == 0){
|
||||
INT t = mul(a, b / 2, m);
|
||||
return (2 * t) % m;
|
||||
}
|
||||
return (mul(a, b - 1, m) + a) % m;
|
||||
return res;
|
||||
}
|
||||
|
||||
template<class INT>
|
||||
@ -171,9 +167,9 @@ QByteArray toArray(INT i, short sizeBlok = -1) {
|
||||
return res;
|
||||
}
|
||||
|
||||
while (res.rbegin() != res.rend() && !*res.rbegin()) {
|
||||
res.remove(res.size() -1, 1);
|
||||
}
|
||||
// while (res.rbegin() != res.rend() && !*res.rbegin()) {
|
||||
// res.remove(res.size() -1, 1);
|
||||
// }
|
||||
|
||||
return res.left(sizeBlok);
|
||||
}
|
||||
@ -288,6 +284,17 @@ QByteArray decodeArray(const QByteArray &rawData, const QByteArray &privKey) {
|
||||
return res.remove(res.lastIndexOf(ENDLINE), res.size());
|
||||
}
|
||||
|
||||
bool QRSAEncryption::testKeyPair(const QByteArray &pubKey, const QByteArray &privKey) {
|
||||
QByteArray tesVal = "Test message of encrypkey";
|
||||
bool result = tesVal == decode(encode(tesVal, pubKey), privKey);
|
||||
|
||||
if (!result) {
|
||||
qWarning() << "(Warning): Testkey Fail, try generate new key pair!";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
QRSAEncryption::QRSAEncryption() {
|
||||
|
||||
}
|
||||
@ -296,11 +303,11 @@ QByteArray QRSAEncryption::encode(const QByteArray &rawData, const QByteArray &p
|
||||
|
||||
switch (pubKey.size()) {
|
||||
case RSA_64 / 4: {
|
||||
return encodeArray<int64_t>(rawData, pubKey);
|
||||
return encodeArray<uint64_t>(rawData, pubKey);
|
||||
}
|
||||
|
||||
case RSA_128 / 4: {
|
||||
return encodeArray<int128_t>(rawData, pubKey);
|
||||
return encodeArray<uint128_t>(rawData, pubKey);
|
||||
}
|
||||
|
||||
default: return QByteArray();
|
||||
@ -312,11 +319,11 @@ QByteArray QRSAEncryption::decode(const QByteArray &rawData, const QByteArray &p
|
||||
|
||||
switch (privKey.size()) {
|
||||
case RSA_64 / 4: {
|
||||
return decodeArray<int64_t>(rawData, privKey);
|
||||
return decodeArray<uint64_t>(rawData, privKey);
|
||||
}
|
||||
|
||||
case RSA_128 / 4: {
|
||||
return decodeArray<int128_t>(rawData, privKey);
|
||||
return decodeArray<uint128_t>(rawData, privKey);
|
||||
}
|
||||
|
||||
default: return QByteArray();
|
||||
@ -346,25 +353,31 @@ bool QRSAEncryption::generatePairKey(QByteArray &pubKey,
|
||||
QByteArray &privKey,
|
||||
QRSAEncryption::Rsa rsa) {
|
||||
|
||||
pubKey.clear();
|
||||
privKey.clear();
|
||||
do {
|
||||
|
||||
switch (rsa) {
|
||||
case RSA_64: {
|
||||
if (!keyGenerator<int64_t>(pubKey, privKey)) {
|
||||
return false;
|
||||
pubKey.clear();
|
||||
privKey.clear();
|
||||
|
||||
switch (rsa) {
|
||||
case RSA_64: {
|
||||
|
||||
if (!keyGenerator<int64_t>(pubKey, privKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RSA_128: {
|
||||
if (!keyGenerator<int128_t>(pubKey, privKey)) {
|
||||
return false;
|
||||
case RSA_128: {
|
||||
if (!keyGenerator<int128_t>(pubKey, privKey)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
} while (!testKeyPair(pubKey, privKey));
|
||||
|
||||
|
||||
return true;
|
||||
|
@ -8,6 +8,8 @@
|
||||
|
||||
class QRSAEncryption
|
||||
{
|
||||
private:
|
||||
bool testKeyPair(const QByteArray &pubKey, const QByteArray &privKey);
|
||||
public:
|
||||
|
||||
enum Rsa {
|
||||
|
@ -24,7 +24,7 @@ CONFIG(release, debug|release): {
|
||||
#include($$PWD/../uint256_t/uint256.pri)
|
||||
|
||||
|
||||
VERSION = 0.0.0.1
|
||||
VERSION = 0.1.0
|
||||
|
||||
HEADERS += \
|
||||
Qt-AES/qaesencryption.h \
|
||||
|
19
test.pri
19
test.pri
@ -1,5 +1,5 @@
|
||||
unix:exec = $$PWD/Stests/build/release/Qt-SecretTest
|
||||
win32:exec = $$PWD/Stests/build/release/Qt-SecretTest.exe
|
||||
unix:exec = $$PWD/tests/build/release/Qt-SecretTest,$$PWD/src/Qt-AES/build/release/QAESEncryption
|
||||
win32:exec = $$PWD/tests/build/release/Qt-SecretTest.exe,$$PWD/src/Qt-AES/build/release/QAESEncryption.exe
|
||||
|
||||
|
||||
contains(QMAKE_HOST.os, Linux):{
|
||||
@ -8,12 +8,21 @@ contains(QMAKE_HOST.os, Linux):{
|
||||
DEPLOYER=%cqtdeployer%
|
||||
}
|
||||
|
||||
|
||||
deployTest.commands = $$DEPLOYER -bin $$exec clear -qmake $$QMAKE_QMAKE -targetDir $$PWD/deployTests -libDir $$PWD -recursiveDepth 5
|
||||
|
||||
test.depends = deployTest
|
||||
unix:test.commands = $$PWD/deployTests/serverTests.sh
|
||||
win32:test.commands = $$PWD/deployTests/serverTests.exe
|
||||
unix:testRSA.commands = $$PWD/deployTests/Qt-SecretTest.sh
|
||||
win32:testRSA.commands = $$PWD/deployTests/Qt-SecretTest.exe
|
||||
|
||||
unix:testAES.commands = $$PWD/deployTests/QAESEncryption.sh
|
||||
win32:testAES.commands = $$PWD/deployTests/QAESEncryption.exe
|
||||
|
||||
test.depends += deployTest
|
||||
test.depends += testRSA
|
||||
test.depends += testAES
|
||||
|
||||
QMAKE_EXTRA_TARGETS += \
|
||||
deployTest \
|
||||
testAES \
|
||||
testRSA \
|
||||
test
|
||||
|
@ -4,11 +4,14 @@
|
||||
#include <qdebug.h>
|
||||
#include <cmath>
|
||||
|
||||
const int testSize = 100;
|
||||
|
||||
|
||||
QByteArray randomArray() {
|
||||
srand(static_cast<unsigned int>(time(nullptr)));
|
||||
QByteArray res;
|
||||
|
||||
int length = rand() % 1024 * 1024;
|
||||
int length = rand() % 124 * 1;
|
||||
|
||||
for (int i = 0; i < length; ++i) {
|
||||
res.push_back(static_cast<char>(rand() % 0xFF));
|
||||
@ -22,9 +25,13 @@ bool testCrypto(QRSAEncryption::Rsa rsa) {
|
||||
QRSAEncryption e;
|
||||
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
for (int i = 0; i < testSize; i++) {
|
||||
e.generatePairKey(pub, priv, rsa);
|
||||
|
||||
qInfo() << QString("Test keys (%0/%1):").arg(i).arg(testSize);
|
||||
qInfo() << QString("Private key: %0").arg(QString(priv.toHex()));
|
||||
qInfo() << QString("Public key: %0").arg(QString(pub.toHex()));
|
||||
|
||||
if (pub.size() != rsa / 4) {
|
||||
qCritical() << "pubKey size wrong RSA" << rsa;
|
||||
return false;
|
||||
@ -36,19 +43,23 @@ bool testCrypto(QRSAEncryption::Rsa rsa) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
for (int i = 0; i < testSize; i++) {
|
||||
auto base = randomArray();
|
||||
|
||||
if ( base != e.decode(e.encode(base, pub), priv)) {
|
||||
qCritical() << "encode decode data error RSA" << rsa;
|
||||
auto encodeData = e.encode(base, pub);
|
||||
auto decodeData = e.decode(encodeData, priv);
|
||||
|
||||
if ( base != decodeData) {
|
||||
qCritical() << "encode/decode data error RSA" << rsa;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!e.checkSignMessage(e.signMessage(base, pub), priv)) {
|
||||
encodeData = e.signMessage(base, priv);
|
||||
|
||||
if (!e.checkSignMessage(encodeData, pub)) {
|
||||
qCritical() << "sig message error RSA" << rsa;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user