]> git.jsancho.org Git - lugaru.git/blob - Source/md5.h
Added GPL license and headers.
[lugaru.git] / Source / md5.h
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 // MD5.CC - source code for the C++/object oriented translation and 
23 //          modification of MD5.
24
25 // Translation and modification (c) 1995 by Mordechai T. Abzug 
26
27 // This translation/ modification is provided "as is," without express or 
28 // implied warranty of any kind.
29
30 // The translator/ modifier does not claim (1) that MD5 will do what you think 
31 // it does; (2) that this translation/ modification is accurate; or (3) that 
32 // this software is "merchantible."  (Language for this disclaimer partially 
33 // copied from the disclaimer below).
34
35 /* based on:
36
37    MD5.H - header file for MD5C.C
38    MDDRIVER.C - test driver for MD2, MD4 and MD5
39
40    Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
41 rights reserved.
42
43 License to copy and use this software is granted provided that it
44 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
45 Algorithm" in all material mentioning or referencing this software
46 or this function.
47
48 License is also granted to make and use derivative works provided
49 that such works are identified as "derived from the RSA Data
50 Security, Inc. MD5 Message-Digest Algorithm" in all material
51 mentioning or referencing the derived work.
52
53 RSA Data Security, Inc. makes no representations concerning either
54 the merchantability of this software or the suitability of this
55 software for any particular purpose. It is provided "as is"
56 without express or implied warranty of any kind.
57
58 These notices must be retained in any copies of any part of this
59 documentation and/or software.
60
61 */
62
63 #include <stdio.h>
64 #include <fstream>
65 #include <iostream>
66
67 using namespace std;
68 class MD5 {
69
70 public:
71 // methods for controlled operation:
72   MD5              ();  // simple initializer
73   void  update     (unsigned char *input, unsigned int input_length);
74   void  update     (istream& stream);
75   void  update     (FILE *file);
76   void  update     (ifstream& stream);
77   void  finalize   ();
78
79 // constructors for special circumstances.  All these constructors finalize
80 // the MD5 context.
81   MD5              (unsigned char *string); // digest string, finalize
82   MD5              (istream& stream);       // digest stream, finalize
83   MD5              (FILE *file);            // digest file, close, finalize
84   MD5              (ifstream& stream);      // digest stream, close, finalize
85
86 // methods to acquire finalized result
87   unsigned char    *raw_digest ();  // digest as a 16-byte binary array
88   char *            hex_digest ();  // digest as a 33-byte ascii-hex string
89   friend ostream&   operator<< (ostream&, MD5 context);
90
91
92
93 private:
94
95 // first, some types:
96   typedef unsigned       int uint4; // assumes integer is 4 words long
97   typedef unsigned short int uint2; // assumes short integer is 2 words long
98   typedef unsigned      char uint1; // assumes char is 1 word long
99
100 // next, the private data:
101   uint4 state[4];
102   uint4 count[2];     // number of *bits*, mod 2^64
103   uint1 buffer[64];   // input buffer
104   uint1 digest[16];
105   uint1 finalized;
106
107 // last, the private methods, mostly static:
108   void init             ();               // called by all constructors
109   void transform        (uint1 *buffer);  // does the real update work.  Note 
110                                           // that length is implied to be 64.
111
112   static void encode    (uint1 *dest, uint4 *src, uint4 length);
113   static void decode    (uint4 *dest, uint1 *src, uint4 length);
114   static void memcpy    (uint1 *dest, uint1 *src, uint4 length);
115   static void memset    (uint1 *start, uint1 val, uint4 length);
116
117   static inline uint4  rotate_left (uint4 x, uint4 n);
118   static inline uint4  F           (uint4 x, uint4 y, uint4 z);
119   static inline uint4  G           (uint4 x, uint4 y, uint4 z);
120   static inline uint4  H           (uint4 x, uint4 y, uint4 z);
121   static inline uint4  I           (uint4 x, uint4 y, uint4 z);
122   static inline void   FF  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
123                             uint4 s, uint4 ac);
124   static inline void   GG  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
125                             uint4 s, uint4 ac);
126   static inline void   HH  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
127                             uint4 s, uint4 ac);
128   static inline void   II  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
129                             uint4 s, uint4 ac);
130
131 };