publickey.h

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /** @file publickey.h Interface for PublicKey class */
00020 
00021 #ifndef __PUBLICKEY_H__
00022 #define __PUBLICKEY_H__
00023 
00024 /**
00025  * PublicKey indicates an entities public RSA key. PublicKey objects share
00026  * the underlying data and are thus inexpensive to copy. PublicKey object's
00027  * contents can never be modified.
00028  */
00029 class PublicKey {
00030 public:
00031         //! Dummy default constructor
00032         PublicKey() : m_keyLen() {}
00033 
00034         //! Construct from string data
00035         PublicKey(const std::string &key)
00036         : m_key(new uint8_t[key.size()]), m_keyLen(key.size()) {
00037                 memcpy(m_key.get(), key.c_str(), key.size());
00038         }
00039 
00040         uint8_t* c_str() const { return m_key.get(); }
00041         size_t   size()  const { return m_keyLen;    }
00042 
00043         //! implicit conversion to bool checks for contained data
00044         operator bool()  const { return m_key; }
00045 
00046         //! Clears the contents of this key
00047         void clear() { m_key.reset(); m_keyLen = 0; }
00048 
00049         //! inequality operator to bool
00050         friend bool operator!(const PublicKey &p) { return !p.m_key; }
00051 
00052         friend bool operator<(const PublicKey &x, const PublicKey &y) {
00053                 if ((x.m_key && !y.m_key) || (!x.m_key && y.m_key)) {
00054                         return false;
00055                 }
00056                 if (x.m_keyLen != y.m_keyLen) {
00057                         return x.m_keyLen < y.m_keyLen;
00058                 }
00059                 return memcmp(x.m_key.get(), y.m_key.get(), x.m_keyLen) < 0;
00060         }
00061         friend bool operator==(const PublicKey &x, const PublicKey &y) {
00062                 if (x.m_keyLen != y.m_keyLen) {
00063                         return false;
00064                 }
00065                 return !memcmp(x.m_key.get(), y.m_key.get(), x.m_keyLen);
00066         }
00067         friend bool operator!=(const PublicKey &x, const PublicKey &y) {
00068                 return !(x == y);
00069         }
00070 private:
00071         boost::shared_array<uint8_t> m_key;
00072         uint8_t m_keyLen;
00073 };
00074 
00075 #endif