qca_tools.h

Go to the documentation of this file.
00001 /*
00002  * qca_tools.h - Qt Cryptographic Architecture
00003  * Copyright (C) 2003-2007  Justin Karneges <justin@affinix.com>
00004  * Copyright (C) 2004,2005  Brad Hards <bradh@frogmouth.net>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00019  *
00020  */
00021 
00035 #ifndef QCA_TOOLS_H
00036 #define QCA_TOOLS_H
00037 
00038 #include <QSharedData>
00039 #include <QSharedDataPointer>
00040 #include <QMetaType>
00041 #include "qca_export.h"
00042 
00043 class QString;
00044 class QByteArray;
00045 class QTextStream;
00046 
00054 QCA_EXPORT void *qca_secure_alloc(int bytes);
00055 
00064 QCA_EXPORT void qca_secure_free(void *p);
00065 
00073 QCA_EXPORT void *qca_secure_realloc(void *p, int bytes);
00074 
00075 namespace QCA {
00076 
00089 class QCA_EXPORT MemoryRegion
00090 {
00091 public:
00092         MemoryRegion();
00093 
00100         MemoryRegion(const char *str);
00101 
00106         MemoryRegion(const QByteArray &from);
00107 
00111         MemoryRegion(const MemoryRegion &from);
00112         ~MemoryRegion();
00113 
00117         MemoryRegion & operator=(const MemoryRegion &from);
00118 
00122         MemoryRegion & operator=(const QByteArray &from);
00123 
00132         bool isNull() const;
00133 
00142         bool isSecure() const;
00143 
00152         QByteArray toByteArray() const;
00153 
00157         bool isEmpty() const;
00158 
00162         int size() const;
00163 
00173         const char *data() const;
00174 
00183         const char *constData() const;
00184 
00195         const char & at(int index) const;
00196 
00197 protected:
00209         MemoryRegion(bool secure);
00210 
00220         MemoryRegion(int size, bool secure);
00221 
00234         MemoryRegion(const QByteArray &from, bool secure);
00235 
00241         char *data();
00242 
00253         char & at(int index);
00254 
00260         bool resize(int size);
00261 
00271         void set(const QByteArray &from, bool secure);
00272 
00284         void setSecure(bool secure);
00285 
00286 private:
00287         bool _secure;
00288         class Private;
00289         QSharedDataPointer<Private> d;
00290 };
00291 
00307 class QCA_EXPORT SecureArray : public MemoryRegion
00308 {
00309 public:
00313         SecureArray();
00314 
00321         explicit SecureArray(int size, char ch = 0);
00322 
00328         SecureArray(const char *str);
00329 
00337         SecureArray(const QByteArray &a);
00338 
00346         SecureArray(const MemoryRegion &a);
00347 
00353         SecureArray(const SecureArray &from);
00354 
00355         ~SecureArray();
00356 
00360         SecureArray & operator=(const SecureArray &from);
00361 
00367         SecureArray & operator=(const QByteArray &a);
00368 
00372         void clear();
00373 
00379         char & operator[](int index);
00380 
00386         const char & operator[](int index) const;
00387 
00395         char *data();
00396 
00404         const char *data() const;
00405 
00413         const char *constData() const;
00414 
00420         char & at(int index);
00421 
00427         const char & at(int index) const;
00428 
00432         int size() const;
00433 
00443         bool isEmpty() const;
00444 
00453         bool resize(int size);
00454 
00469         void fill(char fillChar, int fillToPosition = -1);
00470 
00476         QByteArray toByteArray() const;
00477 
00481         SecureArray & append(const SecureArray &a);
00482 
00487         bool operator==(const MemoryRegion &other) const;
00488         
00493         inline bool operator!=(const MemoryRegion &other) const
00494         {
00495                 return !(*this == other);
00496         }
00497 
00501         SecureArray & operator+=(const SecureArray &a);
00502 
00503 protected:
00510         void set(const SecureArray &from);
00511 
00518         void set(const QByteArray &from);
00519 };
00520 
00524 QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b);
00525 
00542 class QCA_EXPORT BigInteger
00543 {
00544 public:
00548         BigInteger();
00549 
00555         BigInteger(int n);
00556 
00566         BigInteger(const char *c);
00567 
00573         BigInteger(const QString &s);
00574 
00580         BigInteger(const QCA::SecureArray &a);
00581 
00587         BigInteger(const BigInteger &from);
00588 
00589         ~BigInteger();
00590 
00602         BigInteger & operator=(const BigInteger &from);
00603 
00615         BigInteger & operator=(const QString &s);
00616 
00629         BigInteger & operator+=(const BigInteger &b);
00630 
00643         BigInteger & operator-=(const BigInteger &b);
00644 
00652         QCA::SecureArray toArray() const;
00653 
00663         void fromArray(const QCA::SecureArray &a);
00664 
00674         QString toString() const;
00675 
00688         bool fromString(const QString &s);
00689 
00712         int compare(const BigInteger &n) const;
00713 
00718         inline bool operator==(const BigInteger &other) const
00719         {
00720                 return (compare(other) == 0);
00721         }
00722 
00727         inline bool operator!=(const BigInteger &other) const
00728         {
00729                 return !(*this == other);
00730         }
00731 
00737         inline bool operator<=(const BigInteger &other) const
00738         {
00739                 return (compare(other) <= 0);
00740         }
00741 
00747         inline bool operator>=(const BigInteger &other) const
00748         {
00749                 return (compare(other) >= 0);
00750         }
00751 
00757         inline bool operator<(const BigInteger &other) const
00758         {
00759                 return (compare(other) < 0);
00760         }
00761 
00767         inline bool operator>(const BigInteger &other) const
00768         {
00769                 return (compare(other) > 0);
00770         }
00771 
00772 private:
00773         class Private;
00774         QSharedDataPointer<Private> d;
00775 };
00776 
00777 
00778 
00784 QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b);
00785 
00786 
00787 }
00788 
00789 #endif

Generated on Tue Aug 28 08:19:59 2007 for Qt Cryptographic Architecture by  doxygen 1.5.2