| 1 | /* |
| 2 | Copyright (c) 2011, Andre Somers |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the Rathenau Instituut, Andre Somers nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 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_Hcxx . . $ `src/librssguard/3rd-party/sc/simplecrypt.h:29:9`! |
| 30 | |
| 31 | #include <QFlags> |
| 32 | #include <QString> |
| 33 | #include <QVector> |
| 34 | |
| 35 | /** |
| 36 | @short Simple encryption and decryption of strings and byte arrays |
| 37 | |
| 38 | This class provides a simple implementation of encryption and decryption |
| 39 | of strings and byte arrays. |
| 40 | |
| 41 | @warning The encryption provided by this class is NOT strong encryption. It may |
| 42 | help to shield things from curious eyes, but it will NOT stand up to someone |
| 43 | determined to break the encryption. Don't say you were not warned. |
| 44 | |
| 45 | The class uses a 64 bit key. Simply create an instance of the class, set the key, |
| 46 | and use the encryptToString() method to calculate an encrypted version of the input string. |
| 47 | To decrypt that string again, use an instance of SimpleCrypt initialized with |
| 48 | the same key, and call the decryptToString() method with the encrypted string. If the key |
| 49 | matches, the decrypted version of the string will be returned again. |
| 50 | |
| 51 | If you do not provide a key, or if something else is wrong, the encryption and |
| 52 | decryption function will return an empty string or will return a string containing nonsense. |
| 53 | lastError() will return a value indicating if the method was succesful, and if not, why not. |
| 54 | |
| 55 | SimpleCrypt is prepared for the case that the encryption and decryption |
| 56 | algorithm is changed in a later version, by prepending a version identifier to the cypertext. |
| 57 | */ |
| 58 | |
| 59 | class SimpleCryptcxx . . $ SimpleCrypt# { |
| 60 | public: |
| 61 | /** |
| 62 | CompressionMode describes if compression will be applied to the data to be |
| 63 | encrypted. |
| 64 | */ |
| 65 | enum CompressionModecxx . . $ SimpleCrypt#CompressionMode# { |
| 66 | CompressionAutocxx . . $ SimpleCrypt#CompressionAuto., /*!< Only apply compression if that results in a shorter plaintext. */ |
| 67 | CompressionAlwayscxx . . $ SimpleCrypt#CompressionAlways., /*!< Always apply compression. Note that for short inputs, a compression may result in longer |
| 68 | data */ |
| 69 | CompressionNevercxx . . $ SimpleCrypt#CompressionNever. /*!< Never apply compression. */ |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | IntegrityProtectionMode describes measures taken to make it possible to detect problems with the data |
| 74 | or wrong decryption keys. |
| 75 | |
| 76 | Measures involve adding a checksum or a cryptograhpic hash to the data to be encrypted. This |
| 77 | increases the length of the resulting cypertext, but makes it possible to check if the plaintext |
| 78 | appears to be valid after decryption. |
| 79 | */ |
| 80 | enum IntegrityProtectionModecxx . . $ SimpleCrypt#IntegrityProtectionMode# { |
| 81 | ProtectionNonecxx . . $ SimpleCrypt#ProtectionNone., /*!< The integerity of the encrypted data is not protected. It is not really possible to detect a |
| 82 | wrong key, for instance. */ |
| 83 | ProtectionChecksumcxx . . $ SimpleCrypt#ProtectionChecksum., /*!< A simple checksum is used to verify that the data is in order. If not, an empty string is |
| 84 | returned. */ |
| 85 | ProtectionHashcxx . . $ SimpleCrypt#ProtectionHash. /*!< A cryptographic hash is used to verify the integrity of the data. This method produces a much |
| 86 | stronger, but longer check */ |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | Error describes the type of error that occured. |
| 91 | */ |
| 92 | enum Errorcxx . . $ SimpleCrypt#Error# { |
| 93 | ErrorNoErrorcxx . . $ SimpleCrypt#ErrorNoError., /*!< No error occurred. */ |
| 94 | ErrorNoKeySetcxx . . $ SimpleCrypt#ErrorNoKeySet., /*!< No key was set. You can not encrypt or decrypt without a valid key. */ |
| 95 | ErrorUnknownVersioncxx . . $ SimpleCrypt#ErrorUnknownVersion., /*!< The version of this data is unknown, or the data is otherwise not valid. */ |
| 96 | ErrorIntegrityFailedcxx . . $ SimpleCrypt#ErrorIntegrityFailed. /*!< The integrity check of the data failed. Perhaps the wrong key was used. */ |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | Constructor. |
| 101 | |
| 102 | Constructs a SimpleCrypt instance without a valid key set on it. |
| 103 | */ |
| 104 | SimpleCryptcxx . . $ SimpleCrypt#SimpleCrypt(49f6e7a06ebc5aa8).(); |
| 105 | |
| 106 | /** |
| 107 | Constructor. |
| 108 | |
| 109 | Constructs a SimpleCrypt instance and initializes it with the given @arg key. |
| 110 | */ |
| 111 | explicit SimpleCryptcxx . . $ SimpleCrypt#SimpleCrypt(891dc3055356b409).(quint64cxx . . $ quint64# key); |
| 112 | |
| 113 | /** |
| 114 | (Re-) initializes the key with the given @arg key. |
| 115 | */ |
| 116 | void setKeycxx . . $ SimpleCrypt#setKey(891dc3055356b409).(quint64cxx . . $ quint64# key); |
| 117 | |
| 118 | /** |
| 119 | Returns true if SimpleCrypt has been initialized with a key. |
| 120 | */ |
| 121 | bool hasKeycxx . . $ SimpleCrypt#hasKey(50ce9a9e25b4a850).() const { |
| 122 | return !m_keyPartscxx . . $ SimpleCrypt#m_keyParts..isEmptycxx . . $ QList#isEmpty(3482b152b9333168).(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | Sets the compression mode to use when encrypting data. The default mode is Auto. |
| 127 | |
| 128 | Note that decryption is not influenced by this mode, as the decryption recognizes |
| 129 | what mode was used when encrypting. |
| 130 | */ |
| 131 | void setCompressionModecxx . . $ SimpleCrypt#setCompressionMode(75ce82aa4126ef56).(CompressionModecxx . . $ SimpleCrypt#CompressionMode# mode) { |
| 132 | m_compressionModecxx . . $ SimpleCrypt#m_compressionMode. = mode; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | Returns the CompressionMode that is currently in use. |
| 137 | */ |
| 138 | CompressionModecxx . . $ SimpleCrypt#CompressionMode# compressionModecxx . . $ SimpleCrypt#compressionMode(5a2d1842829a5f25).() const { |
| 139 | return m_compressionModecxx . . $ SimpleCrypt#m_compressionMode.; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | Sets the integrity mode to use when encrypting data. The default mode is Checksum. |
| 144 | |
| 145 | Note that decryption is not influenced by this mode, as the decryption recognizes |
| 146 | what mode was used when encrypting. |
| 147 | */ |
| 148 | void setIntegrityProtectionModecxx . . $ SimpleCrypt#setIntegrityProtectionMode(ab6cb298fa960589).(IntegrityProtectionModecxx . . $ SimpleCrypt#IntegrityProtectionMode# mode) { |
| 149 | m_protectionModecxx . . $ SimpleCrypt#m_protectionMode. = mode; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | Returns the IntegrityProtectionMode that is currently in use. |
| 154 | */ |
| 155 | IntegrityProtectionModecxx . . $ SimpleCrypt#IntegrityProtectionMode# integrityProtectionModecxx . . $ SimpleCrypt#integrityProtectionMode(2cc7840c4c4428ea).() const { |
| 156 | return m_protectionModecxx . . $ SimpleCrypt#m_protectionMode.; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | Returns the last error that occurred. |
| 161 | */ |
| 162 | Errorcxx . . $ SimpleCrypt#Error# lastErrorcxx . . $ SimpleCrypt#lastError(de89d58c9db82205).() const { |
| 163 | return m_lastErrorcxx . . $ SimpleCrypt#m_lastError.; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | Encrypts the @arg plaintext string with the key the class was initialized with, and returns |
| 168 | a cyphertext the result. The result is a base64 encoded version of the binary array that is the |
| 169 | actual result of the string, so it can be stored easily in a text format. |
| 170 | */ |
| 171 | QStringcxx . . $ QString# encryptToStringcxx . . $ SimpleCrypt#encryptToString(fd988a2ab94d906b).(const QStringcxx . . $ QString#& plaintext); |
| 172 | |
| 173 | /** |
| 174 | Encrypts the @arg plaintext QByteArray with the key the class was initialized with, and returns |
| 175 | a cyphertext the result. The result is a base64 encoded version of the binary array that is the |
| 176 | actual result of the encryption, so it can be stored easily in a text format. |
| 177 | */ |
| 178 | QStringcxx . . $ QString# encryptToStringcxx . . $ SimpleCrypt#encryptToString(b928598107f8bef5).(QByteArraycxx . . $ QByteArray# plaintext); |
| 179 | |
| 180 | /** |
| 181 | Encrypts the @arg plaintext string with the key the class was initialized with, and returns |
| 182 | a binary cyphertext in a QByteArray the result. |
| 183 | |
| 184 | This method returns a byte array, that is useable for storing a binary format. If you need |
| 185 | a string you can store in a text file, use encryptToString() instead. |
| 186 | */ |
| 187 | QByteArraycxx . . $ QByteArray# encryptToByteArraycxx . . $ SimpleCrypt#encryptToByteArray(743b31b35d83e992).(const QStringcxx . . $ QString#& plaintext); |
| 188 | |
| 189 | /** |
| 190 | Encrypts the @arg plaintext QByteArray with the key the class was initialized with, and returns |
| 191 | a binary cyphertext in a QByteArray the result. |
| 192 | |
| 193 | This method returns a byte array, that is useable for storing a binary format. If you need |
| 194 | a string you can store in a text file, use encryptToString() instead. |
| 195 | */ |
| 196 | QByteArraycxx . . $ QByteArray# encryptToByteArraycxx . . $ SimpleCrypt#encryptToByteArray(f7bd75e5ab38c523).(QByteArraycxx . . $ QByteArray# plaintext); |
| 197 | |
| 198 | /** |
| 199 | Decrypts a cyphertext string encrypted with this class with the set key back to the |
| 200 | plain text version. |
| 201 | |
| 202 | If an error occured, such as non-matching keys between encryption and decryption, |
| 203 | an empty string or a string containing nonsense may be returned. |
| 204 | */ |
| 205 | QStringcxx . . $ QString# decryptToStringcxx . . $ SimpleCrypt#decryptToString(fd988a2ab94d906b).(const QStringcxx . . $ QString#& cyphertext); |
| 206 | |
| 207 | /** |
| 208 | Decrypts a cyphertext string encrypted with this class with the set key back to the |
| 209 | plain text version. |
| 210 | |
| 211 | If an error occured, such as non-matching keys between encryption and decryption, |
| 212 | an empty string or a string containing nonsense may be returned. |
| 213 | */ |
| 214 | QByteArraycxx . . $ QByteArray# decryptToByteArraycxx . . $ SimpleCrypt#decryptToByteArray(743b31b35d83e992).(const QStringcxx . . $ QString#& cyphertext); |
| 215 | |
| 216 | /** |
| 217 | Decrypts a cyphertext binary encrypted with this class with the set key back to the |
| 218 | plain text version. |
| 219 | |
| 220 | If an error occured, such as non-matching keys between encryption and decryption, |
| 221 | an empty string or a string containing nonsense may be returned. |
| 222 | */ |
| 223 | QStringcxx . . $ QString# decryptToStringcxx . . $ SimpleCrypt#decryptToString(b928598107f8bef5).(QByteArraycxx . . $ QByteArray# cypher); |
| 224 | |
| 225 | /** |
| 226 | Decrypts a cyphertext binary encrypted with this class with the set key back to the |
| 227 | plain text version. |
| 228 | |
| 229 | If an error occured, such as non-matching keys between encryption and decryption, |
| 230 | an empty string or a string containing nonsense may be returned. |
| 231 | */ |
| 232 | QByteArraycxx . . $ QByteArray# decryptToByteArraycxx . . $ SimpleCrypt#decryptToByteArray(f7bd75e5ab38c523).(QByteArraycxx . . $ QByteArray# cypher); |
| 233 | |
| 234 | // enum to describe options that have been used for the encryption. Currently only one, but |
| 235 | // that only leaves room for future extensions like adding a cryptographic hash... |
| 236 | enum CryptoFlagcxx . . $ SimpleCrypt#CryptoFlag# { |
| 237 | CryptoFlagNonecxx . . $ SimpleCrypt#CryptoFlagNone. = 0, |
| 238 | CryptoFlagCompressioncxx . . $ SimpleCrypt#CryptoFlagCompression. = 0x01, |
| 239 | CryptoFlagChecksumcxx . . $ SimpleCrypt#CryptoFlagChecksum. = 0x02, |
| 240 | CryptoFlagHashcxx . . $ SimpleCrypt#CryptoFlagHash. = 0x04 |
| 241 | }; |
| 242 | |
| 243 | Q_DECLARE_FLAGScxx . . $ SimpleCrypt#CryptoFlags#(CryptoFlags, CryptoFlag) |
| 244 | |
| 245 | private: |
| 246 | void splitKeycxx . . $ SimpleCrypt#splitKey(49f6e7a06ebc5aa8).(); |
| 247 | |
| 248 | quint64cxx . . $ quint64# m_keycxx . . $ SimpleCrypt#m_key.; |
| 249 | |
| 250 | QVectorcxx . . $ QVector#<char> m_keyPartscxx . . $ SimpleCrypt#m_keyParts.; |
| 251 | CompressionModecxx . . $ SimpleCrypt#CompressionMode# m_compressionModecxx . . $ SimpleCrypt#m_compressionMode.; |
| 252 | IntegrityProtectionModecxx . . $ SimpleCrypt#IntegrityProtectionMode# m_protectionModecxx . . $ SimpleCrypt#m_protectionMode.; |
| 253 | Errorcxx . . $ SimpleCrypt#Error# m_lastErrorcxx . . $ SimpleCrypt#m_lastError.; |
| 254 | }; |
| 255 | |
| 256 | Q_DECLARE_OPERATORS_FOR_FLAGScxx . . $ SimpleCrypt#CryptoFlags#(SimpleCrypt::CryptoFlags) |
| 257 | |
| 258 | #endif // SimpleCrypt_H |