Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

digest.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2002 Open Source Telecom Corporation.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 //
00017 // As a special exception to the GNU General Public License, permission is
00018 // granted for additional uses of the text contained in its release
00019 // of Common C++.
00020 //
00021 // The exception is that, if you link the Common C++ library with other
00022 // files to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the Common C++ library code into it.
00026 //
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 //
00030 // This exception applies only to the code released under the
00031 // name Common C++.  If you copy code from other releases into a copy of
00032 // Common C++, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 //
00037 // If you write modifications of your own for Common C++, it is your choice
00038 // whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.
00040 
00046 #ifndef CCXX_DIGEST_H_
00047 #define CCXX_DIGEST_H_
00048 
00049 #ifndef CCXX_MISSING_H_
00050 #include <cc++/missing.h>
00051 #endif
00052 
00053 #ifndef CCXX_THREAD_H_
00054 #include <cc++/thread.h>
00055 #endif
00056 
00057 #ifndef CCXX_EXCEPTION_H_
00058 #include <cc++/exception.h>
00059 #endif
00060 
00061 #ifdef  CCXX_NAMESPACES
00062 namespace ost {
00063 #endif
00064 
00072 class __EXPORT Digest : protected std::streambuf, public std::ostream
00073 {
00074 protected:
00075         Digest();
00076 
00082         virtual unsigned getSize(void) = 0;
00083 
00090         virtual unsigned getDigest(unsigned char *buffer) = 0;
00091 
00098         virtual void putDigest(const unsigned char *buffer, unsigned length) = 0;
00099 
00105         virtual std::ostream &strDigest(std::ostream &os) = 0;
00106 
00107         friend std::ostream &operator<<(std::ostream &os, Digest &ia)
00108                 {return ia.strDigest(os);};
00109 
00110 public:
00114         virtual void initDigest(void) = 0;
00115 };
00116 
00123 class __EXPORT ChecksumDigest : public Digest
00124 {
00125 private:
00126         unsigned char csum;
00127 
00128 protected:
00129         int overflow(int c);
00130         std::ostream &strDigest(std::ostream &os);
00131 
00132 public:
00133         ChecksumDigest();
00134 
00135         void initDigest(void)
00136                 {csum = 0;};
00137 
00138         unsigned getSize(void)
00139                 {return 1;};
00140 
00141         unsigned getDigest(unsigned char *buffer);
00142 
00143         void putDigest(const unsigned char *buffer, unsigned length);
00144 };
00145 
00152 class __EXPORT CRC16Digest : public Digest
00153 {
00154 private:
00155         unsigned short crc16;
00156 
00157 protected:
00158         int overflow(int c);
00159 
00160         std::ostream &strDigest(std::ostream &os);
00161 
00162 public:
00163         CRC16Digest();
00164 
00165         inline void initDigest(void)
00166                 {crc16 = 0;};
00167 
00168         inline unsigned getSize(void)
00169                 {return 2;};
00170 
00171         unsigned getDigest(unsigned char *buffer);
00172 
00173         void putDigest(const unsigned char *buffer, unsigned length);
00174 };
00175 
00183 class __EXPORT CRC32Digest : public Digest
00184 {
00185 private:
00186    unsigned long crc_table[256];
00187    unsigned long crc_reg;
00188    unsigned long crc32;
00189    
00190 protected:
00191    unsigned char overflow(unsigned char octet);
00192 
00193    std::ostream &strDigest(std::ostream &os);
00194    
00195 public:
00196    CRC32Digest();
00197    
00198    void initDigest(void);
00199    
00200    inline unsigned getSize(void) {return 4;}
00201    
00202    unsigned getDigest(unsigned char *buffer);
00203    
00204    void putDigest(const unsigned char *buffer, unsigned length);
00205 };
00206 
00213 class __EXPORT MD5Digest : public Digest
00214 {
00215 private:
00216         unsigned long state[4];
00217         unsigned long count[2];
00218         unsigned char buf[64];
00219         unsigned bpos;
00220         unsigned char md5[16];
00221         bool updated;
00222 
00223 protected:
00224         int overflow(int c);
00225 
00226         void update(void);
00227 
00228         void commit(void);
00229 
00230         std::ostream &strDigest(std::ostream &os);
00231 
00232 public:
00233         MD5Digest();
00234 
00235         void initDigest(void);
00236 
00237         inline unsigned getSize(void)
00238                 {return 16;};
00239 
00240         unsigned getDigest(unsigned char *buffer);
00241 
00242         void putDigest(const unsigned char *buffer, unsigned len);
00243 };
00244 
00245 #ifdef  COMMON_STD_EXCEPTION
00246 
00255 class __EXPORT DigestException : public Exception {
00256 public:
00257         DigestException(const String &str) : Exception(str) {};
00258 };
00259 #endif
00260 
00261 #ifdef  HAVE_64_BITS 
00262 
00273 template <class int_type>
00274 class __EXPORT SHATumbler {
00275 public:
00276         SHATumbler(int);
00277 
00278         SHATumbler(const SHATumbler&);
00279         SHATumbler& operator=(const SHATumbler&);
00280 
00281         int_type& operator[](int);
00282 
00283         ~SHATumbler();
00284   
00285         SHATumbler operator+(const SHATumbler& addend) const;
00286         SHATumbler& operator+=(const SHATumbler& addend);
00287         std::ostream & toString(std::ostream & os);
00288 
00289         friend std::ostream &operator<<(std::ostream &os, SHATumbler<int_type>& ia)
00290         {return ia.toString(os);};
00291 
00292         unsigned getSize();
00293         unsigned placeInBuffer(unsigned char *);
00294     
00295 private:
00296         int_type * h;
00297         int size;
00298 
00299         char * tmp_buff;
00300 
00301         static char format_string[];
00302 };
00303 
00315 class __EXPORT SHAConstant {
00316 protected:
00317         const static uint64 K[];
00318 };
00319 
00338 template <class uint_type, unsigned blockSizeInBytes>
00339 class __EXPORT SHADigest : public Digest {
00340 private:
00341         uint_type totalLengthInBits;
00342 
00343         void copyTempBlock(const SHADigest &);
00344 
00345 protected:
00346         unsigned char tempBlock[blockSizeInBytes];
00347         void initDigest(void);
00348 
00349         virtual void processBlock(const unsigned char * buffer) = 0;
00350         void padBuffer(unsigned char * buffer);
00351 
00352         bool completed;
00353 
00354         SHADigest();
00355         SHADigest(const SHADigest & other);
00356         SHADigest & operator=(const SHADigest & other);
00357 
00358 public:
00359         unsigned getSize(void) = 0;
00360         void putDigest(const unsigned char * buffer, unsigned length) 
00361                 THROWS(DigestException);
00362         std::ostream & strDigest(std::ostream &os) = 0;
00363 
00364 };
00365 
00376 class __EXPORT SHA64DigestHelper: public SHADigest<uint64, 64> {
00377 protected:
00378         SHATumbler<uint32> h;
00379         SHATumbler<uint32> a;
00380 
00381         SHA64DigestHelper(unsigned);
00382         SHATumbler<uint32> getDigest();
00383 
00384 public:
00385         unsigned getDigest(unsigned char * buffer) 
00386         { return getDigest().placeInBuffer(buffer); }
00387         std::ostream & strDigest(std::ostream & os);
00388 
00389         SHA64DigestHelper(const SHA64DigestHelper & other);
00390         SHA64DigestHelper & operator=(const SHA64DigestHelper & other);
00391 };
00392 
00401 class __EXPORT SHA1Digest : public SHA64DigestHelper {
00402 protected:
00403         void processBlock(const unsigned char * buffer);
00404 
00405 public:
00406         SHA1Digest();
00407         void initDigest();
00408         unsigned getSize() { return 20; }
00409         SHA1Digest(const SHA1Digest & other);
00410         SHA1Digest & operator=(const SHA1Digest & other)
00411         { *((SHA64DigestHelper*)this) = other; return *this; }
00412 };
00413 
00419 class __EXPORT SHA256Digest : public SHA64DigestHelper, public SHAConstant {
00420 protected:
00421         void processBlock(const unsigned char * buffer);
00422 
00423 public:
00424         SHA256Digest();
00425         SHA256Digest(const SHA256Digest &);
00426         void initDigest();
00427         unsigned getSize() { return 32; }
00428         SHA256Digest & operator=(const SHA256Digest & other)
00429         { *((SHA64DigestHelper*)this) = other; return *this; }
00430 };
00431 #endif
00432 
00433 #ifdef  CCXX_NAMESPACES
00434 }
00435 #endif
00436 
00437 #endif
00438 

Generated on Tue Jan 18 14:32:36 2005 for GNU CommonC++ by  doxygen 1.3.9.1