00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00043 #ifndef CCXX_DIGEST_H_
00044 #define CCXX_DIGEST_H_
00045
00046 #ifndef CCXX_MISSING_H_
00047 #include <cc++/missing.h>
00048 #endif
00049
00050 #ifndef CCXX_THREAD_H_
00051 #include <cc++/thread.h>
00052 #endif
00053
00054 #ifndef CCXX_EXCEPTION_H_
00055 #include <cc++/exception.h>
00056 #endif
00057
00058 #ifdef CCXX_NAMESPACES
00059 namespace ost {
00060 #endif
00061
00069 class __EXPORT Digest : protected std::streambuf, public std::ostream
00070 {
00071 protected:
00072 Digest();
00073
00079 virtual unsigned getSize(void) = 0;
00080
00087 virtual unsigned getDigest(unsigned char *buffer) = 0;
00088
00095 virtual void putDigest(const unsigned char *buffer, unsigned length) = 0;
00096
00102 virtual std::ostream &strDigest(std::ostream &os) = 0;
00103
00104 friend std::ostream &operator<<(std::ostream &os, Digest &ia)
00105 {return ia.strDigest(os);};
00106
00107 public:
00111 virtual void initDigest(void) = 0;
00112 };
00113
00120 class __EXPORT ChecksumDigest : public Digest
00121 {
00122 private:
00123 unsigned char csum;
00124
00125 protected:
00126 int overflow(int c);
00127 std::ostream &strDigest(std::ostream &os);
00128
00129 public:
00130 ChecksumDigest();
00131
00132 void initDigest(void)
00133 {csum = 0;};
00134
00135 unsigned getSize(void)
00136 {return 1;};
00137
00138 unsigned getDigest(unsigned char *buffer);
00139
00140 void putDigest(const unsigned char *buffer, unsigned length);
00141 };
00142
00149 class __EXPORT CRC16Digest : public Digest
00150 {
00151 private:
00152 unsigned short crc16;
00153
00154 protected:
00155 int overflow(int c);
00156
00157 std::ostream &strDigest(std::ostream &os);
00158
00159 public:
00160 CRC16Digest();
00161
00162 inline void initDigest(void)
00163 {crc16 = 0;};
00164
00165 inline unsigned getSize(void)
00166 {return 2;};
00167
00168 unsigned getDigest(unsigned char *buffer);
00169
00170 void putDigest(const unsigned char *buffer, unsigned length);
00171 };
00172
00180 class __EXPORT CRC32Digest : public Digest
00181 {
00182 private:
00183 unsigned long crc_table[256];
00184 unsigned long crc_reg;
00185 unsigned long crc32;
00186
00187 protected:
00188 unsigned char overflow(unsigned char octet);
00189
00190 std::ostream &strDigest(std::ostream &os);
00191
00192 public:
00193 CRC32Digest();
00194
00195 void initDigest(void);
00196
00197 inline unsigned getSize(void) {return 4;}
00198
00199 unsigned getDigest(unsigned char *buffer);
00200
00201 void putDigest(const unsigned char *buffer, unsigned length);
00202 };
00203
00210 class __EXPORT MD5Digest : public Digest
00211 {
00212 private:
00213 unsigned long state[4];
00214 unsigned long count[2];
00215 unsigned char buf[64];
00216 unsigned bpos;
00217 unsigned char md5[16];
00218 bool updated;
00219
00220 protected:
00221 int overflow(int c);
00222
00223 void update(void);
00224
00225 void commit(void);
00226
00227 std::ostream &strDigest(std::ostream &os);
00228
00229 public:
00230 MD5Digest();
00231
00232 void initDigest(void);
00233
00234 inline unsigned getSize(void)
00235 {return 16;};
00236
00237 unsigned getDigest(unsigned char *buffer);
00238
00239 void putDigest(const unsigned char *buffer, unsigned len);
00240 };
00241
00242 #ifdef COMMON_STD_EXCEPTION
00243
00252 class __EXPORT DigestException : public Exception {
00253 public:
00254 DigestException(const String &str) : Exception(str) {};
00255 };
00256 #endif
00257
00258 #ifdef HAVE_64_BITS
00259
00270 template <class int_type>
00271 class __EXPORT SHATumbler {
00272 public:
00273 SHATumbler(int);
00274
00275 SHATumbler(const SHATumbler&);
00276 SHATumbler& operator=(const SHATumbler&);
00277
00278 int_type& operator[](int);
00279
00280 ~SHATumbler();
00281
00282 SHATumbler operator+(const SHATumbler& addend) const;
00283 SHATumbler& operator+=(const SHATumbler& addend);
00284 std::ostream & toString(std::ostream & os);
00285
00286 friend std::ostream &operator<<(std::ostream &os, SHATumbler<int_type>& ia)
00287 {return ia.toString(os);};
00288
00289 unsigned getSize();
00290 unsigned placeInBuffer(unsigned char *);
00291
00292 private:
00293 int_type * h;
00294 int size;
00295
00296 char * tmp_buff;
00297
00298 static char format_string[];
00299 };
00300
00312 class __EXPORT SHAConstant {
00313 protected:
00314 const static uint64 K[];
00315 };
00316
00335 template <class uint_type, unsigned blockSizeInBytes>
00336 class __EXPORT SHADigest : public Digest {
00337 private:
00338 uint_type totalLengthInBits;
00339
00340 void copyTempBlock(const SHADigest &);
00341
00342 protected:
00343 unsigned char tempBlock[blockSizeInBytes];
00344 void initDigest(void);
00345
00346 virtual void processBlock(const unsigned char * buffer) = 0;
00347 void padBuffer(unsigned char * buffer);
00348
00349 bool completed;
00350
00351 SHADigest();
00352 SHADigest(const SHADigest & other);
00353 SHADigest & operator=(const SHADigest & other);
00354
00355 public:
00356 unsigned getSize(void) = 0;
00357 void putDigest(const unsigned char * buffer, unsigned length)
00358 THROWS(DigestException);
00359 std::ostream & strDigest(std::ostream &os) = 0;
00360
00361 };
00362
00373 class __EXPORT SHA64DigestHelper: public SHADigest<uint64, 64> {
00374 protected:
00375 SHATumbler<uint32> h;
00376 SHATumbler<uint32> a;
00377
00378 SHA64DigestHelper(unsigned);
00379 SHATumbler<uint32> getDigest();
00380
00381 public:
00382 unsigned getDigest(unsigned char * buffer)
00383 { return getDigest().placeInBuffer(buffer); }
00384 std::ostream & strDigest(std::ostream & os);
00385
00386 SHA64DigestHelper(const SHA64DigestHelper & other);
00387 SHA64DigestHelper & operator=(const SHA64DigestHelper & other);
00388 };
00389
00398 class __EXPORT SHA1Digest : public SHA64DigestHelper {
00399 protected:
00400 void processBlock(const unsigned char * buffer);
00401
00402 public:
00403 SHA1Digest();
00404 void initDigest();
00405 unsigned getSize() { return 20; }
00406 SHA1Digest(const SHA1Digest & other);
00407 SHA1Digest & operator=(const SHA1Digest & other)
00408 { *((SHA64DigestHelper*)this) = other; return *this; }
00409 };
00410
00416 class __EXPORT SHA256Digest : public SHA64DigestHelper, public SHAConstant {
00417 protected:
00418 void processBlock(const unsigned char * buffer);
00419
00420 public:
00421 SHA256Digest();
00422 SHA256Digest(const SHA256Digest &);
00423 void initDigest();
00424 unsigned getSize() { return 32; }
00425 SHA256Digest & operator=(const SHA256Digest & other)
00426 { *((SHA64DigestHelper*)this) = other; return *this; }
00427 };
00428 #endif
00429
00430 #ifdef CCXX_NAMESPACES
00431 }
00432 #endif
00433
00434 #endif
00435