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
00038
00039
00040
00046 #ifndef CCXX_DIGEST_H_
00047 #define CCXX_DIGEST_H_
00048
00049 #ifndef CCXX_CONFIG_H_
00050 #include <cc++/config.h>
00051 #endif
00052
00053 #ifndef CCXX_THREAD_H_
00054 #include <cc++/thread.h>
00055 #endif
00056
00057 #include <iostream>
00058 #include <fstream>
00059
00060 #ifdef CCXX_NAMESPACES
00061 namespace ost {
00062 #endif
00063
00064 #ifdef WIN32
00065 class CCXX_CLASS_EXPORT Digest;
00066 class CCXX_CLASS_EXPORT ChecksumDigest;
00067 class CCXX_CLASS_EXPORT CRC16Digest;
00068 class CCXX_CLASS_EXPORT CRC32Digest;
00069 class CCXX_CLASS_EXPORT MD5Digest;
00070 class CCXX_CLASS_EXPORT SHAConstant;
00071 class CCXX_CLASS_EXPORT SHA64DigestHelper;
00072 class CCXX_CLASS_EXPORT SHA1Digest;
00073 class CCXX_CLASS_EXPORT SHA256Digest;
00074 #endif
00075
00083 #if defined(__KCC)
00084 #define ostream ostream_withassign
00085 #endif
00086 class Digest : protected std::streambuf, public std::ostream
00087 {
00088 protected:
00089 Digest();
00090
00094 virtual void initDigest(void) = 0;
00095
00101 virtual unsigned getSize(void) = 0;
00102
00109 virtual unsigned getDigest(unsigned char *buffer) = 0;
00110
00117 virtual void putDigest(const unsigned char *buffer, unsigned length) = 0;
00118
00124 virtual std::ostream &strDigest(std::ostream &os) = 0;
00125
00126 friend std::ostream &operator<<(std::ostream &os, Digest &ia)
00127 {return ia.strDigest(os);};
00128 };
00129
00136 class ChecksumDigest : public Digest
00137 {
00138 private:
00139 unsigned char csum;
00140
00141 protected:
00142 int overflow(int c);
00143 std::ostream &strDigest(std::ostream &os);
00144
00145 public:
00146 ChecksumDigest();
00147
00148 void initDigest(void)
00149 {csum = 0;};
00150
00151 unsigned getSize(void)
00152 {return 1;};
00153
00154 unsigned getDigest(unsigned char *buffer);
00155
00156 void putDigest(const unsigned char *buffer, unsigned length);
00157 };
00158
00165 class CRC16Digest : public Digest
00166 {
00167 private:
00168 unsigned short crc16;
00169
00170 protected:
00171 int overflow(int c);
00172
00173 std::ostream &strDigest(std::ostream &os);
00174
00175 public:
00176 CRC16Digest();
00177
00178 inline void initDigest(void)
00179 {crc16 = 0;};
00180
00181 inline unsigned getSize(void)
00182 {return 2;};
00183
00184 unsigned getDigest(unsigned char *buffer);
00185
00186 void putDigest(const unsigned char *buffer, unsigned length);
00187 };
00188
00196 class CRC32Digest : public Digest
00197 {
00198 private:
00199 unsigned long crc_table[256];
00200 unsigned long crc_reg;
00201 unsigned long crc32;
00202
00203 protected:
00204 unsigned char overflow(unsigned char octet);
00205
00206 std::ostream &strDigest(std::ostream &os);
00207
00208 public:
00209 CRC32Digest();
00210
00211 void initDigest(void);
00212
00213 inline unsigned getSize(void) {return 4;}
00214
00215 unsigned getDigest(unsigned char *buffer);
00216
00217 void putDigest(const unsigned char *buffer, unsigned length);
00218 };
00219
00226 class MD5Digest : public Digest
00227 {
00228 private:
00229 unsigned long state[4];
00230 unsigned long count[2];
00231 unsigned char buf[64];
00232 unsigned bpos;
00233 unsigned char md5[16];
00234 bool updated;
00235
00236 protected:
00237 int overflow(int c);
00238
00239 void update(void);
00240
00241 void commit(void);
00242
00243 std::ostream &strDigest(std::ostream &os);
00244
00245 public:
00246 MD5Digest();
00247
00248 void initDigest(void);
00249
00250 inline unsigned getSize(void)
00251 {return 16;};
00252
00253 unsigned getDigest(unsigned char *buffer);
00254
00255 void putDigest(const unsigned char *buffer, unsigned len);
00256 };
00257
00267 class DigestException : public Exception {
00268 public:
00269 DigestException(const std::string & what_arg) throw();
00270 virtual ~DigestException() throw();
00271 };
00272
00284 template <class int_type>
00285 class CCXX_CLASS_EXPORT SHATumbler {
00286 public:
00287 SHATumbler(int);
00288
00289 SHATumbler(const SHATumbler&);
00290 SHATumbler& operator=(const SHATumbler&);
00291
00292 int_type& operator[](int);
00293
00294 ~SHATumbler();
00295
00296 SHATumbler operator+(const SHATumbler& addend) const;
00297 SHATumbler& operator+=(const SHATumbler& addend);
00298 std::ostream & toString(std::ostream & os);
00299
00300 friend std::ostream &operator<<(std::ostream &os, SHATumbler<int_type>& ia)
00301 {return ia.toString(os);};
00302
00303 unsigned getSize();
00304 unsigned placeInBuffer(unsigned char *);
00305
00306 private:
00307 int_type * h;
00308 int size;
00309
00310 char * tmp_buff;
00311
00312 static char format_string[];
00313 };
00314
00326 class SHAConstant {
00327 protected:
00328 const static uint64 K[];
00329 };
00330
00349 template <class uint_type, unsigned blockSizeInBytes>
00350 class CCXX_CLASS_EXPORT SHADigest : public Digest {
00351 private:
00352 uint_type totalLengthInBits;
00353
00354 void copyTempBlock(const SHADigest &);
00355
00356 protected:
00357 unsigned char tempBlock[blockSizeInBytes];
00358 void initDigest(void);
00359
00360 virtual void processBlock(const unsigned char * buffer) = 0;
00361 void padBuffer(unsigned char * buffer);
00362
00363 bool completed;
00364
00365 SHADigest();
00366 SHADigest(const SHADigest & other);
00367 SHADigest & operator=(const SHADigest & other);
00368
00369 public:
00370 unsigned getSize(void) = 0;
00371 void putDigest(const unsigned char * buffer, unsigned length)
00372 throw(DigestException);
00373 std::ostream & strDigest(std::ostream &os) = 0;
00374
00375 };
00376
00387 class SHA64DigestHelper: public SHADigest<uint64, 64> {
00388 protected:
00389 SHATumbler<uint32> h;
00390 SHATumbler<uint32> a;
00391
00392 SHA64DigestHelper(unsigned);
00393 SHATumbler<uint32> getDigest();
00394
00395 public:
00396 unsigned getDigest(unsigned char * buffer)
00397 { return getDigest().placeInBuffer(buffer); }
00398 std::ostream & strDigest(std::ostream & os);
00399
00400 SHA64DigestHelper(const SHA64DigestHelper & other);
00401 SHA64DigestHelper & operator=(const SHA64DigestHelper & other);
00402 };
00403
00412 class SHA1Digest : public SHA64DigestHelper {
00413 protected:
00414 void processBlock(const unsigned char * buffer);
00415
00416 public:
00417 SHA1Digest();
00418 void initDigest();
00419 unsigned getSize() { return 20; }
00420 SHA1Digest(const SHA1Digest & other);
00421 SHA1Digest & operator=(const SHA1Digest & other)
00422 { *((SHA64DigestHelper*)this) = other; return *this; }
00423 };
00424
00430 class SHA256Digest : public SHA64DigestHelper, public SHAConstant {
00431 protected:
00432 void processBlock(const unsigned char * buffer);
00433
00434 public:
00435 SHA256Digest();
00436 SHA256Digest(const SHA256Digest &);
00437 void initDigest();
00438 unsigned getSize() { return 32; }
00439 SHA256Digest & operator=(const SHA256Digest & other)
00440 { *((SHA64DigestHelper*)this) = other; return *this; }
00441 };
00442
00443 #ifdef CCXX_NAMESPACES
00444 };
00445 #endif
00446
00447 #endif
00448