GNU CommonC++
digest.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_DIGEST_H_
44 #define CCXX_DIGEST_H_
45 
46 #ifndef CCXX_MISSING_H_
47 #include <cc++/missing.h>
48 #endif
49 
50 #ifndef CCXX_THREAD_H_
51 #include <cc++/thread.h>
52 #endif
53 
54 #ifndef CCXX_EXCEPTION_H_
55 #include <cc++/exception.h>
56 #endif
57 
58 #ifdef CCXX_NAMESPACES
59 namespace ost {
60 #endif
61 
69 class __EXPORT Digest : protected std::streambuf, public std::ostream
70 {
71 protected:
72  Digest();
73 
79  virtual unsigned getSize(void) = 0;
80 
87  virtual unsigned getDigest(unsigned char *buffer) = 0;
88 
95  virtual void putDigest(const unsigned char *buffer, unsigned length) = 0;
96 
102  virtual std::ostream &strDigest(std::ostream &os) = 0;
103 
104  friend std::ostream &operator<<(std::ostream &os, Digest &ia)
105  {return ia.strDigest(os);};
106 
107 public:
111  virtual void initDigest(void) = 0;
112 
113  virtual ~Digest();
114 };
115 
123 {
124 private:
125  unsigned char csum;
126 
127 protected:
128  int overflow(int c);
129  std::ostream &strDigest(std::ostream &os);
130 
131 public:
132  ChecksumDigest();
133 
134  void initDigest(void)
135  {csum = 0;};
136 
137  unsigned getSize(void)
138  {return 1;};
139 
140  unsigned getDigest(unsigned char *buffer);
141 
142  void putDigest(const unsigned char *buffer, unsigned length);
143 };
144 
152 {
153 private:
154  unsigned short crc16;
155 
156 protected:
157  int overflow(int c);
158 
159  std::ostream &strDigest(std::ostream &os);
160 
161 public:
162  CRC16Digest();
163 
164  inline void initDigest(void)
165  {crc16 = 0;};
166 
167  inline unsigned getSize(void)
168  {return 2;};
169 
170  unsigned getDigest(unsigned char *buffer);
171 
172  void putDigest(const unsigned char *buffer, unsigned length);
173 };
174 
183 {
184 private:
185  unsigned long crc_table[256];
186  unsigned long crc_reg;
187  unsigned long crc32;
188 
189 protected:
190  unsigned char overflow(unsigned char octet);
191 
192  std::ostream &strDigest(std::ostream &os);
193 
194 public:
195  CRC32Digest();
196 
197  void initDigest(void);
198 
199  inline unsigned getSize(void) {return 4;}
200 
201  unsigned getDigest(unsigned char *buffer);
202 
203  void putDigest(const unsigned char *buffer, unsigned length);
204 };
205 
212 class __EXPORT MD5Digest : public Digest
213 {
214 private:
215  unsigned long state[4];
216  unsigned long count[2];
217  unsigned char buf[64];
218  unsigned bpos;
219  unsigned char md5[16];
220  bool updated;
221 
222 protected:
223  int overflow(int c);
224 
225  void update(void);
226 
227  void commit(void);
228 
229  std::ostream &strDigest(std::ostream &os);
230 
231 public:
232  MD5Digest();
233 
234  void initDigest(void);
235 
236  inline unsigned getSize(void)
237  {return 16;};
238 
239  unsigned getDigest(unsigned char *buffer);
240 
241  void putDigest(const unsigned char *buffer, unsigned len);
242 };
243 
244 #ifdef COMMON_STD_EXCEPTION
245 
254 class __EXPORT DigestException : public Exception {
255 public:
256  DigestException(const String &str) : Exception(str) {};
257 };
258 #endif
259 
260 #ifdef CCXX_NAMESPACES
261 }
262 #endif
263 
264 #endif
265