QtLingo 1
QtLingo is an Application to make Qt Computer Translations easier
SimpleCrypt.h
Go to the documentation of this file.
1/*
2 * https://wiki.qt.io/Simple_encryption_with_SimpleCrypt#Versions
3 * Copyright (c) 2011, Andre Somers
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the Rathenau Instituut, Andre Somers nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR #######; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef SIMPLECRYPT_H
29#define SIMPLECRYPT_H
30
31#include <QString>
32#include <QVector>
33#include <QFlags>
34#include <QByteArray>
35#include <QtDebug>
36#include <QtGlobal>
37#include <QDateTime>
38#include <QCryptographicHash>
39#include <QDataStream>
40#include <QRandomGenerator>
41// STD
42#include <iostream>
43#include <assert.h>
44#include <algorithm>
45#include <sstream>
46#include <stdio.h>
47#include <stdlib.h>
48
72{
73 public:
74
78 {
82 };
83
90 {
94 };
95 // Error describes the type of error that occured.
96 enum Error
97 {
102 };
103
107 SimpleCrypt();
108
112 explicit SimpleCrypt(quint64 key);
113
116 void setKey(quint64 key);
117
120 bool hasKey() const {return !myKeyParts.isEmpty();}
121
126 void setCompressionMode(CompressionMode mode) {myCompressionMode = mode;}
127
130 CompressionMode compressionMode() const {return myCompressionMode;}
131
136 void setIntegrityProtectionMode(IntegrityProtectionMode mode) {myProtectionMode = mode;}
137 // Returns the IntegrityProtectionMode that is currently in use.
138 IntegrityProtectionMode integrityProtectionMode() const {return myProtectionMode;}
139
140 // Returns the last error that occurred.
141 Error lastError() const {return myLastError;}
142
143
149 QString encryptToString(const QString &plaintext);
150
156 QString encryptToString(const QByteArray &plaintext);
157
164 QByteArray encryptToByteArray(const QString &plaintext);
165
172 QByteArray encryptToByteArray(const QByteArray &plaintext);
173
179 QString decryptToString(const QString &cyphertext);
180
186 QByteArray decryptToByteArray(const QString &cyphertext);
187
192 QString decryptToString(const QByteArray &cypher);
193
199 QByteArray decryptToByteArray(QByteArray cypher);
200
205 {
209 CryptoFlagHash = 0x04
210 };
212 // Is Debug Message
213 void setDebugMessage(bool thisState);
214 bool getDebugMessage();
215 void setMessage(const QString &thisMessage);
216
217 private:
218
219 void splitKey();
220
221 quint64 myKey;
222 QVector<char> myKeyParts;
223 CompressionMode myCompressionMode;
224 IntegrityProtectionMode myProtectionMode;
225 Error myLastError;
226 bool isDebugMessage = true;
227}; // end class SimpleCrypt
228Q_DECLARE_OPERATORS_FOR_FLAGS(SimpleCrypt::CryptoFlags)
229#endif // SimpleCrypt_H
230
Simple encryption and decryption of strings and byte arrays.
Definition: SimpleCrypt.h:72
@ CompressionAlways
Definition: SimpleCrypt.h:80
@ CompressionNever
Definition: SimpleCrypt.h:81
bool getDebugMessage()
get Debug Message
Definition: SimpleCrypt.cpp:63
IntegrityProtectionMode
Definition: SimpleCrypt.h:90
@ ProtectionChecksum
Definition: SimpleCrypt.h:92
void setIntegrityProtectionMode(IntegrityProtectionMode mode)
Definition: SimpleCrypt.h:136
Q_DECLARE_FLAGS(CryptoFlags, CryptoFlag)
void setDebugMessage(bool thisState)
set Debug Message
Definition: SimpleCrypt.cpp:55
void setMessage(const QString &thisMessage)
set Message
QString decryptToString(const QString &cyphertext)
decrypt To String QString. decryptToString
void setKey(quint64 key)
set Key. setKey
Definition: SimpleCrypt.cpp:71
@ ErrorUnknownVersion
Definition: SimpleCrypt.h:100
@ ErrorIntegrityFailed
Definition: SimpleCrypt.h:101
@ CryptoFlagChecksum
CryptoFlagChecksum
Definition: SimpleCrypt.h:208
@ CryptoFlagNone
CryptoFlagNone
Definition: SimpleCrypt.h:206
@ CryptoFlagCompression
CryptoFlagCompression
Definition: SimpleCrypt.h:207
@ CryptoFlagHash
CryptoFlagHash
Definition: SimpleCrypt.h:209
CompressionMode compressionMode() const
Definition: SimpleCrypt.h:130
SimpleCrypt()
Constructor. SimpleCrypt.
Definition: SimpleCrypt.cpp:34
IntegrityProtectionMode integrityProtectionMode() const
Definition: SimpleCrypt.h:138
Error lastError() const
Definition: SimpleCrypt.h:141
QByteArray decryptToByteArray(const QString &cyphertext)
decrypt To Byte Array QString. decryptToByteArray
bool hasKey() const
Definition: SimpleCrypt.h:120
void setCompressionMode(CompressionMode mode)
Definition: SimpleCrypt.h:126
QByteArray encryptToByteArray(const QString &plaintext)
encrypt To Byte Array QString. encryptToByteArray
Definition: SimpleCrypt.cpp:96
QString encryptToString(const QString &plaintext)
encrypt To String QString. encryptToString