Files
pip/libs/main/crypt/picrypt.h

190 lines
8.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*! \file picrypt.h
* \ingroup Crypt
* \~\brief
* \~english Cryptographic using libsodium
* \~russian Шифрование с помощью libsodium
*/
/*
PIP - Platform Independent Primitives
Cryptographic class using lib Sodium
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of 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.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PICRYPT_H
#define PICRYPT_H
#include "pip_crypt_export.h"
#include "pistring.h"
//! \ingroup Crypt
//! \~\brief
//! \~english Class for encrypting and decrypting data.
//! \~russian Класс для шифрования и дешифрования данных.
class PIP_CRYPT_EXPORT PICrypt {
public:
//! \~\brief
//! \~english Constructor that generates a random key
//! \~russian Конструктор, генерирующий случайный ключ
PICrypt();
~PICrypt();
//! \~\brief
//! \~english Set key to "key", key size must be a \a sizeKey()
//! \~russian Установить ключ "key", размер ключа должен быть равен \a sizeKey()
bool setKey(const PIByteArray & key);
//! \~\brief
//! \~english Generate and set key from keyphrase "secret"
//! \~russian Генерировать и установить ключ из ключевой фразы "secret"
bool setKey(const PIString & secret);
//! \~\brief
//! \~english Returns current key
//! \~russian Возвращает текущий ключ
PIByteArray key() { return key_; }
//! \~\brief
//! \~english Encrypt given data "data", result size will be increased by \a sizeCrypt()
//! \~russian Зашифровать данные "data", размер результата увеличится на \a sizeCrypt()
PIByteArray crypt(const PIByteArray & data);
//! \~\brief
//! \~english Decrypt given data "crypt_data"
//! \~russian Расшифровать данные "crypt_data"
PIByteArray decrypt(const PIByteArray & crypt_data, bool * ok = 0);
//! \~\brief
//! \~english Encrypt given data "data" with key "key", result size will be increased by \a sizeCrypt()
//! \~russian Зашифровать данные "data" ключом "key", размер результата увеличится на \a sizeCrypt()
static PIByteArray crypt(const PIByteArray & data, PIByteArray key);
//! \~\brief
//! \~english Decrypt given data "crypt_data" with key "key"
//! \~russian Расшифровать данные "crypt_data" ключом "key"
static PIByteArray decrypt(const PIByteArray & crypt_data, PIByteArray key, bool * ok = 0);
//! \~\brief
//! \~english Generate hash from keyphrase "secret", may be used as a key for encryption
//! \~russian Генерировать хэш из ключевой фразы "secret", может использоваться в качестве ключа для шифрования
static PIByteArray hash(PIString secret);
//! \~\brief
//! \~english Generate hash from bytearray
//! \~russian Генерировать хэш из массива байт
static PIByteArray hash(const PIByteArray & data);
//! \~\brief
//! \~english Generate hash from bytearray
//! \~russian Генерировать хэш из массива байт
static PIByteArray hash(const PIByteArray & data, const unsigned char * key, size_t keylen);
//! \~\brief
//! \~english Returns hash size
//! \~russian Возвращает размер хэша
static size_t sizeHash();
//! \~\brief
//! \~english Generate short hash from string "s", may be used for hash table
//! \~russian Генерировать короткий хэш из строки "s", может использоваться для хэш-таблиц
static ullong shorthash(const PIString & s, PIByteArray key = PIByteArray());
//! \~\brief
//! \~english Generate random key
//! \~russian Генерировать случайный ключ
static PIByteArray generateKey();
//! \~\brief
//! \~english Generate random buffer
//! \~russian Генерировать случайный буфер
static PIByteArray generateRandomBuff(int size);
//! \~\brief
//! \~english Returns key size
//! \~russian Возвращает размер ключа
static size_t sizeKey();
//! \~\brief
//! \~english Returns size which be added to data size in encryption process
//! \~russian Возвращает размер, который будет добавлен к размеру данных в процессе шифрования
static size_t sizeCrypt();
//! \~\brief
//! \~english Function randomly generates a secret key and a corresponding public key for digital signature
//! \~russian Функция случайным образом генерирует секретный ключ и соответствующий ему открытый ключ для цифровой подписи
static bool generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key);
//! \~\brief
//! \~english Function generates a secret key from input data and a corresponding public key for digital signature
//! \~russian Функция генерирует секретный ключ из входных данных и соответствующий ему открытый ключ для цифровой подписи
static bool generateSignKeys(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed);
//! \~\brief
//! \~english Function extract sign public key from sing secret key
//! \~russian Функция извлекает открытый ключ для подписи из секретного ключа для подписи
static PIByteArray extractSignPublicKey(const PIByteArray & secret_key);
//! \~\brief
//! \~english Calculate digital signature for data
//! \~russian Вычислить цифровую подпись для данных
PIByteArray signMessage(const PIByteArray & data, const PIByteArray & secret_key);
//! \~\brief
//! \~english Verify digital signature of signed message
//! \~russian Проверить цифровую подпись подписанного сообщения
bool verifySign(const PIByteArray & data, const PIByteArray & signature, const PIByteArray & public_key);
//! \~\brief
//! \~english Function randomly generates a secret key and a corresponding public key for authenticated encryption
//! \~russian Функция случайным образом генерирует секретный ключ и соответствующий ему открытый ключ для аутентифицированного
//! шифрования
static bool generateKeypair(PIByteArray & public_key, PIByteArray & secret_key);
//! \~\brief
//! \~english Function generates a secret key from input data and a corresponding public key for authenticated encryption
//! \~russian Функция генерирует секретный ключ из входных данных и соответствующий ему открытый ключ для аутентифицированного
//! шифрования
static bool generateKeypair(PIByteArray & public_key, PIByteArray & secret_key, const PIByteArray & seed);
//! \~\brief
//! \~english Encrypt given data "data"
//! \~russian Зашифровать данные "data"
PIByteArray crypt(const PIByteArray & data, const PIByteArray & public_key, const PIByteArray & secret_key);
//! \~\brief
//! \~english Decrypt given data "crypt_data"
//! \~russian Расшифровать данные "crypt_data"
PIByteArray decrypt(const PIByteArray & crypt_data, const PIByteArray & public_key, const PIByteArray & secret_key, bool * ok = 0);
//! \~\brief
//! \~english Generate password hash from "password"
//! \~russian Генерировать хэш пароля из "password"
static PIByteArray passwordHash(PIString password, const PIByteArray & seed);
//! \~\brief
//! \~english Returns libsodium version
//! \~russian Возвращает версию libsodium
static PIString version();
private:
static bool init();
PIByteArray nonce_, key_;
};
#endif // PICRYPT_H