00001 //============================================== 00002 // copyright : (C) 2003 by Will Stokes 00003 //============================================== 00004 // This program is free software; you can redistribute it 00005 // and/or modify it under the terms of the GNU General 00006 // Public License as published by the Free Software 00007 // Foundation; either version 2 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // As a special exception, Will Stokes gives permission to 00011 // link this program with Qt non-commercial edition, and 00012 // distribute the resulting executable, without including the 00013 // source code for the Qt non-commercial edition in the 00014 // source distribution. 00015 //============================================== 00016 00017 //===================================== 00021 //===================================== 00022 00023 00024 // MD5.CC - source code for the C++/object oriented translation and 00025 // modification of MD5. 00026 00027 // Translation and modification (c) 1995 by Mordechai T. Abzug 00028 00029 // This translation/ modification is provided "as is," without express or 00030 // implied warranty of any kind. 00031 00032 // The translator/ modifier does not claim (1) that MD5 will do what you think 00033 // it does; (2) that this translation/ modification is accurate; or (3) that 00034 // this software is "merchantible." (Language for this disclaimer partially 00035 // copied from the disclaimer below). 00036 00037 /* based on: 00038 00039 MD5.H - header file for MD5C.C 00040 MDDRIVER.C - test driver for MD2, MD4 and MD5 00041 00042 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 00043 rights reserved. 00044 00045 License to copy and use this software is granted provided that it 00046 is identified as the "RSA Data Security, Inc. MD5 Message-Digest 00047 Algorithm" in all material mentioning or referencing this software 00048 or this function. 00049 00050 License is also granted to make and use derivative works provided 00051 that such works are identified as "derived from the RSA Data 00052 Security, Inc. MD5 Message-Digest Algorithm" in all material 00053 mentioning or referencing the derived work. 00054 00055 RSA Data Security, Inc. makes no representations concerning either 00056 the merchantability of this software or the suitability of this 00057 software for any particular purpose. It is provided "as is" 00058 without express or implied warranty of any kind. 00059 00060 These notices must be retained in any copies of any part of this 00061 documentation and/or software. 00062 00063 */ 00064 00065 #include <stdio.h> 00066 #include <fstream.h> 00067 #include <iostream.h> 00068 #include <qstring.h> 00069 00070 #ifndef BACKEND_MD5_H 00071 #define BACKEND_MD5_H 00072 00073 class MD5 { 00074 00075 public: 00076 // methods for controlled operation: 00077 MD5 (); // simple initializer 00078 void update (unsigned char *input, unsigned int input_length); 00079 void update (istream& stream); 00080 void update (FILE *file); 00081 void update (ifstream& stream); 00082 void finalize (); 00083 00084 // constructors for special circumstances. All these constructors finalize 00085 // the MD5 context. 00086 MD5 (unsigned char *string); // digest string, finalize 00087 MD5 (istream& stream); // digest stream, finalize 00088 MD5 (FILE *file); // digest file, close, finalize 00089 MD5 (ifstream& stream); // digest stream, close, finalize 00090 00091 // methods to acquire finalized result 00092 unsigned char *raw_digest (); // digest as a 16-byte binary array 00093 QString hex_digest (); // digest as a 33-byte ascii-hex string 00094 00095 00096 private: 00097 00098 // first, some types: 00099 typedef unsigned int uint4; // assumes integer is 4 words long 00100 typedef unsigned short int uint2; // assumes short integer is 2 words long 00101 typedef unsigned char uint1; // assumes char is 1 word long 00102 00103 // next, the private data: 00104 uint4 state[4]; 00105 uint4 count[2]; // number of *bits*, mod 2^64 00106 uint1 buffer[64]; // input buffer 00107 uint1 digest[16]; 00108 uint1 finalized; 00109 00110 // last, the private methods, mostly static: 00111 void init (); // called by all constructors 00112 void transform (uint1 *buffer); // does the real update work. Note 00113 // that length is implied to be 64. 00114 00115 static void encode (uint1 *dest, uint4 *src, uint4 length); 00116 static void decode (uint4 *dest, uint1 *src, uint4 length); 00117 static void memcpy (uint1 *dest, uint1 *src, uint4 length); 00118 static void memset (uint1 *start, uint1 val, uint4 length); 00119 00120 static inline uint4 rotate_left (uint4 x, uint4 n); 00121 static inline uint4 F (uint4 x, uint4 y, uint4 z); 00122 static inline uint4 G (uint4 x, uint4 y, uint4 z); 00123 static inline uint4 H (uint4 x, uint4 y, uint4 z); 00124 static inline uint4 I (uint4 x, uint4 y, uint4 z); 00125 static inline void FF (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 00126 uint4 s, uint4 ac); 00127 static inline void GG (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 00128 uint4 s, uint4 ac); 00129 static inline void HH (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 00130 uint4 s, uint4 ac); 00131 static inline void II (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 00132 uint4 s, uint4 ac); 00133 00134 }; 00135 00136 //returns md5 for a given file 00137 QString getMD5(ifstream& stream); 00138 00139 //compares md5's for two files 00140 //returns -1 if unable ot open file 00141 //returns 0 if files are different 00142 //returns 1 if files are same 00143 bool filesMatch(ifstream& stream, QString oldMD5); 00144 00145 #endif //BACKEND_MD5_H