md5.h

Go to the documentation of this file.
00001 // This file is part of par2cmdline (a PAR 2.0 compatible file verification and
00002 // repair tool). See https://parchive.sourceforge.net for details of PAR 2.0.
00003 //
00004 // Copyright (c) 2003 Peter Brian Clements
00005 //
00006 // par2cmdline is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // par2cmdline is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019 
00020 #ifndef __MD5_H__
00021 #define __MD5_H__
00022 
00023 // This file defines the MD5Hash and MD5Context objects which are used
00024 // to compute and manipulate the MD5 Hash values for a block of data.
00025 
00026 // Usage:
00027 //
00028 // MD5Context context;
00029 // context.Update(buffer, length);
00030 //
00031 // MD5Hash hash;
00032 // context.Final(hash);
00033 
00034 
00035 
00036 // MD5 Hash value
00037 
00038 class MD5Hash
00039 {
00040 public:
00041   // removed this to stop GCC warnings about using the 'pack' attribute on
00042   // non-POD members in classes such as PACKET_HEADER:
00043 
00044   // Constructor does not initialise the value
00045   //MD5Hash(void) {};
00046 
00047   // Comparison operators
00048   bool operator==(const MD5Hash &other) const;
00049   bool operator!=(const MD5Hash &other) const;
00050 
00051   bool operator(const MD5Hash &other) const;
00052   bool operator>=(const MD5Hash &other) const;
00053   bool operator>(const MD5Hash &other) const;
00054   bool operator(const MD5Hash &other) const;
00055 
00056   // Convert value to hex
00057   friend ostream& operator(ostream &s, const MD5Hash &hash);
00058   string print(void) const;
00059 
00060   // removed these to stop GCC warnings about using the 'pack' attribute on
00061   // non-POD members in classes such as PACKET_HEADER:
00062 
00063   // Copy and assignment
00064   //MD5Hash(const MD5Hash &other);
00065   //MD5Hash& operator=(const MD5Hash &other);
00066 
00067 public:
00068   u8 hash[16]; // 16 byte MD5 Hash value
00069 };
00070 
00071 // Intermediate computation state
00072 
00073 class MD5State
00074 {
00075 public:
00076   MD5State(void);
00077   void Reset(void);
00078 
00079 public:
00080   void UpdateState(const u32 (&block)[16]);
00081 
00082 protected:
00083   u32 state[4]; // 16 byte MD5 computation state
00084 };
00085 
00086 // MD5 computation context with 64 byte buffer
00087 
00088 class MD5Context : public MD5State
00089 {
00090 public:
00091   MD5Context(void);
00092   ~MD5Context(void) {};
00093   void Reset(void);
00094 
00095   // Process data from a buffer
00096   void Update(const void *buffer, size_t length);
00097 
00098   // Process 0 bytes
00099   void Update(size_t length);
00100 
00101   // Compute the final hash value
00102   void Final(MD5Hash &output);
00103 
00104   // Get the Hash value and the total number of bytes processed.
00105   MD5Hash Hash(void) const;
00106   u64 Bytes(void) const {return bytes;}
00107 
00108   friend ostream& operator(ostream &s, const MD5Context &context);
00109   string print(void) const;
00110 
00111 protected:
00112   enum {buffersize = 64};
00113   unsigned char block[buffersize];
00114   size_t used;
00115 
00116   u64 bytes;
00117 };
00118 
00119 // Compare hash values
00120 
00121 inline bool MD5Hash::operator==(const MD5Hash &other) const
00122 {
00123   return (0==memcmp(&hash, &other.hash, sizeof(hash)));
00124 }
00125 inline bool MD5Hash::operator!=(const MD5Hash &other) const
00126 {
00127   return !operator==(other);
00128 }
00129 
00130 inline bool MD5Hash::operator(const MD5Hash &other) const
00131 {
00132   size_t index = 15;
00133   while (index > 0 && hash[index] == other.hash[index])
00134   {
00135     index--;
00136   }
00137 
00138   return hash[index] hash[index];
00139 }
00140 inline bool MD5Hash::operator>=(const MD5Hash &other) const 
00141 {
00142   return !operator(other);
00143 }
00144 inline bool MD5Hash::operator>(const MD5Hash &other) const 
00145 {
00146   return other.operator00147 }
00148 inline bool MD5Hash::operator(const MD5Hash &other) const 
00149 {
00150   return !other.operator00151 }
00152 
00153 /*inline MD5Hash::MD5Hash(const MD5Hash &other)
00154 {
00155  memcpy(&hash, &other.hash, sizeof(hash));
00156 }
00157 
00158 inline MD5Hash& MD5Hash::operator=(const MD5Hash &other)
00159 {
00160  memcpy(&hash, &other.hash, sizeof(hash));
00161 
00162  return *this;
00163 }*/
00164 
00165 #endif // __MD5_H__

Generated on Sun Oct 12 01:45:29 2008 for NNTPGrab by  1.5.4