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 
00087 class QCA_EXPORT MemoryRegion
00088 {
00089 public:
00090         MemoryRegion();
00091 
00098         MemoryRegion(const char *str);
00099 
00104         MemoryRegion(const QByteArray &from);
00105 
00109         MemoryRegion(const MemoryRegion &from);
00110         ~MemoryRegion();
00111 
00115         MemoryRegion & operator=(const MemoryRegion &from);
00116 
00120         MemoryRegion & operator=(const QByteArray &from);
00121 
00130         bool isNull() const;
00131 
00140         bool isSecure() const;
00141 
00150         QByteArray toByteArray() const;
00151 
00155         bool isEmpty() const;
00156 
00160         int size() const;
00161 
00171         const char *data() const;
00172 
00181         const char *constData() const;
00182 
00193         const char & at(int index) const;
00194 
00195 protected:
00207         MemoryRegion(bool secure);
00208 
00218         MemoryRegion(int size, bool secure);
00219 
00232         MemoryRegion(const QByteArray &from, bool secure);
00233 
00239         char *data();
00240 
00251         char & at(int index);
00252 
00258         bool resize(int size);
00259 
00269         void set(const QByteArray &from, bool secure);
00270 
00282         void setSecure(bool secure);
00283 
00284 private:
00285         bool _secure;
00286         class Private;
00287         QSharedDataPointer<Private> d;
00288 };
00289 
00304 class QCA_EXPORT SecureArray : public MemoryRegion
00305 {
00306 public:
00310         SecureArray();
00311 
00318         explicit SecureArray(int size, char ch = 0);
00319 
00325         SecureArray(const char *str);
00326 
00334         SecureArray(const QByteArray &a);
00335 
00343         SecureArray(const MemoryRegion &a);
00344 
00350         SecureArray(const SecureArray &from);
00351 
00352         ~SecureArray();
00353 
00357         SecureArray & operator=(const SecureArray &from);
00358 
00364         SecureArray & operator=(const QByteArray &a);
00365 
00369         void clear();
00370 
00376         char & operator[](int index);
00377 
00383         const char & operator[](int index) const;
00384 
00392         char *data();
00393 
00401         const char *data() const;
00402 
00410         const char *constData() const;
00411 
00417         char & at(int index);
00418 
00424         const char & at(int index) const;
00425 
00429         int size() const;
00430 
00440         bool isEmpty() const;
00441 
00450         bool resize(int size);
00451 
00466         void fill(char fillChar, int fillToPosition = -1);
00467 
00473         QByteArray toByteArray() const;
00474 
00478         SecureArray & append(const SecureArray &a);
00479 
00484         bool operator==(const MemoryRegion &other) const;
00485         
00490         inline bool operator!=(const MemoryRegion &other) const
00491         {
00492                 return !(*this == other);
00493         }
00494 
00498         SecureArray & operator+=(const SecureArray &a);
00499 
00500 protected:
00507         void set(const SecureArray &from);
00508 
00515         void set(const QByteArray &from);
00516 };
00517 
00521 QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b);
00522 
00537 class QCA_EXPORT BigInteger
00538 {
00539 public:
00543         BigInteger();
00544 
00550         BigInteger(int n);
00551 
00561         BigInteger(const char *c);
00562 
00568         BigInteger(const QString &s);
00569 
00575         BigInteger(const QCA::SecureArray &a);
00576 
00582         BigInteger(const BigInteger &from);
00583 
00584         ~BigInteger();
00585 
00597         BigInteger & operator=(const BigInteger &from);
00598 
00610         BigInteger & operator=(const QString &s);
00611 
00624         BigInteger & operator+=(const BigInteger &b);
00625 
00638         BigInteger & operator-=(const BigInteger &b);
00639 
00647         QCA::SecureArray toArray() const;
00648 
00658         void fromArray(const QCA::SecureArray &a);
00659 
00669         QString toString() const;
00670 
00683         bool fromString(const QString &s);
00684 
00707         int compare(const BigInteger &n) const;
00708 
00713         inline bool operator==(const BigInteger &other) const
00714         {
00715                 return (compare(other) == 0);
00716         }
00717 
00722         inline bool operator!=(const BigInteger &other) const
00723         {
00724                 return !(*this == other);
00725         }
00726 
00732         inline bool operator<=(const BigInteger &other) const
00733         {
00734                 return (compare(other) <= 0);
00735         }
00736 
00742         inline bool operator>=(const BigInteger &other) const
00743         {
00744                 return (compare(other) >= 0);
00745         }
00746 
00752         inline bool operator<(const BigInteger &other) const
00753         {
00754                 return (compare(other) < 0);
00755         }
00756 
00762         inline bool operator>(const BigInteger &other) const
00763         {
00764                 return (compare(other) > 0);
00765         }
00766 
00767 private:
00768         class Private;
00769         QSharedDataPointer<Private> d;
00770 };
00771 
00777 QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b);
00778 
00779 }
00780 
00781 #endif

Generated on Fri Jul 6 12:14:04 2007 for Qt Cryptographic Architecture by  doxygen 1.4.6