Code

change API: separate functions creating a blur filter, one for a given item, another...
[inkscape.git] / src / dom / util / digest.h
1 #ifndef __DIGEST_H__\r
2 #define __DIGEST_H__\r
3 /**\r
4  * Secure Hashing Tool\r
5  * *\r
6  * Authors:\r
7  *   Bob Jamison\r
8  *\r
9  * Copyright (C) 2006 Bob Jamison\r
10  *\r
11  *  This library is free software; you can redistribute it and/or\r
12  *  modify it under the terms of the GNU Lesser General Public\r
13  *  License as published by the Free Software Foundation; either\r
14  *  version 2.1 of the License, or (at your option) any later version.\r
15  *\r
16  *  This library is distributed in the hope that it will be useful,\r
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
19  *  Lesser General Public License for more details.\r
20  *\r
21  *  You should have received a copy of the GNU Lesser General Public\r
22  *  License along with this library; if not, write to the Free Software\r
23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
24  */\r
25 \r
26 /**\r
27  *\r
28  *  This base class and its subclasses  provide an easy API for providing\r
29  *  several different types of secure hashing functions for whatever use\r
30  *  a developer might need.  This is not intended as a high-performance\r
31  *  replacement for the fine implementations already available.  Rather, it\r
32  *  is a small and simple (and maybe a bit slow?) tool for moderate common\r
33  *  hashing requirements, like for communications and authentication.\r
34  *  \r
35  *  These hashes are intended to be simple to use.  For example:\r
36  *  Sha256Digest digest;\r
37  *  digest.append("The quick brown dog");\r
38  *  std::string result = digest.finishHex();\r
39  *  \r
40  *  There are several forms of append() for convenience.\r
41  *  finish() and finishHex() call reset() for both security and\r
42  *  to prepare for the next use.\r
43  *          \r
44  */\r
45 \r
46 #include <vector>\r
47 #include <string>\r
48 \r
49 \r
50 /**\r
51  *  Base class.  Do not use this class directly.  Rather, use of of the\r
52  *  subclasses below.   \r
53  *  For all subclasses, overload reset(), update(unsigned char), and finish()\r
54  */\r
55 class Digest\r
56 {\r
57 public:\r
58 \r
59     /**\r
60      *  Different types of hash algorithms.\r
61      */\r
62     typedef enum\r
63         {\r
64         HASH_NONE,\r
65         HASH_SHA1,\r
66         HASH_SHA224,\r
67         HASH_SHA256,\r
68         HASH_SHA384,\r
69         HASH_SHA512,\r
70         HASH_MD5\r
71         } HashType;\r
72         \r
73     /**\r
74      *  Constructor, with no type\r
75      */\r
76     Digest() : hashType(HASH_NONE)\r
77         { reset(); }\r
78 \r
79     /**\r
80      *  Destructor\r
81      */\r
82     virtual ~Digest()\r
83         { reset(); }\r
84 \r
85     /**\r
86      *  Return one of the enumerated hash types above\r
87      */\r
88     virtual int getType()\r
89         { return hashType; }\r
90 \r
91     /**\r
92      *  Append a single byte to the hash\r
93      */\r
94     void append(unsigned char ch)\r
95         { update(ch); }\r
96 \r
97     /**\r
98      *  Append a string to the hash\r
99      */\r
100     virtual void append(const std::string &str)\r
101         {\r
102         for (unsigned int i=0 ; i<str.size() ; i++)\r
103             update((unsigned char)str[i]);\r
104         }\r
105 \r
106     /**\r
107      *  Append a byte buffer to the hash\r
108      */\r
109     virtual void append(unsigned char *buf, int len)\r
110         {\r
111         for (int i=0 ; i<len ; i++)\r
112             update(buf[i]);\r
113         }\r
114 \r
115     /**\r
116      *  Append a byte vector to the hash\r
117      */\r
118     virtual void append(const std::vector<unsigned char> buf)\r
119         {\r
120         for (unsigned int i=0 ; i<buf.size() ; i++)\r
121             update(buf[i]);\r
122         }\r
123 \r
124     /**\r
125      *  Finish the hash and return a hexidecimal version of the computed\r
126      *  value     \r
127      */\r
128     virtual std::string finishHex();\r
129 \r
130     /**\r
131      *  Initialize the fields of this hash engine to its starting values.    \r
132      *  Overload this in every subclass\r
133      */\r
134     virtual void reset()\r
135         {}\r
136 \r
137     /**\r
138      *  Finish the hash and return its computed value    \r
139      *  Overload this in every subclass\r
140      */\r
141     virtual std::vector<unsigned char> finish()\r
142         {\r
143         std::vector<unsigned char> ret;\r
144         return ret;\r
145         }\r
146 \r
147 protected:\r
148 \r
149     /**\r
150      *  Update the hash with a given byte    \r
151      *  Overload this in every subclass\r
152      */\r
153     virtual void update(unsigned char ch)\r
154         {}\r
155 \r
156     /**\r
157      * The enumerated type of the hash\r
158      */      \r
159     int hashType;\r
160 };\r
161 \r
162 \r
163 \r
164 \r
165 \r
166 /**\r
167  *  SHA-1,  \r
168  *  Section 6.1, SECURE HASH STANDARD\r
169  *  Federal Information Processing Standards Publication 180-2\r
170  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
171  */\r
172 class Sha1Digest : public Digest\r
173 {\r
174 public:\r
175 \r
176     /**\r
177      *  Constructor\r
178      */\r
179     Sha1Digest()\r
180         { hashType = HASH_SHA1; reset(); }\r
181 \r
182     /**\r
183      *  Destructor\r
184      */\r
185     virtual ~Sha1Digest()\r
186         { reset(); }\r
187 \r
188     /**\r
189      *  Overloaded from Digest\r
190      */\r
191     virtual void reset();\r
192 \r
193     /**\r
194      *  Overloaded from Digest\r
195      */\r
196     virtual std::vector<unsigned char> finish();\r
197 \r
198 protected:\r
199 \r
200     /**\r
201      *  Overloaded from Digest\r
202      */\r
203     virtual void update(unsigned char val);\r
204 \r
205 private:\r
206 \r
207     void hashblock();\r
208 \r
209     unsigned long H[5];\r
210     unsigned long W[80];\r
211     unsigned long long size;\r
212     int lenW;\r
213 \r
214 };\r
215 \r
216 \r
217 \r
218 \r
219 \r
220 \r
221 /**\r
222  *  SHA-224,  \r
223  *  Section 6.1, SECURE HASH STANDARD\r
224  *  Federal Information Processing Standards Publication 180-2\r
225  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
226  */\r
227 class Sha224Digest : public Digest\r
228 {\r
229 public:\r
230 \r
231     /**\r
232      *  Constructor\r
233      */\r
234     Sha224Digest()\r
235         { hashType = HASH_SHA224; reset(); }\r
236 \r
237     /**\r
238      *  Destructor\r
239      */\r
240     virtual ~Sha224Digest()\r
241         { reset(); }\r
242 \r
243     /**\r
244      *  Overloaded from Digest\r
245      */\r
246     virtual void reset();\r
247 \r
248     /**\r
249      *  Overloaded from Digest\r
250      */\r
251     virtual std::vector<unsigned char> finish();\r
252 \r
253 protected:\r
254 \r
255     /**\r
256      *  Overloaded from Digest\r
257      */\r
258     virtual void update(unsigned char val);\r
259 \r
260 private:\r
261 \r
262     void hashblock();\r
263 \r
264     unsigned long H[8];\r
265     unsigned long W[64];\r
266     unsigned long long size;\r
267     int lenW;\r
268 \r
269 };\r
270 \r
271 \r
272 \r
273 /**\r
274  *  SHA-256,  \r
275  *  Section 6.1, SECURE HASH STANDARD\r
276  *  Federal Information Processing Standards Publication 180-2\r
277  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
278  */\r
279 class Sha256Digest : public Digest\r
280 {\r
281 public:\r
282 \r
283     /**\r
284      *  Constructor\r
285      */\r
286     Sha256Digest()\r
287         { hashType = HASH_SHA256; reset(); }\r
288 \r
289     /**\r
290      *  Destructor\r
291      */\r
292     virtual ~Sha256Digest()\r
293         { reset(); }\r
294 \r
295     /**\r
296      *  Overloaded from Digest\r
297      */\r
298     virtual void reset();\r
299 \r
300     /**\r
301      *  Overloaded from Digest\r
302      */\r
303     virtual std::vector<unsigned char> finish();\r
304 \r
305 protected:\r
306 \r
307     /**\r
308      *  Overloaded from Digest\r
309      */\r
310     virtual void update(unsigned char val);\r
311 \r
312 private:\r
313 \r
314     void hashblock();\r
315 \r
316     unsigned long H[8];\r
317     unsigned long W[64];\r
318     unsigned long long size;\r
319     int lenW;\r
320 \r
321 };\r
322 \r
323 \r
324 /**\r
325  *  SHA-384,\r
326  *  Section 6.1, SECURE HASH STANDARD\r
327  *  Federal Information Processing Standards Publication 180-2\r
328  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
329  */\r
330 class Sha384Digest : public Digest\r
331 {\r
332 public:\r
333 \r
334     /**\r
335      *  Constructor\r
336      */\r
337     Sha384Digest()\r
338         { hashType = HASH_SHA384; reset(); }\r
339 \r
340     /**\r
341      *  Destructor\r
342      */\r
343     virtual ~Sha384Digest()\r
344         { reset(); }\r
345 \r
346     /**\r
347      *  Overloaded from Digest\r
348      */\r
349     virtual void reset();\r
350 \r
351     /**\r
352      *  Overloaded from Digest\r
353      */\r
354     virtual std::vector<unsigned char> finish();\r
355 \r
356 protected:\r
357 \r
358     /**\r
359      *  Overloaded from Digest\r
360      */\r
361     virtual void update(unsigned char val);\r
362 \r
363 private:\r
364 \r
365     void hashblock();\r
366 \r
367     unsigned long long H[8];\r
368     unsigned long long W[80];\r
369     unsigned long long size;\r
370     int lenW;\r
371 \r
372 };\r
373 \r
374 \r
375 \r
376 \r
377 /**\r
378  *  SHA-512,  \r
379  *  Section 6.1, SECURE HASH STANDARD\r
380  *  Federal Information Processing Standards Publication 180-2\r
381  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
382  */\r
383 class Sha512Digest : public Digest\r
384 {\r
385 public:\r
386 \r
387     /**\r
388      *  Constructor\r
389      */\r
390     Sha512Digest()\r
391         { hashType = HASH_SHA512; reset(); }\r
392 \r
393     /**\r
394      *  Destructor\r
395      */\r
396     virtual ~Sha512Digest()\r
397         { reset(); }\r
398 \r
399     /**\r
400      *  Overloaded from Digest\r
401      */\r
402     virtual void reset();\r
403 \r
404     /**\r
405      *  Overloaded from Digest\r
406      */\r
407     virtual std::vector<unsigned char> finish();\r
408 \r
409 protected:\r
410 \r
411     /**\r
412      *  Overloaded from Digest\r
413      */\r
414     virtual void update(unsigned char val);\r
415 \r
416 private:\r
417 \r
418     void hashblock();\r
419 \r
420     unsigned long long H[8];\r
421     unsigned long long W[80];\r
422     unsigned long long size;\r
423     int lenW;\r
424 \r
425 };\r
426 \r
427 \r
428 \r
429 \r
430 \r
431 \r
432 \r
433 \r
434 \r
435 /**\r
436  * IETF RFC 1321, MD5 Specification\r
437  * http://www.ietf.org/rfc/rfc1321.txt \r
438  */\r
439 class Md5Digest :  public Digest\r
440 {\r
441 public:\r
442 \r
443     /**\r
444      *  Constructor\r
445      */\r
446     Md5Digest()\r
447         { hashType = HASH_MD5; reset(); }\r
448 \r
449     /**\r
450      *  Destructor\r
451      */\r
452     virtual ~Md5Digest()\r
453         { reset(); }\r
454 \r
455     /**\r
456      *  Overloaded from Digest\r
457      */\r
458     virtual void reset();\r
459 \r
460     /**\r
461      *  Overloaded from Digest\r
462      */\r
463     virtual std::vector<unsigned char> finish();\r
464 \r
465 protected:\r
466 \r
467     /**\r
468      *  Overloaded from Digest\r
469      */\r
470     virtual void update(unsigned char val);\r
471 \r
472 private:\r
473 \r
474     void hashblock();\r
475 \r
476     unsigned long hash[8];\r
477     unsigned long W[64];\r
478     unsigned long long size;\r
479     int lenW;\r
480 \r
481 };\r
482 \r
483 \r
484 \r
485 \r
486 \r
487 \r
488 \r
489 \r
490 \r
491 #endif /*  __DIGEST_H__ */\r
492 \r
493 \r