Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 1999-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | // |
| 18 | // As a special exception, you may use this file as part of a free software |
| 19 | // library without restriction. Specifically, if other files instantiate |
| 20 | // templates or use macros or inline functions from this file, or you compile |
| 21 | // this file and link it with other files to produce an executable, this |
| 22 | // file does not by itself cause the resulting executable to be covered by |
| 23 | // the GNU General Public License. This exception does not however |
| 24 | // invalidate any other reasons why the executable file might be covered by |
| 25 | // the GNU General Public License. |
| 26 | // |
| 27 | // This exception applies only to the code released under the name GNU |
| 28 | // Common C++. If you copy code from other releases into a copy of GNU |
| 29 | // Common C++, as the General Public License permits, the exception does |
| 30 | // not apply to the code that you add in this way. To avoid misleading |
| 31 | // anyone as to the status of such modified files, you must delete |
| 32 | // this exception notice from them. |
| 33 | // |
| 34 | // If you write modifications of your own for GNU Common C++, it is your choice |
| 35 | // whether to permit this exception to apply to your modifications. |
| 36 | // If you do not wish that, delete this exception notice. |
| 37 | // |
| 38 | |
| 39 | /** |
| 40 | * @file digest.h |
| 41 | * @short Digest algorithms: checksum, CRC and MD5. |
| 42 | **/ |
| 43 | |
| 44 | #ifndef CCXX_DIGEST_H_ |
| 45 | #define CCXX_DIGEST_H_ |
| 46 | |
| 47 | #ifndef CCXX_MISSING_H_ |
| 48 | #include <cc++/missing.h> |
| 49 | #endif |
| 50 | |
| 51 | #ifndef CCXX_THREAD_H_ |
| 52 | #include <cc++/thread.h> |
| 53 | #endif |
| 54 | |
| 55 | #ifndef CCXX_EXCEPTION_H_ |
| 56 | #include <cc++/exception.h> |
| 57 | #endif |
| 58 | |
| 59 | #ifdef CCXX_NAMESPACES |
| 60 | namespace ost { |
| 61 | #endif |
| 62 | |
| 63 | /** |
| 64 | * The digest base class is used for implementing and deriving one way |
| 65 | * hashing functions. |
| 66 | * |
| 67 | * @author David Sugar <dyfet@ostel.com> |
| 68 | * @short base class for hashing services. |
| 69 | */ |
| 70 | class __EXPORT Digest : protected std::streambuf, public std::ostream |
| 71 | { |
| 72 | protected: |
| 73 | Digest(); |
| 74 | |
| 75 | /** |
| 76 | * Get the size of a digest in octets. |
| 77 | * |
| 78 | * @return number of bytes in digest. |
| 79 | */ |
| 80 | virtual unsigned getSize(void) = 0; |
| 81 | |
| 82 | /** |
| 83 | * Copy the binary digest buffer to user memory. |
| 84 | * |
| 85 | * @return number of bytes in digest. |
| 86 | * @param buffer to write into. |
| 87 | */ |
| 88 | virtual unsigned getDigest(unsigned char *buffer) = 0; |
| 89 | |
| 90 | /** |
| 91 | * Put data into the digest bypassing the stream subsystem. |
| 92 | * |
| 93 | * @param buffer to read from. |
| 94 | * @param length of data. |
| 95 | */ |
| 96 | virtual void putDigest(const unsigned char *buffer, unsigned length) = 0; |
| 97 | |
| 98 | /** |
| 99 | * print a digest string for export. |
| 100 | * |
| 101 | * @return string representation of digest. |
| 102 | */ |
| 103 | virtual std::ostream &strDigest(std::ostream &os) = 0; |
| 104 | |
| 105 | friend std::ostream &operator<<(std::ostream &os, Digest &ia) |
| 106 | {return ia.strDigest(os);}; |
| 107 | |
| 108 | public: |
| 109 | /** |
| 110 | * Reset the digest table to an initial default value. |
| 111 | */ |
| 112 | virtual void initDigest(void) = 0; |
| 113 | |
| 114 | virtual ~Digest(); |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * A simple checksum digest function. |
| 119 | * |
| 120 | * @author David Sugar <dyfet@ostel.com> |
| 121 | * @short checksum hash function. |
| 122 | */ |
| 123 | class __EXPORT ChecksumDigest : public Digest |
| 124 | { |
| 125 | private: |
| 126 | unsigned char csum; |
| 127 | |
| 128 | protected: |
| 129 | int overflow(int c); |
| 130 | std::ostream &strDigest(std::ostream &os); |
| 131 | |
| 132 | public: |
| 133 | ChecksumDigest(); |
| 134 | |
| 135 | void initDigest(void) |
| 136 | {csum = 0;}; |
| 137 | |
| 138 | unsigned getSize(void) |
| 139 | {return 1;}; |
| 140 | |
| 141 | unsigned getDigest(unsigned char *buffer); |
| 142 | |
| 143 | void putDigest(const unsigned char *buffer, unsigned length); |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * A crc16 collection/compution hash accumulator class. |
| 148 | * |
| 149 | * @author David Sugar <dyfet@ostel.com> |
| 150 | * @short crc16 computation hash. |
| 151 | */ |
| 152 | class __EXPORT CRC16Digest : public Digest |
| 153 | { |
| 154 | private: |
| 155 | uint16 crc16; |
| 156 | |
| 157 | protected: |
| 158 | int overflow(int c); |
| 159 | |
| 160 | std::ostream &strDigest(std::ostream &os); |
| 161 | |
| 162 | public: |
| 163 | CRC16Digest(); |
| 164 | |
| 165 | CRC16Digest ( const CRC16Digest &crc ); |
| 166 | |
| 167 | virtual ~CRC16Digest() {}; |
| 168 | |
| 169 | inline void initDigest(uint16 crc) {crc16 = crc;}; |
| 170 | |
| 171 | void initDigest(void) {initDigest(0);}; |
| 172 | |
| 173 | inline unsigned getSize ( void ) |
| 174 | {return sizeof(crc16);}; |
| 175 | |
| 176 | CRC16Digest& operator= ( const CRC16Digest &right ); |
| 177 | |
| 178 | operator const uint16() const |
| 179 | {return crc16;}; |
| 180 | |
| 181 | inline uint16 getDigest(void) |
| 182 | {return crc16;}; |
| 183 | |
| 184 | unsigned getDigest ( unsigned char *buffer ); |
| 185 | |
| 186 | void putDigest ( const unsigned char *buffer, unsigned length ); |
| 187 | |
| 188 | }; |
| 189 | |
| 190 | /** |
| 191 | * A crc32 collection/computation hash accumulator class. |
| 192 | * |
| 193 | * @author Kevin Kraatz <kraatz@arlut.utexas.edu> |
| 194 | * @short crc32 computation hash. |
| 195 | */ |
| 196 | |
| 197 | class __EXPORT CRC32Digest : public Digest |
| 198 | { |
| 199 | private: |
| 200 | uint32 crc_table[256]; |
| 201 | uint32 crc_reg; |
| 202 | uint32 crc32; |
| 203 | |
| 204 | protected: |
| 205 | unsigned char overflow(unsigned char octet); |
| 206 | |
| 207 | std::ostream &strDigest(std::ostream &os); |
| 208 | |
| 209 | public: |
| 210 | CRC32Digest(); |
| 211 | CRC32Digest(const CRC32Digest &crc); |
| 212 | |
| 213 | void initDigest(void); |
| 214 | |
| 215 | inline unsigned getSize(void) {return sizeof(crc32);} |
| 216 | |
| 217 | operator const uint32() const |
| 218 | {return crc32;}; |
| 219 | |
| 220 | inline uint32 getDigest(void) |
| 221 | {return crc32;}; |
| 222 | |
| 223 | unsigned getDigest(unsigned char *buffer); |
| 224 | |
| 225 | void putDigest(const unsigned char *buffer, unsigned length); |
| 226 | |
| 227 | CRC32Digest& operator= (const CRC32Digest &right); |
| 228 | }; |
| 229 | |
| 230 | /** |
| 231 | * A md5 collection/computation accululator class. |
| 232 | * |
| 233 | * @author David Sugar <dyfet@ostel.com> |
| 234 | * @short md5 hash accumulation. |
| 235 | */ |
| 236 | class __EXPORT MD5Digest : public Digest |
| 237 | { |
| 238 | private: |
| 239 | unsigned long state[4]; |
| 240 | unsigned long count[2]; |
| 241 | unsigned char buf[64]; |
| 242 | unsigned bpos; |
| 243 | unsigned char md5[16]; |
| 244 | bool updated; |
| 245 | |
| 246 | protected: |
| 247 | int overflow(int c); |
| 248 | |
| 249 | void update(void); |
| 250 | |
| 251 | void commit(void); |
| 252 | |
| 253 | std::ostream &strDigest(std::ostream &os); |
| 254 | |
| 255 | public: |
| 256 | MD5Digest(); |
| 257 | |
| 258 | void initDigest(void); |
| 259 | |
| 260 | inline unsigned getSize(void) |
| 261 | {return 16;}; |
| 262 | |
| 263 | unsigned getDigest(unsigned char *buffer); |
| 264 | |
| 265 | void putDigest(const unsigned char *buffer, unsigned len); |
| 266 | }; |
| 267 | |
| 268 | #ifdef COMMON_STD_EXCEPTION |
| 269 | /** |
| 270 | * DigestException |
| 271 | * |
| 272 | * Exception classes that pertain to errors when making or otherwise |
| 273 | * working with digests. |
| 274 | * |
| 275 | * @author Elizabeth Barham <lizzy@soggytrousers.net> |
| 276 | * @short Exceptions involving digests. |
| 277 | */ |
| 278 | class __EXPORT DigestException : public Exception { |
| 279 | public: |
| 280 | DigestException(const String &str) : Exception(str) {}; |
| 281 | }; |
| 282 | #endif |
| 283 | |
| 284 | #ifdef CCXX_NAMESPACES |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | #endif |
| 289 | /** EMACS ** |
| 290 | * Local variables: |
| 291 | * mode: c++ |
| 292 | * c-basic-offset: 4 |
| 293 | * End: |
| 294 | */ |
| 295 | |