noo/client/encryption.h

75 lines
1.5 KiB
C
Raw Permalink Normal View History

2018-09-24 22:25:14 +03:00
#ifndef ENCRYPTION_H
#define ENCRYPTION_H
#include <QByteArray>
2019-11-13 14:30:18 +02:00
#include "openssl/blowfish.h"
#include "openssl/sha.h"
2018-09-24 22:25:14 +03:00
#include "twofish.h"
class BlowfishCipher
{
protected:
2019-07-28 20:49:34 +03:00
QByteArray mKey;
QByteArray mIV;
BF_KEY mContext;
2018-09-24 22:25:14 +03:00
public:
2019-07-28 20:49:34 +03:00
BlowfishCipher();
~BlowfishCipher();
void setKey(const QByteArray& ba);
const QByteArray& key() const;
void setIV(const QByteArray& ba);
QByteArray& IV() const;
2018-09-24 22:25:14 +03:00
2019-07-28 20:49:34 +03:00
void encrypt(const QByteArray& plain, QByteArray& encrypted);
void decrypt(const QByteArray& encrypted, QByteArray& plain);
2018-09-24 22:25:14 +03:00
};
class TwofishCipher
{
protected:
2019-07-28 20:49:34 +03:00
QByteArray mKey;
QByteArray mIV;
TwofishKey mKeyStruct;
Twofish* mContext;
2018-09-24 22:25:14 +03:00
public:
2019-07-28 20:49:34 +03:00
TwofishCipher();
~TwofishCipher();
2018-09-24 22:25:14 +03:00
2019-07-28 20:49:34 +03:00
int blocksize() const { return 16; }
void setKey(const QByteArray& ba);
const QByteArray& key() const;
2018-09-24 22:25:14 +03:00
2019-07-28 20:49:34 +03:00
// IV vector must be 16 bytes
void setIV(const QByteArray& ba);
const QByteArray& IV() const;
2018-09-24 22:25:14 +03:00
2019-07-28 20:49:34 +03:00
// Plain and encrypted must be padded to 16 bytes boundary before call
void encrypt(const QByteArray& plain, int plainOffset, QByteArray& encrypted, int encryptedOffset);
void decrypt(const QByteArray& encrypted, int encryptedOffset, QByteArray& plain, int plainOffset);
2018-09-24 22:25:14 +03:00
};
class SHA256
{
protected:
2019-07-28 20:49:34 +03:00
SHA256_CTX mContext;
QByteArray mDigest;
2018-09-24 22:25:14 +03:00
public:
2019-07-28 20:49:34 +03:00
SHA256();
~SHA256();
2018-09-24 22:25:14 +03:00
2019-07-28 20:49:34 +03:00
QByteArray& digest();
void update(const void* data, int length);
void final();
2018-09-24 22:25:14 +03:00
};
class IV
{
public:
2019-07-28 20:49:34 +03:00
static void Generate(QByteArray& buffer);
2018-09-24 22:25:14 +03:00
};
#endif // ENCRYPTION_H