removed Qt-Secret and added qt-openssl-encryption to replace it

This commit is contained in:
Dorian Zedler 2020-07-18 18:48:00 +02:00
parent a46e916999
commit 8aa41a3456
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
17 changed files with 4 additions and 6403 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "qt-openssl-encryption"]
path = qt-openssl-encryption
url = ssh://git@git.itsblue.de:2222/itsblue-development/qt-openssl-encryption.git

View file

@ -1 +0,0 @@
/build/

View file

@ -1,24 +0,0 @@
#
# Copyright (C) 2018-2019 QuasarApp.
# Distributed under the lgplv3 software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
!isEmpty(Qt_GMP_LIB):error("GMP.pri already included")
Qt_GMP_LIB = 1
DEFINES += MINIGMP_LIBRARY
DEFINES += QT_DEPRECATED_WARNINGS
HEADERS += \
$$PWD/bigint.h \
$$PWD/mini-gmp.h \
$$PWD/minigmp_global.h
SOURCES += \
$$PWD/bigint.cpp \
$$PWD/mini-gmp.c
INCLUDEPATH += $$PWD

View file

@ -1,38 +0,0 @@
#
# Copyright (C) 2018-2019 QuasarApp.
# Distributed under the lgplv3 software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
QT -= core gui
TARGET = MiniGMP
TEMPLATE = lib
DEFINES += MINIGMP_LIBRARY
DEFINES += QT_DEPRECATED_WARNINGS
TARGET = QtBigInt
CONFIG += static
VERSION = 6.1.2
CONFIG(release, debug|release): {
DESTDIR="$$PWD/build/release"
} else {
DESTDIR="$$PWD/build/debug"
}
DISTFILES += \
README \
HEADERS += \
bigint.h \
mini-gmp.h \
minigmp_global.h
SOURCES += \
bigint.cpp \
mini-gmp.c

View file

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2019 QuasarApp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,618 +0,0 @@
//#
//# Copyright (C) 2018-2019 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "bigint.h"
#include <limits>
#include <cstring>
#include <cmath>
// constructors
BigInt::BigInt() {
mpz_init(data);
}
BigInt::BigInt(const BigInt &val, int bitCount) {
if (bitCount > 0) {
mpz_init2(data, static_cast<unsigned int>(bitCount));
} else {
mpz_init(data);
}
mpz_set(data, val.data);
}
BigInt::BigInt(const std::string &str, int base):
BigInt() {
mpz_set_str(data, str.c_str(), base);
}
BigInt::BigInt(intMpz val):
BigInt() {
mpz_set_si(data, val);
}
BigInt::BigInt(char item, unsigned int size, int base = 2):
BigInt(std::string(size, item),base) {
}
std::string BigInt::getString(int base) const {
char *str = mpz_get_str(nullptr, base, data);
return str;
}
BigInt::~BigInt() {
mpz_clear(data);
}
BigInt &BigInt::powm(const BigInt &pow, const BigInt &mod) {
mpz_powm(data, data, pow.data, mod.data);
return *this;
}
BigInt BigInt::powm(BigInt val, const BigInt &pow, const BigInt &mod) {
return val.powm(pow, mod);
}
BigInt &BigInt::pow(uIntMpz pow) {
mpz_pow_ui(data, data, pow);
return *this;
}
int BigInt::sizeBits() const {
return sizeBytes() * 8;
}
int BigInt::sizeBytes() const {
return static_cast<int>(mpz_size(data) * sizeof ((*data->_mp_d)));
}
int BigInt::longBits() const {
return static_cast<int>(getString(2).size());
}
int BigInt::longBytes() const {
return static_cast<int>(std::ceil(static_cast<double>(longBits()) / 8));
}
#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
int BigInt::sizeType() const {
return static_cast<int>(static_cast<size_t>(GMP_ABS( data->_mp_alloc)) *
sizeof ((*data->_mp_d)));
}
bool BigInt::isPrime(bool absalut) const {
return (mpz_probab_prime_p(data, 50) - (absalut? 1: 0)) > 0;
}
BigInt& BigInt::gcd(const BigInt &a, const BigInt &b) {
mpz_gcd(data, a.data, b.data);
return *this;
}
void BigInt::fromHex(const std::string &hex) {
mpz_set_str(data, hex.c_str(), 16);
}
BigInt BigInt::bigPow10(unsigned short pow) {
return "1" + std::string(pow, '0');
}
BigInt &BigInt::toNegative() {
mpz_neg(data, data);
return *this;
}
BigInt &BigInt::operator =(const BigInt &val) {
mpz_set(data, val.data);
return *this;
}
BigInt &BigInt::operator =(const std::string &imput) {
mpz_set_str(data, imput.c_str(), 10);
return *this;
}
BigInt &BigInt::operator =(intMpz val) {
mpz_set_si(data, val);
return *this;
}
// add operators
BigInt operator +(BigInt left, intMpz right) {
if (right >= 0) {
mpz_add_ui(left.data, left.data, static_cast<uIntMpz>(right));
return left;
}
return left -= std::abs(right);
}
BigInt operator +(intMpz left, BigInt right) {
return right += left;
}
BigInt operator +(BigInt left, const BigInt &right) {
mpz_add(left.data, left.data, right.data);
return left;
}
BigInt operator +(BigInt left, const std::string &right) {
return left += BigInt(right);
}
BigInt operator +(const std::string &left, const BigInt &right) {
return BigInt(left) + right;
}
BigInt& operator +=(BigInt &left, intMpz right) {
if (right >= 0) {
mpz_add_ui(left.data, left.data, static_cast<uIntMpz>(right));
return left;
}
return left -= std::abs(right);
}
BigInt& operator +=(BigInt &left, const BigInt &right) {
mpz_add(left.data, left.data, right.data);
return left;
}
BigInt& operator +=(BigInt &left, const std::string &right) {
return left += BigInt(right);
}
// sub operators
BigInt operator -(BigInt left, const BigInt &right) {
mpz_sub(left.data, left.data, right.data);
return left;
}
BigInt operator -(BigInt left, intMpz right) {
if (right >= 0) {
mpz_sub_ui(left.data, left.data, static_cast<uIntMpz>(right));
return left;
}
return left += std::abs(right);
}
BigInt operator -(intMpz left, BigInt right) {
if (left >= 0) {
mpz_ui_sub(right.data, static_cast<uIntMpz>(left), right.data);
return right;
}
return right += std::abs(left);
}
BigInt operator-(BigInt val) {
mpz_neg(val.data, val.data);
return val;
}
BigInt operator -(BigInt left, const std::string &right) {
return left -= BigInt(right);
}
BigInt operator -(const std::string & left, const BigInt &right) {
return BigInt(left) - right;
}
BigInt& operator -=(BigInt &left, const BigInt &right) {
mpz_sub(left.data, left.data, right.data);
return left;
}
BigInt& operator -=(BigInt &left, const std::string &right) {
return left -= BigInt(right);
}
BigInt& operator -=(BigInt &left, intMpz right) {
if (right >= 0) {
mpz_sub_ui(left.data, left.data, static_cast<uIntMpz>(right));
return left;
}
return left += std::abs(right);
}
// div operators
BigInt operator /(BigInt left, const BigInt &right) {
mpz_tdiv_q(left.data, left.data, right.data);
return left;
}
BigInt operator /(BigInt left, intMpz right) {
mpz_tdiv_q_ui(left.data, left.data, static_cast<uIntMpz>(std::abs(right)));
if (right >= 0) {
return left;
}
return -left;
}
BigInt operator /(BigInt left, const std::string &right) {
return left /= BigInt(right);
}
BigInt operator /(intMpz left, BigInt right) {
return BigInt(left) / right;
}
BigInt operator /(const std::string & left, const BigInt &right) {
return BigInt(left) / right;
}
BigInt& operator /=(BigInt &left, const BigInt &right) {
mpz_tdiv_q(left.data, left.data, right.data);
return left;
}
BigInt& operator /=(BigInt &left, const std::string &right) {
return left /= BigInt(right);
}
BigInt& operator /=(BigInt &left, intMpz right) {
mpz_tdiv_q_ui(left.data, left.data, static_cast<uIntMpz>(std::abs(right)));
if (right >= 0) {
return left;
}
return left.toNegative();
}
// mul operators
BigInt operator *(BigInt left, const BigInt &right) {
mpz_mul(left.data, left.data, right.data);
return left;
}
BigInt operator *(BigInt left, intMpz right) {
mpz_mul_ui(left.data, left.data, static_cast<uIntMpz>(std::abs(right)));
if (right >= 0) {
return left;
}
return -left;
}
BigInt operator *(intMpz left, BigInt right) {
return right *= left;
}
BigInt operator *(const std::string & left, BigInt right) {
return right *= BigInt(left);
}
BigInt operator *(BigInt left, const std::string &right) {
return left *= BigInt(right);
}
BigInt& operator *=(BigInt &left, const BigInt &right) {
mpz_mul(left.data, left.data, right.data);
return left;
}
BigInt& operator *=(BigInt &left, const std::string &right) {
return left *= BigInt(right);
}
BigInt& operator *=(BigInt &left, intMpz right) {
mpz_mul_ui(left.data, left.data, static_cast<uIntMpz>(std::abs(right)));
if (right >= 0) {
return left;
}
return left.toNegative();
}
//mod operations
BigInt operator %(BigInt left, const BigInt &right) {
mpz_tdiv_r(left.data, left.data, right.data);
return left;
}
BigInt operator %(BigInt left, intMpz right) {
mpz_tdiv_r_ui(left.data, left.data, static_cast<uIntMpz>(std::abs(right)));
return left;
}
BigInt operator %(intMpz left, BigInt right) {
return BigInt(left) % right;
}
BigInt operator %(BigInt left, const std::string & right) {
return left %= BigInt(right);
}
BigInt operator %(const std::string & left, const BigInt &right) {
return BigInt(left) % right;
}
BigInt& operator %=(BigInt& left, const BigInt &right) {
mpz_tdiv_r(left.data, left.data, right.data);
return left;
}
BigInt& operator %=(BigInt& left, intMpz right) {
mpz_tdiv_r_ui(left.data, left.data, static_cast<uIntMpz>(std::abs(right)));
return left;
}
BigInt& operator %=(BigInt &left, const std::string &right) {
return left %= BigInt(right);
}
// incriment and dicriment
BigInt &BigInt::operator--() {
*this -= 1;
return *this;
}
BigInt &BigInt::operator++() {
*this += 1;
return *this;
}
BigInt BigInt::operator--(int) {
BigInt temp(*this);
--*this;
return temp;
}
BigInt BigInt::operator++(int) {
BigInt temp(*this);
++*this;
return temp;
}
// move operators
BigInt operator >>(BigInt left, int right) {
if (right >= 0) {
mpn_rshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left << right;
}
BigInt operator <<(BigInt left, int right) {
if (right >= 0) {
mpn_lshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left >> right;
}
BigInt& operator >>=(BigInt &left, int right) {
if (right >= 0) {
mpn_rshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left <<= right;
}
BigInt& operator <<=(BigInt &left, int right) {
if (right >= 0) {
mpn_lshift(left.data->_mp_d,
left.data->_mp_d,
left.data->_mp_size,
static_cast<unsigned int>(std::abs(right)));
return left;
}
return left >>= right;
}
// other bin operators
BigInt operator ~(BigInt left) {
mpz_com(left.data, left.data);
return left;
}
BigInt operator |(BigInt left, const BigInt &right) {
mpz_ior(left.data, left.data, right.data);
return left;
}
BigInt operator |(const BigInt &left, intMpz right) {
return left | BigInt(right);
}
BigInt& operator |=(BigInt &left, const BigInt &right) {
mpz_ior(left.data, left.data, right.data);
return left;
}
BigInt& operator |=(BigInt &left, intMpz right) {
return left |= BigInt(right);
}
BigInt operator &(BigInt left, const BigInt &right) {
mpz_and(left.data, left.data, right.data);
return left;
}
BigInt operator &(const BigInt &left, intMpz right) {
return left & BigInt(right);
}
BigInt& operator &=(BigInt &left, const BigInt &right) {
mpz_and(left.data, left.data, right.data);
return left;
}
BigInt& operator &=(BigInt &left, intMpz right) {
return left &= BigInt(right);
}
BigInt operator ^(BigInt left, const BigInt &right) {
mpz_xor(left.data, left.data, right.data);
return left;
}
BigInt operator ^(const BigInt &left, intMpz right) {
return left ^ BigInt(right);
}
BigInt& operator ^=(BigInt &left, const BigInt &right) {
mpz_xor(left.data, left.data, right.data);
return left;
}
BigInt& operator ^=(BigInt &left, intMpz right) {
return left ^= BigInt(right);
}
// logic operators
bool operator!(const BigInt &val) {
return val == 0;
}
bool operator == (const BigInt& left, const BigInt& right) {
return mpz_cmp(left.data, right.data) == 0;
}
bool operator == (const BigInt& left, intMpz right) {
return mpz_cmp_si(left.data, right) == 0;
}
bool operator == (const BigInt &left, const std::string &right) {
return left == BigInt(right);
}
bool operator == ( intMpz left, const BigInt & right) {
return right == left;
}
bool operator == ( const std::string & left, const BigInt & right) {
return right == BigInt(left);
}
bool operator != (const BigInt &left, const BigInt& right) {
return !(left == right);
}
bool operator != (const BigInt &left, intMpz right) {
return !(left == right);
}
bool operator != (const BigInt &left, const std::string &right) {
return left != BigInt(right);
}
bool operator != ( intMpz left, const BigInt & right) {
return right != left;
}
bool operator != ( const std::string & left, const BigInt & right) {
return right != BigInt(left);
}
bool operator < ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) < 0;
}
bool operator < ( const BigInt &left, intMpz right) {
return mpz_cmp_si(left.data, right) < 0;
}
bool operator < ( const BigInt &left, const std::string &right) {
return left < BigInt(right);
}
bool operator < ( intMpz left, const BigInt & right) {
return right > left;
}
bool operator < ( const std::string & left, const BigInt & right) {
return right > BigInt(left);
}
bool operator > ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) > 0;
}
bool operator > ( const BigInt &left, intMpz right) {
return mpz_cmp_si(left.data, right) > 0;
}
bool operator > ( const BigInt &left, const std::string &right) {
return left > BigInt(right);
}
bool operator > ( intMpz left, const BigInt & right) {
return right < left;
}
bool operator > ( const std::string & left, const BigInt & right) {
return right < BigInt(left);
}
bool operator <= ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) <= 0;
}
bool operator <= ( const BigInt &left, intMpz right) {
return mpz_cmp_si(left.data, right) <= 0;
}
bool operator <= ( const BigInt &left, const std::string &right) {
return left <= BigInt(right);
}
bool operator <= ( intMpz left, const BigInt & right) {
return right >= left;
}
bool operator <= ( const std::string & left, const BigInt & right) {
return right >= BigInt(left);
}
bool operator >= ( const BigInt &left, const BigInt& right) {
return mpz_cmp(left.data, right.data) >= 0;
}
bool operator >= ( const BigInt &left, intMpz right) {
return mpz_cmp_si(left.data, right) >= 0;
}
bool operator >= ( const BigInt &left, const std::string &right) {
return left >= BigInt(right);
}
bool operator >= ( intMpz left, const BigInt & right) {
return right <= left;
}
bool operator >= ( const std::string & left, const BigInt & right) {
return right <= BigInt(left);
}
//// cast operations
//BigInt::operator bool() const {
// return *this != 0;
//}

View file

@ -1,206 +0,0 @@
//#
//# Copyright (C) 2018-2019 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef BIGINT_H
#define BIGINT_H
#include "mini-gmp.h"
#include <string>
#include <vector>
#include "minigmp_global.h"
/**
* @brief The BigInt class - c++ minigmp wrapper
*/
class MINIGMPSHARED_EXPORT BigInt
{
mpz_t data;
public:
BigInt();
BigInt(const BigInt& val, int bitCount = -1);
BigInt(const std::string &imput, int base = 10);
BigInt(intMpz val);
BigInt(char item, unsigned int size, int base);
std::string getString(int base = 10) const;
~BigInt();
BigInt& powm(const BigInt &pow, const BigInt &mod);
static BigInt powm(BigInt val, const BigInt & pow, const BigInt &mod);
BigInt& pow(uIntMpz pow);
BigInt& log(int base);
/**
* @brief sizeBits
* @return size of bits in memory
*/
int sizeBits() const;
int sizeBytes() const;
/**
* @brief longBits
* @return current length in Bits of number
*/
int longBits() const;
int longBytes() const;
int sizeType() const;
bool isPrime(bool absalut = false) const;
BigInt& gcd(const BigInt &a, const BigInt &b);
void fromHex(const std::string& hex);
/**
* @brief bigPow10
* @param pow
* @return number 10 ^ pow
*/
static BigInt bigPow10(unsigned short pow);
BigInt& toNegative();
BigInt& operator = (const BigInt& val);
BigInt& operator = (const std::string &imput);
BigInt& operator = (intMpz val);
friend BigInt operator + ( BigInt left, const BigInt& right);
friend BigInt operator + ( BigInt left, const std::string &right);
friend BigInt operator + ( BigInt left, intMpz right);
friend BigInt operator + ( intMpz left, BigInt right);
friend BigInt operator + ( const std::string &left, const BigInt &right);
friend BigInt& operator += ( BigInt &left, intMpz right);
friend BigInt& operator += ( BigInt &left, const BigInt& right);
friend BigInt& operator += ( BigInt &left, const std::string &right);
friend BigInt operator - ( BigInt left, const BigInt& right);
friend BigInt operator - ( BigInt left, intMpz right);
friend BigInt operator - ( BigInt left, const std::string &right);
friend BigInt operator - ( intMpz right, BigInt left);
friend BigInt operator - ( const std::string &right, const BigInt &left);
friend BigInt operator-(BigInt val);
friend BigInt& operator -= ( BigInt &left, intMpz right);
friend BigInt& operator -= ( BigInt &left, const BigInt& right);
friend BigInt& operator -= ( BigInt &left, const std::string &right);
friend BigInt operator / ( BigInt left, const BigInt& right);
friend BigInt operator / ( BigInt left, const std::string &right);
friend BigInt operator / ( BigInt left, intMpz right);
friend BigInt operator / ( intMpz left, BigInt right);
friend BigInt operator / ( const std::string &left, const BigInt &right);
friend BigInt& operator /= ( BigInt &left, intMpz right);
friend BigInt& operator /= ( BigInt &left, const std::string &right);
friend BigInt& operator /= ( BigInt &left, const BigInt& right);
friend BigInt operator * ( BigInt left, const BigInt& right);
friend BigInt operator * ( BigInt left, const std::string &right);
friend BigInt operator * ( BigInt left, intMpz right);
friend BigInt operator * ( intMpz left, BigInt right);
friend BigInt& operator *= ( BigInt &left, const BigInt& right);
friend BigInt& operator *= ( BigInt &left, intMpz right);
friend BigInt& operator *= ( BigInt &left, const std::string &right);
friend BigInt operator % ( BigInt left, const BigInt& right);
friend BigInt operator % ( BigInt left, const std::string &right);
friend BigInt operator % ( BigInt left, intMpz right);
friend BigInt operator % ( intMpz left, BigInt right);
friend BigInt operator % ( const std::string & left, const BigInt &right);
friend BigInt& operator %= ( BigInt &left, intMpz right);
friend BigInt& operator %= ( BigInt &left, const std::string &right);
friend BigInt& operator %= ( BigInt &left, const BigInt& right);
friend BigInt operator << ( BigInt left, int right);
friend BigInt operator >> ( BigInt left, int right);
friend BigInt& operator <<= ( BigInt &left, int right);
friend BigInt& operator >>= ( BigInt &left, int right);
friend bool operator == ( const BigInt& left, const BigInt& right);
friend bool operator == ( const BigInt& left, intMpz right);
friend bool operator == ( const BigInt& left, const std::string& right);
friend bool operator == ( const std::string& left, const BigInt& right);
friend bool operator == ( const BigInt& left, const std::string& right);
friend bool operator == ( intMpz left, const std::string& right);
friend bool operator != ( const BigInt& left, const BigInt& right);
friend bool operator != ( const BigInt& left, intMpz right);
friend bool operator != ( const BigInt& left, const std::string& str);
friend bool operator != ( const std::string& left, const BigInt& right);
friend bool operator != ( const BigInt& left, const std::string& right);
friend bool operator != ( intMpz left, const std::string& right);
friend bool operator < ( const BigInt& left, const BigInt& right);
friend bool operator < ( const BigInt& left, intMpz right);
friend bool operator < ( const BigInt& left, const std::string& str);
friend bool operator < ( const std::string& left, const BigInt& right);
friend bool operator < ( const BigInt& left, const std::string& right);
friend bool operator < ( intMpz left, const std::string& right);
friend bool operator > ( const BigInt& left, const BigInt& right);
friend bool operator > ( const BigInt& left, intMpz right);
friend bool operator > ( const BigInt& left, const std::string& str);
friend bool operator > ( const std::string& left, const BigInt& right);
friend bool operator > ( const BigInt& left, const std::string& right);
friend bool operator > ( intMpz left, const std::string& right);
friend bool operator <= ( const BigInt& left, const BigInt& right);
friend bool operator <= ( const BigInt& left, intMpz right);
friend bool operator <= ( const BigInt& left, const std::string& str);
friend bool operator <= ( const std::string& left, const BigInt& right);
friend bool operator <= ( const BigInt& left, const std::string& right);
friend bool operator <= ( intMpz left, const std::string& right);
friend bool operator >= ( const BigInt& left, const BigInt& right);
friend bool operator >= ( const BigInt& left, intMpz right);
friend bool operator >= ( const BigInt& left, const std::string& str);
friend bool operator >= ( const std::string& left, const BigInt& right);
friend bool operator >= ( const BigInt& left, const std::string& right);
friend bool operator >= ( intMpz left, const std::string& right);
friend bool operator!(const BigInt& val);
BigInt& operator-- ();
BigInt& operator++ ();
BigInt operator-- (int);
BigInt operator++ (int);
friend BigInt operator~ (BigInt val);
friend BigInt operator| (BigInt left, const BigInt& right);
friend BigInt operator| (const BigInt &left, intMpz right);
friend BigInt& operator|= (BigInt &left, const BigInt& right);
friend BigInt& operator|= (BigInt &left, intMpz right);
friend BigInt operator& (BigInt left, const BigInt& right);
friend BigInt operator& (const BigInt &left, intMpz right);
friend BigInt& operator&= (BigInt &left, const BigInt& right);
friend BigInt& operator&= (BigInt &left, intMpz right);
friend BigInt operator^ (BigInt left, const BigInt& right);
friend BigInt operator^ (const BigInt &left, intMpz right);
friend BigInt& operator^= (BigInt &left, const BigInt& right);
friend BigInt& operator^= (BigInt &left, intMpz right);
};
#endif // BIGINT_H

File diff suppressed because it is too large Load diff

View file

@ -1,303 +0,0 @@
/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
Copyright 2011-2015 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
/* About mini-gmp: This is a minimal implementation of a subset of the
GMP interface. It is intended for inclusion into applications which
have modest bignums needs, as a fallback when the real GMP library
is not installed.
This file defines the public interface. */
#ifndef MINI_GMP
#define MINI_GMP
/* For size_t */
#include <stddef.h>
#if defined (__cplusplus)
extern "C" {
#endif
#define UN_USED(X) (void)X
void mp_set_memory_functions (void *(*) (size_t),
void *(*) (void *, size_t, size_t),
void (*) (void *, size_t));
void mp_get_memory_functions (void *(**) (size_t),
void *(**) (void *, size_t, size_t),
void (**) (void *, size_t));
typedef unsigned long long uIntMpz;
typedef long long intMpz;
typedef uIntMpz mp_limb_t;
typedef intMpz mp_size_t;
typedef uIntMpz mp_bitcnt_t;
typedef mp_limb_t *mp_ptr;
typedef const mp_limb_t *mp_srcptr;
typedef struct
{
int _mp_alloc; /* Number of *limbs* allocated and pointed
to by the _mp_d field. */
int _mp_size; /* abs(_mp_size) is the number of limbs the
last field points to. If _mp_size is
negative this is a negative number. */
mp_limb_t *_mp_d; /* Pointer to the limbs. */
} __mpz_struct;
typedef __mpz_struct mpz_t[1];
typedef __mpz_struct *mpz_ptr;
typedef const __mpz_struct *mpz_srcptr;
extern const int mp_bits_per_limb;
void mpn_copyi (mp_ptr, mp_srcptr, mp_size_t);
void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
void mpn_zero (mp_ptr, mp_size_t);
int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
int mpn_zero_p (mp_srcptr, mp_size_t);
mp_limb_t mpn_add_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
mp_limb_t mpn_add (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
mp_limb_t mpn_sub_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
mp_limb_t mpn_sub_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
mp_limb_t mpn_sub (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
mp_limb_t mpn_mul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
mp_limb_t mpn_addmul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
void mpn_mul_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
void mpn_sqr (mp_ptr, mp_srcptr, mp_size_t);
int mpn_perfect_square_p (mp_srcptr, mp_size_t);
mp_size_t mpn_sqrtrem (mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
void mpn_com (mp_ptr, mp_srcptr, mp_size_t);
mp_limb_t mpn_neg (mp_ptr, mp_srcptr, mp_size_t);
mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
size_t mpn_get_str (unsigned char *, int, mp_ptr, mp_size_t);
mp_size_t mpn_set_str (mp_ptr, const unsigned char *, size_t, int);
void mpz_init (mpz_t);
void mpz_init2 (mpz_t, mp_bitcnt_t);
void mpz_clear (mpz_t);
#define mpz_odd_p(z) (((z)->_mp_size != 0) & (int) (z)->_mp_d[0])
#define mpz_even_p(z) (! mpz_odd_p (z))
int mpz_sgn (const mpz_t);
int mpz_cmp_si (const mpz_t, intMpz);
int mpz_cmp_ui (const mpz_t, uIntMpz);
int mpz_cmp (const mpz_t, const mpz_t);
int mpz_cmpabs_ui (const mpz_t, uIntMpz);
int mpz_cmpabs (const mpz_t, const mpz_t);
int mpz_cmp_d (const mpz_t, double);
int mpz_cmpabs_d (const mpz_t, double);
void mpz_abs (mpz_t, const mpz_t);
void mpz_neg (mpz_t, const mpz_t);
void mpz_swap (mpz_t, mpz_t);
void mpz_add_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_add (mpz_t, const mpz_t, const mpz_t);
void mpz_sub_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_ui_sub (mpz_t, uIntMpz, const mpz_t);
void mpz_sub (mpz_t, const mpz_t, const mpz_t);
void mpz_mul_si (mpz_t, const mpz_t, intMpz);
void mpz_mul_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_mul (mpz_t, const mpz_t, const mpz_t);
void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_addmul_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_addmul (mpz_t, const mpz_t, const mpz_t);
void mpz_submul_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_submul (mpz_t, const mpz_t, const mpz_t);
void mpz_cdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
void mpz_fdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
void mpz_tdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
void mpz_cdiv_q (mpz_t, const mpz_t, const mpz_t);
void mpz_fdiv_q (mpz_t, const mpz_t, const mpz_t);
void mpz_tdiv_q (mpz_t, const mpz_t, const mpz_t);
void mpz_cdiv_r (mpz_t, const mpz_t, const mpz_t);
void mpz_fdiv_r (mpz_t, const mpz_t, const mpz_t);
void mpz_tdiv_r (mpz_t, const mpz_t, const mpz_t);
void mpz_cdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_fdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_tdiv_q_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_cdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_fdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_tdiv_r_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
void mpz_mod (mpz_t, const mpz_t, const mpz_t);
void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
int mpz_divisible_p (const mpz_t, const mpz_t);
int mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
uIntMpz mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_tdiv_qr_ui (mpz_t, mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_cdiv_q_ui (mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_fdiv_q_ui (mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_tdiv_q_ui (mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_cdiv_r_ui (mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_fdiv_r_ui (mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_tdiv_r_ui (mpz_t, const mpz_t, uIntMpz);
uIntMpz mpz_cdiv_ui (const mpz_t, uIntMpz);
uIntMpz mpz_fdiv_ui (const mpz_t, uIntMpz);
uIntMpz mpz_tdiv_ui (const mpz_t, uIntMpz);
uIntMpz mpz_mod_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_divexact_ui (mpz_t, const mpz_t, uIntMpz);
int mpz_divisible_ui_p (const mpz_t, uIntMpz);
uIntMpz mpz_gcd_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_gcd (mpz_t, const mpz_t, const mpz_t);
void mpz_gcdext (mpz_t, mpz_t, mpz_t, const mpz_t, const mpz_t);
void mpz_lcm_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_lcm (mpz_t, const mpz_t, const mpz_t);
int mpz_invert (mpz_t, const mpz_t, const mpz_t);
void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
void mpz_sqrt (mpz_t, const mpz_t);
int mpz_perfect_square_p (const mpz_t);
void mpz_pow_ui (mpz_t, const mpz_t, uIntMpz);
void mpz_ui_pow_ui (mpz_t, uIntMpz, uIntMpz);
void mpz_powm (mpz_t, const mpz_t, const mpz_t, const mpz_t);
void mpz_powm_ui (mpz_t, const mpz_t, uIntMpz, const mpz_t);
void mpz_rootrem (mpz_t, mpz_t, const mpz_t, uIntMpz);
int mpz_root (mpz_t, const mpz_t, uIntMpz);
void mpz_fac_ui (mpz_t, uIntMpz);
void mpz_bin_uiui (mpz_t, uIntMpz, uIntMpz);
int mpz_probab_prime_p (const mpz_t, int);
int mpz_tstbit (const mpz_t, mp_bitcnt_t);
void mpz_setbit (mpz_t, mp_bitcnt_t);
void mpz_clrbit (mpz_t, mp_bitcnt_t);
void mpz_combit (mpz_t, mp_bitcnt_t);
void mpz_com (mpz_t, const mpz_t);
void mpz_and (mpz_t, const mpz_t, const mpz_t);
void mpz_ior (mpz_t, const mpz_t, const mpz_t);
void mpz_xor (mpz_t, const mpz_t, const mpz_t);
mp_bitcnt_t mpz_popcount (const mpz_t);
mp_bitcnt_t mpz_hamdist (const mpz_t, const mpz_t);
mp_bitcnt_t mpz_scan0 (const mpz_t, mp_bitcnt_t);
mp_bitcnt_t mpz_scan1 (const mpz_t, mp_bitcnt_t);
int mpz_fits_slong_p (const mpz_t);
int mpz_fits_ulong_p (const mpz_t);
intMpz mpz_get_si (const mpz_t);
uIntMpz mpz_get_ui (const mpz_t);
double mpz_get_d (const mpz_t);
size_t mpz_size (const mpz_t);
mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t);
void mpz_realloc2 (mpz_t, mp_bitcnt_t);
mp_srcptr mpz_limbs_read (mpz_srcptr);
mp_ptr mpz_limbs_modify (mpz_t, mp_size_t);
mp_ptr mpz_limbs_write (mpz_t, mp_size_t);
void mpz_limbs_finish (mpz_t, mp_size_t);
mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t);
#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
void mpz_set_si (mpz_t, intMpz);
void mpz_set_ui (mpz_t, uIntMpz);
void mpz_set (mpz_t, const mpz_t);
void mpz_set_d (mpz_t, double);
void mpz_init_set_si (mpz_t, intMpz);
void mpz_init_set_ui (mpz_t, uIntMpz);
void mpz_init_set (mpz_t, const mpz_t);
void mpz_init_set_d (mpz_t, double);
size_t mpz_sizeinbase (const mpz_t, int);
char *mpz_get_str (char *, int, const mpz_t);
int mpz_set_str (mpz_t, const char *, int);
int mpz_init_set_str (mpz_t, const char *, int);
/* This long list taken from gmp.h. */
/* For reference, "defined(EOF)" cannot be used here. In g++ 2.95.4,
<iostream> defines EOF but not FILE. */
#if defined (FILE) \
|| defined (H_STDIO) \
|| defined (_H_STDIO) /* AIX */ \
|| defined (_STDIO_H) /* glibc, Sun, SCO */ \
|| defined (_STDIO_H_) /* BSD, OSF */ \
|| defined (__STDIO_H) /* Borland */ \
|| defined (__STDIO_H__) /* IRIX */ \
|| defined (_STDIO_INCLUDED) /* HPUX */ \
|| defined (__dj_include_stdio_h_) /* DJGPP */ \
|| defined (_FILE_DEFINED) /* Microsoft */ \
|| defined (__STDIO__) /* Apple MPW MrC */ \
|| defined (_MSL_STDIO_H) /* Metrowerks */ \
|| defined (_STDIO_H_INCLUDED) /* QNX4 */ \
|| defined (_ISO_STDIO_ISO_H) /* Sun C++ */ \
|| defined (__STDIO_LOADED) /* VMS */
size_t mpz_out_str (FILE *, int, const mpz_t);
#endif
void mpz_import (mpz_t, size_t, int, size_t, int, size_t, const void *);
void *mpz_export (void *, size_t *, int, size_t, int, size_t, const mpz_t);
#if defined (__cplusplus)
}
#endif
#endif /* MINI_GMP */

View file

@ -1,21 +0,0 @@
//#
//# Copyright (C) 2018-2019 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef MINIGMP_GLOBAL_H
#define MINIGMP_GLOBAL_H
#ifdef _WIN32
# define MINIGMPSHARED_EXPORT __declspec(dllexport)
#endif
#ifdef linux
# define MINIGMPSHARED_EXPORT __attribute__((visibility("default")))
#endif
#endif //MINIGMP_GLOBAL_H

View file

@ -1,165 +0,0 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

View file

@ -1,403 +0,0 @@
//#
//# Copyright (C) 2018-2019 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "qrsaencryption.h"
#include <QString>
#include <iostream>
#include <ctime>
#include <chrono>
#define KEY_GEN_LIMIT 10
const QString SIGN_MARKER = "-SIGN-";
const int signMarkerLength = SIGN_MARKER.length();
QRSAEncryption::INT eulerFunc(const QRSAEncryption::INT &p, const QRSAEncryption::INT &q) {
return (p - 1) * (q - 1);
}
bool QRSAEncryption::isMutuallyPrime(const INT &a, const INT &b) {
if ( (!(a % 2) && !(b % 2))
|| (!(a % 3) && !(b % 3))
|| (!(a % 5) && !(b % 5))
|| (!(a % 7) && !(b % 7))
) return false;
return INT().gcd(a, b) == 1;
}
QRSAEncryption::Rsa QRSAEncryption::getBitsSize(const INT &i) const {
int rsaBits = RSA_64;
int intBits = i.sizeBits();
while (rsaBits < intBits) {
rsaBits *= 2;
}
return static_cast<QRSAEncryption::Rsa>(rsaBits);
}
QRSAEncryption::Rsa QRSAEncryption::getBitsSize(const QByteArray &key) const {
if (isValidRsaKey(key)) {
return static_cast<QRSAEncryption::Rsa>(key.size() * 4);
}
return QRSAEncryption::Rsa::Invalid;
}
QRSAEncryption::INT QRSAEncryption::fromArray(const QByteArray &array) const {
INT res = 0;
res.fromHex(array.toHex().toStdString());
return res;
}
QByteArray QRSAEncryption::toArray(const INT &i, short sizeBlok) {
QByteArray res;
res = QByteArray::fromHex(QByteArray::fromStdString(i.getString(16)));
if (sizeBlok < 0) {
return res;
}
while (res.size() < sizeBlok) {
res.push_front(char(0));
}
return res.left(sizeBlok);
}
QRSAEncryption::INT QRSAEncryption::randomNumber(bool fullFill) const {
srand(std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::system_clock::now().time_since_epoch()).count()
% std::numeric_limits<int>::max());
INT res{1};
if(fullFill) {
while(res.longBits() < _rsa) {
res *= (rand() % (std::numeric_limits<int>::max() - 1)) + 1;
}
} else {
int longDiff = _rsa / (sizeof (int) * 8);
while (longDiff > 0) {
longDiff--;
res *= (rand() % (std::numeric_limits<int>::max() - 1)) + 1;
}
}
return res;
}
QRSAEncryption::INT QRSAEncryption::toPrime(INT n) const {
if (!(n % 2)) {
++n;
}
INT LN = n;
INT RN = n;
while (true) {
if (LN.isPrime(false)) return LN;
RN+=2;
if (RN.isPrime(false)) return RN;
LN-=2;
}
}
QRSAEncryption::INT QRSAEncryption::randomPrimeNumber(INT no) const {
srand(static_cast<unsigned int>(time(nullptr)));
// max INT
INT max('1', _rsa / 2, 2);
auto p = toPrime(randomNumber() % max);
while(p == no) p = toPrime(randomNumber() % max);
return p;
}
QRSAEncryption::INT QRSAEncryption::extEuclid(INT a, INT b) const {
INT x = 0, y = 1, u = 1, v = 0, gcd = b, m, n, q, r;
while (a != 0) {
q = gcd / a;
r = gcd % a;
m = x - u * q;
n = y - v * q;
gcd = a;
a = r;
x = u;
y = v;
u = m;
v = n;
}
return y;
}
short QRSAEncryption::getBlockSize(INT i) const {
return static_cast<short>(i.longBytes()) - 1;
}
QByteArray QRSAEncryption::encodeBlok(const INT &block, const INT &e, const INT &m, short blockSize) {
return toArray(INT::powm(block, e, m), blockSize);
}
QByteArray QRSAEncryption::decodeBlok(const INT &block, const INT &d, const INT &m, short blockSize) {
return toArray(INT::powm(block, d, m), blockSize);
}
QRSAEncryption::QRSAEncryption(Rsa rsa) {
_rsa = rsa;
}
bool QRSAEncryption::generatePairKeyS(QByteArray &pubKey, QByteArray &privKey, QRSAEncryption::Rsa rsa) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use generatePairKey method";
return generatePairKey(pubKey, privKey, rsa);
}
QByteArray QRSAEncryption::encodeS(const QByteArray &rawData, const QByteArray &pubKey, QRSAEncryption::Rsa rsa, QRSAEncryption::BlockSize blockSizeMode) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use encode method";
return encode(rawData, pubKey, rsa, blockSizeMode);
}
QByteArray QRSAEncryption::decodeS(const QByteArray &rawData, const QByteArray &privKey, QRSAEncryption::Rsa rsa, QRSAEncryption::BlockSize blockSizeMode) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use decode method";
return decode(rawData, privKey, rsa, blockSizeMode);
}
QByteArray QRSAEncryption::signMessageS(QByteArray rawData, const QByteArray &privKey, QRSAEncryption::Rsa rsa) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use signMessage method";
return signMessage(rawData, privKey, rsa);
}
bool QRSAEncryption::checkSignMessageS(const QByteArray &rawData, const QByteArray &pubKey, QRSAEncryption::Rsa rsa) {
qWarning() << "method " << Q_FUNC_INFO <<
" will be deleted in newxt version. please use signMessage method";
return checkSignMessage(rawData, pubKey, rsa);
}
unsigned int QRSAEncryption::getKeyBytesSize(QRSAEncryption::Rsa rsa) {
return rsa / 4;
}
// --- static methods ---
bool QRSAEncryption::generatePairKey(QByteArray &pubKey, QByteArray &privKey,
QRSAEncryption::Rsa rsa) {
return QRSAEncryption(rsa).generatePairKey(pubKey, privKey);
}
QByteArray QRSAEncryption::encode(const QByteArray &rawData, const QByteArray &pubKey,
Rsa rsa, BlockSize blockSizeMode) {
return QRSAEncryption(rsa).encode(rawData, pubKey, blockSizeMode);
}
QByteArray QRSAEncryption::decode(const QByteArray &rawData, const QByteArray &privKey,
Rsa rsa, BlockSize blockSizeMode) {
return QRSAEncryption(rsa).decode(rawData, privKey, blockSizeMode);
}
QByteArray QRSAEncryption::signMessage(QByteArray rawData, const QByteArray &privKey, Rsa rsa) {
return QRSAEncryption(rsa).signMessage(rawData, privKey);
}
bool QRSAEncryption::checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey, Rsa rsa) {
return QRSAEncryption(rsa).checkSignMessage(rawData, pubKey);
}
// --- end of static methods ---
bool QRSAEncryption::generatePairKey(QByteArray &pubKey, QByteArray &privKey) {
int cnt{0};
bool keyGenRes{false};
INT p, q, modul, eilor, e, d;
do {
pubKey.clear();
privKey.clear();
p = randomPrimeNumber();
q = randomPrimeNumber(p);
modul = 0;
while ((modul = p * q) < 0) {
p = toPrime((p - 1) / 2);
}
eilor = eulerFunc(p, q);
e = randomNumber() % eilor;
if (!(e % 2)) --e;
do {
e -= 2;
} while((!isMutuallyPrime(eilor, e)));
d = extEuclid(eilor , e);
while(d < 0 ) {
d += eilor;
}
pubKey.append(toArray(e, _rsa / 8));
pubKey.append(toArray(modul, _rsa / 8));
privKey.append(toArray(d, _rsa / 8));
privKey.append(toArray(modul, _rsa / 8));
} while (!(keyGenRes = testKeyPair(pubKey, privKey)) && (++cnt < KEY_GEN_LIMIT));
if(cnt >= KEY_GEN_LIMIT) qWarning() << QString("(Warning): Exceeded limit of key generation (%0)!").arg(KEY_GEN_LIMIT);
return (keyGenRes && cnt < KEY_GEN_LIMIT);
}
// --- non-static methods ---
QByteArray QRSAEncryption::encode(const QByteArray &rawData, const QByteArray &pubKey, BlockSize blockSizeMode) {
if (getBitsSize(pubKey) != _rsa) {
return QByteArray();
}
int index = 0;
QByteArray block;
INT e = fromArray(pubKey.mid(0, pubKey.size() / 2));
INT m = fromArray(pubKey.mid(pubKey.size() / 2));
short blockSizeOut = getBlockSize(m) + 1; // BlockSize::OneByte
short blockSizeIn = 1; // BlockSize::OneByte
if (blockSizeMode == BlockSize::Auto) {
blockSizeIn = getBlockSize(m);
}
if (!blockSizeIn) {
qDebug() << "module of key small! size = 1 byte, 2 byte is minimum";
return QByteArray();
}
QByteArray res;
while ((block = rawData.mid(index, blockSizeIn)).size()) {
if (index + blockSizeIn > rawData.size() && block.size() && !block[0]) {
qWarning() << "When trying to encrypt data, problems arose, the last block contains non-significant zeros."
" These zeros will be deleted during the decryption process."
" For encode and decode data with non-significant zeros use BlockSize::OneByte";
}
res.append(encodeBlok(fromArray(block), e, m, blockSizeOut));
index += blockSizeIn;
}
return res;
}
QByteArray QRSAEncryption::decode(const QByteArray &rawData, const QByteArray &privKey, BlockSize blockSizeMode) {
if (getBitsSize(privKey) != _rsa) {
return QByteArray();
}
int index = 0;
QByteArray block;
INT d = fromArray(privKey.mid(0, privKey.size() / 2));
INT m = fromArray(privKey.mid(privKey.size() / 2));
short blockSizeIn = getBlockSize(m) + 1;
short blockSizeOut = 1; // BlockSize::OneByte
if (blockSizeMode == BlockSize::Auto) {
blockSizeOut = getBlockSize(m);
}
QByteArray res;
while ((block = rawData.mid(index, blockSizeIn)).size()) {
bool isLastBlock = (index + blockSizeIn) >= rawData.size();
res.append(decodeBlok(fromArray(block), d, m,
(isLastBlock && blockSizeMode == BlockSize::Auto)? -1 : blockSizeOut));
index += blockSizeIn;
}
return res;
}
QByteArray QRSAEncryption::signMessage(QByteArray rawData, const QByteArray &privKey) {
QByteArray hash = QCryptographicHash::hash(rawData, HashAlgorithm::Sha256);
QByteArray signature = encode(hash, privKey, BlockSize::OneByte);
rawData.append(SIGN_MARKER + signature.toHex() + SIGN_MARKER);
return rawData;
}
bool QRSAEncryption::checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey) {
// start position of SIGN_MARKER in rawData
auto signStartPos = rawData.lastIndexOf(SIGN_MARKER, rawData.length() - signMarkerLength - 1);
// length of signature in rawData
auto signLength = rawData.length() - signStartPos - signMarkerLength * 2;
// message, that was recieved from channel
QByteArray message = rawData.left(signStartPos);
// hash, that was decrypt from recieved signature
QByteArray recievedHash = decode(QByteArray::fromHex(rawData.mid(signStartPos + signMarkerLength, signLength)),
pubKey, BlockSize::OneByte);
// if recievedHash == hashAlgorithm(recived message), then signed message is valid
return recievedHash == QCryptographicHash::hash(message, HashAlgorithm::Sha256);
}
QRSAEncryption::Rsa QRSAEncryption::getRsa() const {
return _rsa;
}
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;
}
// --- end of non-static methods ---
bool QRSAEncryption::isValidRsaKey(const QByteArray &key) {
return key.size() && ((static_cast<unsigned int>(key.size()) % getKeyBytesSize(RSA_64)) == 0);
}

View file

@ -1,116 +0,0 @@
//#
//# Copyright (C) 2018-2019 QuasarApp.
//# Distributed under the lgplv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef QRSAENCRYPTION_H
#define QRSAENCRYPTION_H
#include <QByteArray>
#include <QList>
#include <QFile>
#include <cmath>
#include <QDebug>
#include <QCryptographicHash> // to use sha256
#include "./../qtsecret_global.h"
#include <bigint.h>
class Qt_SECRETSHARED_EXPORT QRSAEncryption
{
public:
typedef BigInt INT;
typedef QCryptographicHash::Algorithm HashAlgorithm;
enum Rsa {
Invalid = 0,
RSA_64 = 64,
RSA_128 = 128,
RSA_256 = 256,
RSA_512 = 512,
RSA_1024 = 1024,
RSA_2048 = 2048,
RSA_4096 = 4096,
RSA_8192 = 8192,
};
enum BlockSize {
Auto = 0, // fast but not stable. (using by default)
OneByte = 1 // stable but slow. (using for sig and check sig messages)
};
QRSAEncryption(Rsa rsa = Rsa::RSA_256);
// static methods
// OLDMETHODS DELETE IN next Version
static bool generatePairKeyS(QByteArray &pubKey, QByteArray &privKey,
QRSAEncryption::Rsa rsa = RSA_256);
static QByteArray encodeS(const QByteArray &rawData, const QByteArray &pubKey,
Rsa rsa = RSA_256, BlockSize blockSizeMode = BlockSize::Auto);
static QByteArray decodeS(const QByteArray &rawData, const QByteArray &privKey,
Rsa rsa = RSA_256, BlockSize blockSizeMode = BlockSize::Auto);
static QByteArray signMessageS(QByteArray rawData, const QByteArray &privKey,
Rsa rsa = RSA_256);
static bool checkSignMessageS(const QByteArray &rawData, const QByteArray &pubKey,
Rsa rsa);
// OLDMETHODS END
static bool generatePairKey(QByteArray &pubKey, QByteArray &privKey,
QRSAEncryption::Rsa rsa);
static QByteArray encode(const QByteArray &rawData, const QByteArray &pubKey,
Rsa rsa, BlockSize blockSizeMode = BlockSize::Auto);
static QByteArray decode(const QByteArray &rawData, const QByteArray &privKey,
Rsa rsa, BlockSize blockSizeMode = BlockSize::Auto);
static QByteArray signMessage(QByteArray rawData, const QByteArray &privKey,
Rsa rsa);
static bool checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey,
Rsa rsa);
static bool isValidRsaKey(const QByteArray& key);
static unsigned int getKeyBytesSize(QRSAEncryption::Rsa rsa);
// non-static methods
bool generatePairKey(QByteArray &pubKey, QByteArray &privKey);
QByteArray encode(const QByteArray &rawData, const QByteArray &pubKey,
BlockSize blockSizeMode = BlockSize::Auto);
QByteArray decode(const QByteArray &rawData, const QByteArray &privKey,
BlockSize blockSizeMode = BlockSize::Auto);
QByteArray signMessage(QByteArray rawData, const QByteArray &privKey);
bool checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey);
Rsa getRsa() const;
private:
Rsa _rsa;
bool testKeyPair(const QByteArray &pubKey, const QByteArray &privKey);
bool isMutuallyPrime(const INT &a, const INT &b);
Rsa getBitsSize(const INT& i) const;
Rsa getBitsSize(const QByteArray& array) const;
INT fromArray(const QByteArray& array) const;
QByteArray toArray(const INT &i, short sizeBlok = -1);
INT randomNumber(bool fullFilled = true) const;
INT toPrime(INT) const;
INT randomPrimeNumber(INT no = 0) const;
INT extEuclid(INT a, INT b) const;
short getBlockSize(INT i) const;
QByteArray encodeBlok(const INT& block, const INT& e, const INT& m, short blockSize);
QByteArray decodeBlok(const INT& block, const INT& d, const INT& m, short blockSize);
};
#endif // QRSAENCRYPTION_H

View file

@ -1,24 +0,0 @@
#
# Copyright (C) 2018-2019 QuasarApp.
# Distributed under the lgplv3 software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
!isEmpty(Qt_SECRET_LIB):error("Qt-Secret.pri already included")
Qt_SECRET_LIB = 1
DEFINES += Qt_SECRET_LIBRARY
DEFINES += QT_DEPRECATED_WARNINGS
HEADERS += \
$$PWD/qtsecret_global.h \
$$PWD/Qt-RSA/qrsaencryption.h
SOURCES += \
$$PWD/Qt-RSA/qrsaencryption.cpp
INCLUDEPATH += $$PWD $$PWD/Qt-RSA
include($$PWD/GMP/GMP.pri)

View file

@ -1,36 +0,0 @@
#
# Copyright (C) 2018-2019 QuasarApp.
# Distributed under the lgplv3 software license, see the accompanying
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
QT -= gui
CONFIG += c++11
TARGET = Qt-Secret
TEMPLATE = lib
DEFINES += Qt_SECRET_LIBRARY
DEFINES += QT_DEPRECATED_WARNINGS
#DEPENDS
CONFIG(release, debug|release): {
DESTDIR="$$PWD/build/release"
} else {
DESTDIR="$$PWD/build/debug"
}
include($$PWD/GMP/GMP.pri)
VERSION = 1.2.0
HEADERS += \
qtsecret_global.h \
Qt-RSA/qrsaencryption.h
SOURCES += \
Qt-RSA/qrsaencryption.cpp
target.path = /usr/local/lib
!isEmpty(target.path): INSTALLS += target

View file

@ -1,12 +0,0 @@
#ifndef QTSECRET_GLOBAL_H
#define QTSECRET_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(Qt_SECRET_LIBRARY)
# define Qt_SECRETSHARED_EXPORT Q_DECL_EXPORT
#else
# define Qt_SECRETSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // QTSECRET_GLOBAL_H

1
qt-openssl-encryption Submodule

@ -0,0 +1 @@
Subproject commit 05fbf562c3435f3bbff10a68c031ff465e81d337