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 
00308 class QCA_EXPORT SecureArray : public MemoryRegion
00309 {
00310 public:
00314         SecureArray();
00315 
00322         explicit SecureArray(int size, char ch = 0);
00323 
00329         SecureArray(const char *str);
00330 
00338         SecureArray(const QByteArray &a);
00339 
00347         SecureArray(const MemoryRegion &a);
00348 
00354         SecureArray(const SecureArray &from);
00355 
00356         ~SecureArray();
00357 
00361         SecureArray & operator=(const SecureArray &from);
00362 
00368         SecureArray & operator=(const QByteArray &a);
00369 
00373         void clear();
00374 
00380         char & operator[](int index);
00381 
00387         const char & operator[](int index) const;
00388 
00396         char *data();
00397 
00405         const char *data() const;
00406 
00414         const char *constData() const;
00415 
00421         char & at(int index);
00422 
00428         const char & at(int index) const;
00429 
00433         int size() const;
00434 
00444         bool isEmpty() const;
00445 
00454         bool resize(int size);
00455 
00470         void fill(char fillChar, int fillToPosition = -1);
00471 
00477         QByteArray toByteArray() const;
00478 
00482         SecureArray & append(const SecureArray &a);
00483 
00488         bool operator==(const MemoryRegion &other) const;
00489         
00494         inline bool operator!=(const MemoryRegion &other) const
00495         {
00496                 return !(*this == other);
00497         }
00498 
00502         SecureArray & operator+=(const SecureArray &a);
00503 
00504 protected:
00511         void set(const SecureArray &from);
00512 
00519         void set(const QByteArray &from);
00520 };
00521 
00525 QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b);
00526 
00543 class QCA_EXPORT BigInteger
00544 {
00545 public:
00549         BigInteger();
00550 
00556         BigInteger(int n);
00557 
00567         BigInteger(const char *c);
00568 
00574         BigInteger(const QString &s);
00575 
00581         BigInteger(const QCA::SecureArray &a);
00582 
00588         BigInteger(const BigInteger &from);
00589 
00590         ~BigInteger();
00591 
00603         BigInteger & operator=(const BigInteger &from);
00604 
00616         BigInteger & operator=(const QString &s);
00617 
00630         BigInteger & operator+=(const BigInteger &b);
00631 
00644         BigInteger & operator-=(const BigInteger &b);
00645 
00653         QCA::SecureArray toArray() const;
00654 
00664         void fromArray(const QCA::SecureArray &a);
00665 
00675         QString toString() const;
00676 
00689         bool fromString(const QString &s);
00690 
00713         int compare(const BigInteger &n) const;
00714 
00719         inline bool operator==(const BigInteger &other) const
00720         {
00721                 return (compare(other) == 0);
00722         }
00723 
00728         inline bool operator!=(const BigInteger &other) const
00729         {
00730                 return !(*this == other);
00731         }
00732 
00738         inline bool operator<=(const BigInteger &other) const
00739         {
00740                 return (compare(other) <= 0);
00741         }
00742 
00748         inline bool operator>=(const BigInteger &other) const
00749         {
00750                 return (compare(other) >= 0);
00751         }
00752 
00758         inline bool operator<(const BigInteger &other) const
00759         {
00760                 return (compare(other) < 0);
00761         }
00762 
00768         inline bool operator>(const BigInteger &other) const
00769         {
00770                 return (compare(other) > 0);
00771         }
00772 
00773 private:
00774         class Private;
00775         QSharedDataPointer<Private> d;
00776 };
00777 
00778 
00779 
00785 QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b);
00786 
00787 
00788 }
00789 
00790 #endif

Generated on Tue Aug 7 18:06:27 2007 for Qt Cryptographic Architecture by  doxygen 1.5.1