OpenLexocad  27.0
simplecrypt.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4 Copyright (c) 2011, Andre Somers
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  * Neither the name of the Rathenau Instituut, Andre Somers nor the
15  names of its contributors may be used to endorse or promote products
16  derived from this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS BE LIABLE FOR ANY
22 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef SIMPLECRYPT_H
31 #define SIMPLECRYPT_H
32 #include <Core/core_defines2.h>
33 
34 #include <QFlags>
35 #include <QString>
36 #include <QVector>
37 
62 {
63 public:
69  {
72  CompressionNever
73  };
83  {
86  ProtectionHash
88  };
92  enum Error
93  {
98  };
99 
105  SimpleCrypt();
111  explicit SimpleCrypt(quint64 key);
112 
116  void setKey(quint64 key);
120  bool hasKey() const { return !m_keyParts.isEmpty(); }
121 
128  void setCompressionMode(CompressionMode mode) { m_compressionMode = mode; }
132  CompressionMode compressionMode() const { return m_compressionMode; }
133 
140  void setIntegrityProtectionMode(IntegrityProtectionMode mode) { m_protectionMode = mode; }
144  IntegrityProtectionMode integrityProtectionMode() const { return m_protectionMode; }
145 
149  Error lastError() const { return m_lastError; }
150 
156  QString encryptToString(const QString& plaintext);
162  QString encryptToString(QByteArray plaintext);
170  QByteArray encryptToByteArray(const QString& plaintext);
178  QByteArray encryptToByteArray(QByteArray plaintext);
179 
187  QString decryptToString(const QString& cyphertext);
195  QByteArray decryptToByteArray(const QString& cyphertext);
203  QString decryptToString(QByteArray cypher);
211  QByteArray decryptToByteArray(QByteArray cypher);
212 
213  // enum to describe options that have been used for the encryption. Currently only one, but
214  // that only leaves room for future extensions like adding a cryptographic hash...
216  {
217  CryptoFlagNone = 0,
218  CryptoFlagCompression = 0x01,
219  CryptoFlagChecksum = 0x02,
220  CryptoFlagHash = 0x04
221  };
222  Q_DECLARE_FLAGS(CryptoFlags, CryptoFlag);
223 
224 private:
225  void splitKey();
226 
227  quint64 m_key;
228  QVector<char> m_keyParts;
229  CompressionMode m_compressionMode;
230  IntegrityProtectionMode m_protectionMode;
231  Error m_lastError;
232 };
233 Q_DECLARE_OPERATORS_FOR_FLAGS(SimpleCrypt::CryptoFlags)
234 
235 #endif // SimpleCrypt_H
#define CORE_EXPORT
Definition: core_defines2.h:10
Definition: simplecrypt.h:70
Definition: simplecrypt.h:97
IntegrityProtectionMode
Definition: simplecrypt.h:82
CompressionMode compressionMode() const
Definition: simplecrypt.h:132
Definition: simplecrypt.h:94
CryptoFlag
Definition: simplecrypt.h:215
IntegrityProtectionMode integrityProtectionMode() const
Definition: simplecrypt.h:144
CompressionMode
Definition: simplecrypt.h:68
void setIntegrityProtectionMode(IntegrityProtectionMode mode)
Definition: simplecrypt.h:140
Definition: simplecrypt.h:95
Simple encryption and decryption of strings and byte arrays.
Definition: simplecrypt.h:61
void setCompressionMode(CompressionMode mode)
Definition: simplecrypt.h:128
Error
Definition: simplecrypt.h:92
bool hasKey() const
Definition: simplecrypt.h:120
Definition: simplecrypt.h:85
Definition: simplecrypt.h:84
Definition: simplecrypt.h:71
Error lastError() const
Definition: simplecrypt.h:149
Definition: simplecrypt.h:96