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_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