00001 /************************************************************************** 00002 *** 00003 *** Copyright (c) 1995-2000 Regents of the University of California, 00004 *** Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov 00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan, 00006 *** Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov 00007 *** 00008 *** Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu 00009 *** Original Affiliation: UCLA, Computer Science Department, 00010 *** Los Angeles, CA 90095-1596 USA 00011 *** 00012 *** Permission is hereby granted, free of charge, to any person obtaining 00013 *** a copy of this software and associated documentation files (the 00014 *** "Software"), to deal in the Software without restriction, including 00015 *** without limitation 00016 *** the rights to use, copy, modify, merge, publish, distribute, sublicense, 00017 *** and/or sell copies of the Software, and to permit persons to whom the 00018 *** Software is furnished to do so, subject to the following conditions: 00019 *** 00020 *** The above copyright notice and this permission notice shall be included 00021 *** in all copies or substantial portions of the Software. 00022 *** 00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00030 *** 00031 *** 00032 ***************************************************************************/ 00033 00034 00035 00036 00037 00038 00039 00040 /*Copyright notice: 00041 The following software is derived from the RSA Data 00042 Security, Inc. MD5 Message-Digest Algorithm. Accordingly, 00043 the following copyright notice is reproduced, and applies to 00044 such parts of this file which are copied from RSA's source code. 00045 All other parts of the file are copyright 1998 ABKGroup and 00046 the Regents of the University of California. 00047 */ 00048 00049 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 00050 rights reserved. 00051 00052 License to copy and use this software is granted provided that it 00053 is identified as the "RSA Data Security, Inc. MD5 Message-Digest 00054 Algorithm" in all material mentioning or referencing this software 00055 or this function. 00056 00057 License is also granted to make and use derivative works provided 00058 that such works are identified as "derived from the RSA Data 00059 Security, Inc. MD5 Message-Digest Algorithm" in all material 00060 mentioning or referencing the derived work. 00061 00062 RSA Data Security, Inc. makes no representations concerning either 00063 the merchantability of this software or the suitability of this 00064 software for any particular purpose. It is provided "as is" 00065 without express or implied warranty of any kind. 00066 00067 These notices must be retained in any copies of any part of this 00068 documentation and/or software. 00069 */ 00070 00071 00072 #ifndef _MD5_H 00073 #define _MD5_H 00074 00075 #include <vector> 00076 #include "abkcommon.h" 00077 00078 //: Message-Digest Algorithm 00079 class MD5 00080 { 00081 friend ostream& operator<<(ostream& os, const MD5& md5); 00082 public: 00083 class Wrap128 00084 { 00085 public: 00086 unsigned meat[4]; 00087 unsigned getWord(unsigned idx) const {return meat[idx];} 00088 unsigned operator[](unsigned idx) const {return meat[idx];} 00089 }; 00090 public: 00091 MD5(); 00092 MD5(const char *input, unsigned length, bool lastUpdate=true); 00093 00094 MD5(const char *input, bool lastUpdate=true); 00095 //here input should be null-terminated 00096 MD5(const MD5&){abkfatal(0,"can't copy-ctruct MD5");} 00097 00098 00099 ~MD5(); 00100 00101 void update(const char *input,unsigned inputLen, bool lastUpdate); 00102 void update(const char *input, bool lastUpdate); 00103 //here input should be null-terminated 00104 operator unsigned() const; 00105 // returns word 0 of state 00106 /*operator DWORD();*/ 00107 operator Wrap128() const; 00108 00109 MD5 &operator=(const MD5&){abkfatal(0,"can't assign MD5"); 00110 return *this;} 00111 unsigned getWord(unsigned idx) const; //idx goes from 0 to 3 00112 00113 void finalize(); 00114 00115 public: 00116 static void encode (unsigned char *output, 00117 const unsigned *input, unsigned len); 00118 // helper functions that there's no need to protect 00119 // (not that they're likely to be of much use) 00120 00121 static void decode (unsigned *output, 00122 const unsigned char *input, unsigned len); 00123 00124 protected: 00125 void _update(const unsigned char *input,unsigned inputLen); 00126 //helper functions that change things 00127 void _initState(); 00128 void _finalize(); 00129 void _transform(const unsigned char *block); 00130 protected: 00131 unsigned _state[4]; 00132 //internal data 00133 unsigned _count[2]; 00134 // number of bits, modulo 2\^64 (lsb first) 00135 unsigned char _buffer[64]; 00136 bool _isFinalized; 00137 00138 }; 00139 00140 ostream& operator<<(ostream& os, const MD5& md5); 00141 00142 #endif