OpenLexocad  27.1
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 <QFlags>
33 #include <QString>
34 #include <QVector>
35 
59 class LX_CORE_EXPORT SimpleCrypt
60 {
61 public:
67  {
70  CompressionNever
71  };
81  {
84  ProtectionHash
86  };
90  enum Error
91  {
96  };
97 
103  SimpleCrypt();
109  explicit SimpleCrypt(quint64 key);
110 
114  void setKey(quint64 key);
118  bool hasKey() const { return !m_keyParts.isEmpty(); }
119 
126  void setCompressionMode(CompressionMode mode) { m_compressionMode = mode; }
130  CompressionMode compressionMode() const { return m_compressionMode; }
131 
138  void setIntegrityProtectionMode(IntegrityProtectionMode mode) { m_protectionMode = mode; }
142  IntegrityProtectionMode integrityProtectionMode() const { return m_protectionMode; }
143 
147  Error lastError() const { return m_lastError; }
148 
154  QString encryptToString(const QString& plaintext);
160  QString encryptToString(QByteArray plaintext);
168  QByteArray encryptToByteArray(const QString& plaintext);
176  QByteArray encryptToByteArray(QByteArray plaintext);
177 
185  QString decryptToString(const QString& cyphertext);
193  QByteArray decryptToByteArray(const QString& cyphertext);
201  QString decryptToString(QByteArray cypher);
209  QByteArray decryptToByteArray(QByteArray cypher);
210 
211  // enum to describe options that have been used for the encryption. Currently only one, but
212  // that only leaves room for future extensions like adding a cryptographic hash...
214  {
215  CryptoFlagNone = 0,
216  CryptoFlagCompression = 0x01,
217  CryptoFlagChecksum = 0x02,
218  CryptoFlagHash = 0x04
219  };
220  Q_DECLARE_FLAGS(CryptoFlags, CryptoFlag);
221 
222 private:
223  void splitKey();
224 
225  quint64 m_key;
226  QVector<char> m_keyParts;
227  CompressionMode m_compressionMode;
228  IntegrityProtectionMode m_protectionMode;
229  Error m_lastError;
230 };
231 Q_DECLARE_OPERATORS_FOR_FLAGS(SimpleCrypt::CryptoFlags)
232 
233 #endif // SimpleCrypt_H
Definition: simplecrypt.h:68
Definition: simplecrypt.h:95
IntegrityProtectionMode
Definition: simplecrypt.h:80
CompressionMode compressionMode() const
Definition: simplecrypt.h:130
Definition: simplecrypt.h:92
CryptoFlag
Definition: simplecrypt.h:213
IntegrityProtectionMode integrityProtectionMode() const
Definition: simplecrypt.h:142
CompressionMode
Definition: simplecrypt.h:66
void setIntegrityProtectionMode(IntegrityProtectionMode mode)
Definition: simplecrypt.h:138
Definition: simplecrypt.h:93
Simple encryption and decryption of strings and byte arrays.
Definition: simplecrypt.h:59
void setCompressionMode(CompressionMode mode)
Definition: simplecrypt.h:126
Error
Definition: simplecrypt.h:90
Definition: DocObject.h:25
bool hasKey() const
Definition: simplecrypt.h:118
Definition: simplecrypt.h:83
Definition: simplecrypt.h:82
Definition: simplecrypt.h:69
Error lastError() const
Definition: simplecrypt.h:147
Definition: simplecrypt.h:94