QxOrm  1.2.9
C++ Object Relational Mapping library
QxSimpleCrypt.h
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2011, Andre Somers
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the Rathenau Instituut, Andre Somers nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00028 #ifndef _QX_SIMPLECRYPT_H_
00029 #define _QX_SIMPLECRYPT_H_
00030 
00031 #ifdef _MSC_VER
00032 #pragma once
00033 #endif
00034 
00042 #include <QtCore/qglobal.h>
00043 #include <QtCore/qstring.h>
00044 #include <QtCore/qvector.h>
00045 #include <QtCore/qbytearray.h>
00046 
00047 namespace qx {
00048 
00073 class QX_DLL_EXPORT QxSimpleCrypt
00074 {
00075 
00076 public:
00077 
00082    enum CompressionMode {
00083      CompressionAuto,    
00084      CompressionAlways,  
00085      CompressionNever    
00086    };
00087 
00096    enum IntegrityProtectionMode {
00097      ProtectionNone,    
00098      ProtectionChecksum,
00099      ProtectionHash     
00100    };
00101 
00105    enum Error {
00106      ErrorNoError,         
00107      ErrorNoKeySet,        
00108      ErrorUnknownVersion,  
00109      ErrorIntegrityFailed, 
00110    };
00111 
00117    QxSimpleCrypt();
00123    explicit QxSimpleCrypt(quint64 key);
00124 
00128    void setKey(quint64 key);
00132    bool hasKey() const {return !m_keyParts.isEmpty();}
00133 
00140    void setCompressionMode(CompressionMode mode) {m_compressionMode = mode;}
00144    CompressionMode compressionMode() const {return m_compressionMode;}
00145 
00152    void setIntegrityProtectionMode(IntegrityProtectionMode mode) {m_protectionMode = mode;}
00156    IntegrityProtectionMode integrityProtectionMode() const {return m_protectionMode;}
00157 
00161    Error lastError() const {return m_lastError;}
00162 
00168    QString encryptToString(const QString& plaintext) ;
00174    QString encryptToString(QByteArray plaintext) ;
00182    QByteArray encryptToByteArray(const QString& plaintext) ;
00190    QByteArray encryptToByteArray(QByteArray plaintext) ;
00191 
00199    QString decryptToString(const QString& cyphertext) ;
00207    QByteArray decryptToByteArray(const QString& cyphertext) ;
00215    QString decryptToString(QByteArray cypher) ;
00223    QByteArray decryptToByteArray(QByteArray cypher) ;
00224 
00225    // enum to describe options that have been used for the encryption. Currently only one, but
00226    // that only leaves room for future extensions like adding a cryptographic hash...
00227    enum CryptoFlag{CryptoFlagNone = 0,
00228                  CryptoFlagCompression = 0x01,
00229                  CryptoFlagChecksum = 0x02,
00230                  CryptoFlagHash = 0x04
00231                 };
00232    Q_DECLARE_FLAGS(CryptoFlags, CryptoFlag);
00233 
00234 private:
00235 
00236    void splitKey();
00237 
00238    quint64 m_key;
00239    QVector<char> m_keyParts;
00240    CompressionMode m_compressionMode;
00241    IntegrityProtectionMode m_protectionMode;
00242    Error m_lastError;
00243 
00244 };
00245 
00246 } // namespace qx
00247 
00248 Q_DECLARE_OPERATORS_FOR_FLAGS(qx::QxSimpleCrypt::CryptoFlags)
00249 
00250 #endif // _QX_SIMPLECRYPT_H_