Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

md5.h

Go to the documentation of this file.
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 <fstream>
00066 #include <qstring.h>
00067 
00068 #ifndef BACKEND_MD5_H
00069 #define BACKEND_MD5_H
00070 
00071 class MD5 {
00072 
00073 public:
00074 // methods for controlled operation:
00075   MD5              ();  // simple initializer
00076   void  update     (unsigned char *input, unsigned int input_length);
00077   void  update     (std::istream& stream);
00078   void  update     (FILE *file);
00079   void  update     (std::ifstream& stream);
00080   void  finalize   ();
00081 
00082 // constructors for special circumstances.  All these constructors finalize
00083 // the MD5 context.
00084   MD5              (unsigned char *string); // digest string, finalize
00085   MD5              (std::istream& stream);       // digest stream, finalize
00086   MD5              (FILE *file);            // digest file, close, finalize
00087   MD5              (std::ifstream& stream);      // digest stream, close, finalize
00088 
00089 // methods to acquire finalized result
00090   unsigned char    *raw_digest ();  // digest as a 16-byte binary array
00091   QString            hex_digest ();  // digest as a 33-byte ascii-hex string
00092 
00093 
00094 private:
00095 
00096 // first, some types:
00097   typedef unsigned       int uint4; // assumes integer is 4 words long
00098   typedef unsigned short int uint2; // assumes short integer is 2 words long
00099   typedef unsigned      char uint1; // assumes char is 1 word long
00100 
00101 // next, the private data:
00102   uint4 state[4];
00103   uint4 count[2];     // number of *bits*, mod 2^64
00104   uint1 buffer[64];   // input buffer
00105   uint1 digest[16];
00106   uint1 finalized;
00107 
00108 // last, the private methods, mostly static:
00109   void init             ();               // called by all constructors
00110   void transform        (uint1 *buffer);  // does the real update work.  Note 
00111                                           // that length is implied to be 64.
00112 
00113   static void encode    (uint1 *dest, uint4 *src, uint4 length);
00114   static void decode    (uint4 *dest, uint1 *src, uint4 length);
00115   static void memcpy    (uint1 *dest, uint1 *src, uint4 length);
00116   static void memset    (uint1 *start, uint1 val, uint4 length);
00117 
00118   static inline uint4  rotate_left (uint4 x, uint4 n);
00119   static inline uint4  F           (uint4 x, uint4 y, uint4 z);
00120   static inline uint4  G           (uint4 x, uint4 y, uint4 z);
00121   static inline uint4  H           (uint4 x, uint4 y, uint4 z);
00122   static inline uint4  I           (uint4 x, uint4 y, uint4 z);
00123   static inline void   FF  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00124                             uint4 s, uint4 ac);
00125   static inline void   GG  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00126                             uint4 s, uint4 ac);
00127   static inline void   HH  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00128                             uint4 s, uint4 ac);
00129   static inline void   II  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00130                             uint4 s, uint4 ac);
00131 
00132 };
00133 
00134 //returns md5 for a given file
00135 QString getMD5(std::ifstream& stream);
00136 
00137 //compares md5's for two files
00138 //returns -1 if unable ot open file
00139 //returns 0 if files are different
00140 //returns 1 if files are same
00141 bool filesMatch(std::ifstream& stream, QString oldMD5);
00142 
00143 #endif //BACKEND_MD5_H

Generated on Thu Nov 13 00:10:54 2003 for AlbumShaper by doxygen 1.3.4