This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
qtinyaes.h
68 lines (52 loc) · 1.91 KB
/
qtinyaes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef QTINYAES
#define QTINYAES
#include <QObject>
#include <QByteArray>
#include <QVector>
class QTinyAesPrivate;
class QTinyAes : public QObject
{
Q_OBJECT
Q_PROPERTY(CipherMode mode READ mode WRITE setMode)
Q_PROPERTY(QByteArray key READ key WRITE setKey RESET resetKey)
Q_PROPERTY(QByteArray iv READ iv WRITE setIv RESET resetIv)
public:
enum CipherMode {
CTR,
CBC,
ECB
};
Q_ENUM(CipherMode)
static const int BlockSize;
static const int KeySize;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
static QByteArray generateKey();
#endif
QTinyAes(QObject *parent = nullptr);
QTinyAes(CipherMode mode, const QByteArray &key, const QByteArray &iv = QByteArray(), QObject *parent = nullptr);
~QTinyAes();
CipherMode mode() const;
QByteArray key() const;
QByteArray iv() const;
Q_INVOKABLE QByteArray encrypt(const QByteArray &plain) const;
Q_INVOKABLE QByteArray decrypt(const QByteArray &cipher) const;
Q_INVOKABLE QByteArray encryptRaw(const QByteArray &plain) const;
Q_INVOKABLE QByteArray decryptRaw(const QByteArray &cipher) const;
static QByteArray ctrEncrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &plain);
static QByteArray ctrDecrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &cipher);
static QByteArray cbcEncrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &plain);
static QByteArray cbcDecrypt(const QByteArray &key, const QByteArray &iv, const QByteArray &cipher);
static QByteArray ecbEncrypt(const QByteArray &key, const QByteArray &plain);
static QByteArray ecbDecrypt(const QByteArray &key, const QByteArray &cipher);
public slots:
void setMode(CipherMode mode);
void setKey(const QByteArray &key);
void resetKey();
void setIv(const QByteArray &iv);
void resetIv();
private:
QScopedPointer<QTinyAesPrivate> d;
static void preparePlainText(QByteArray &data);
static void restorePlainText(QByteArray &data);
};
#endif // QTINYAES