OpenLexocad  28.0
simplecrypt.h
Go to the documentation of this file.
1 
2 /*
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 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #pragma once
30 
31 #include <QString>
32 #include <QVector>
33 
57 class LX_CORE_EXPORT SimpleCrypt
58 {
59 public:
65  {
68  CompressionNever
69  };
79  {
82  ProtectionHash
84  };
88  enum Error
89  {
94  };
95 
107  explicit SimpleCrypt(quint64 key);
108 
112  void setKey(quint64 key);
116  bool hasKey() const { return !m_keyParts.isEmpty(); }
117 
124  void setCompressionMode(CompressionMode mode) { m_compressionMode = mode; }
128  CompressionMode compressionMode() const { return m_compressionMode; }
129 
136  void setIntegrityProtectionMode(IntegrityProtectionMode mode) { m_protectionMode = mode; }
140  IntegrityProtectionMode integrityProtectionMode() const { return m_protectionMode; }
141 
145  Error lastError() const { return m_lastError; }
146 
152  QString encryptToString(const QString& plaintext);
158  QString encryptToString(QByteArray plaintext);
166  QByteArray encryptToByteArray(const QString& plaintext);
174  QByteArray encryptToByteArray(QByteArray plaintext);
175 
183  QString decryptToString(const QString& cyphertext);
191  QByteArray decryptToByteArray(const QString& cyphertext);
199  QString decryptToString(QByteArray cypher);
207  QByteArray decryptToByteArray(QByteArray cypher);
208 
209  // enum to describe options that have been used for the encryption. Currently only one, but
210  // that only leaves room for future extensions like adding a cryptographic hash...
212  {
213  CryptoFlagNone = 0,
214  CryptoFlagCompression = 0x01,
215  CryptoFlagChecksum = 0x02,
216  CryptoFlagHash = 0x04
217  };
219 
220 private:
221  void splitKey();
222 
223  quint64 m_key;
224  QVector<char> m_keyParts;
225  CompressionMode m_compressionMode;
226  IntegrityProtectionMode m_protectionMode;
227  Error m_lastError;
228 };
229 Q_DECLARE_OPERATORS_FOR_FLAGS(SimpleCrypt::CryptoFlags)
230 
SimpleCrypt::ProtectionChecksum
@ ProtectionChecksum
Definition: simplecrypt.h:81
SimpleCrypt::encryptToByteArray
QByteArray encryptToByteArray(const QString &plaintext)
SimpleCrypt::ErrorNoKeySet
@ ErrorNoKeySet
Definition: simplecrypt.h:91
SimpleCrypt::CompressionMode
CompressionMode
Definition: simplecrypt.h:65
SimpleCrypt::setIntegrityProtectionMode
void setIntegrityProtectionMode(IntegrityProtectionMode mode)
Definition: simplecrypt.h:136
SimpleCrypt::setKey
void setKey(quint64 key)
SimpleCrypt::SimpleCrypt
SimpleCrypt(quint64 key)
SimpleCrypt::SimpleCrypt
SimpleCrypt()
SimpleCrypt::Q_DECLARE_FLAGS
Q_DECLARE_FLAGS(CryptoFlags, CryptoFlag)
SimpleCrypt::ErrorUnknownVersion
@ ErrorUnknownVersion
Definition: simplecrypt.h:92
SimpleCrypt::compressionMode
CompressionMode compressionMode() const
Definition: simplecrypt.h:128
SimpleCrypt::setCompressionMode
void setCompressionMode(CompressionMode mode)
Definition: simplecrypt.h:124
SimpleCrypt::CryptoFlag
CryptoFlag
Definition: simplecrypt.h:212
SimpleCrypt::ProtectionNone
@ ProtectionNone
Definition: simplecrypt.h:80
SimpleCrypt::encryptToString
QString encryptToString(const QString &plaintext)
SimpleCrypt::encryptToByteArray
QByteArray encryptToByteArray(QByteArray plaintext)
SimpleCrypt::CompressionAlways
@ CompressionAlways
Definition: simplecrypt.h:67
SimpleCrypt::integrityProtectionMode
IntegrityProtectionMode integrityProtectionMode() const
Definition: simplecrypt.h:140
SimpleCrypt::Error
Error
Definition: simplecrypt.h:89
SimpleCrypt::decryptToByteArray
QByteArray decryptToByteArray(const QString &cyphertext)
SimpleCrypt
Simple encryption and decryption of strings and byte arrays.
Definition: simplecrypt.h:58
SimpleCrypt::encryptToString
QString encryptToString(QByteArray plaintext)
SimpleCrypt::CompressionAuto
@ CompressionAuto
Definition: simplecrypt.h:66
SimpleCrypt::decryptToByteArray
QByteArray decryptToByteArray(QByteArray cypher)
SimpleCrypt::lastError
Error lastError() const
Definition: simplecrypt.h:145
SimpleCrypt::ErrorIntegrityFailed
@ ErrorIntegrityFailed
Definition: simplecrypt.h:93
SimpleCrypt::decryptToString
QString decryptToString(QByteArray cypher)
SimpleCrypt::IntegrityProtectionMode
IntegrityProtectionMode
Definition: simplecrypt.h:79
SimpleCrypt::decryptToString
QString decryptToString(const QString &cyphertext)
SimpleCrypt::ErrorNoError
@ ErrorNoError
Definition: simplecrypt.h:90
SimpleCrypt::hasKey
bool hasKey() const
Definition: simplecrypt.h:116