Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

base/MD5

Go to the documentation of this file.
00001 // MD5.CC - source code for the C++/object oriented translation and 
00002 //          modification of MD5.
00003 
00004 // Translation and modification (c) 1995 by Mordechai T. Abzug 
00005 
00006 // This translation/ modification is provided "as is," without express or 
00007 // implied warranty of any kind.
00008 
00009 // The translator/ modifier does not claim (1) that MD5 will do what you think 
00010 // it does; (2) that this translation/ modification is accurate; or (3) that 
00011 // this software is "merchantible."  (Language for this disclaimer partially 
00012 // copied from the disclaimer below).
00013 
00014 /* based on:
00015 
00016    MD5.H - header file for MD5C.C
00017    MDDRIVER.C - test driver for MD2, MD4 and MD5
00018 
00019    Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
00020 rights reserved.
00021 
00022 License to copy and use this software is granted provided that it
00023 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
00024 Algorithm" in all material mentioning or referencing this software
00025 or this function.
00026 
00027 License is also granted to make and use derivative works provided
00028 that such works are identified as "derived from the RSA Data
00029 Security, Inc. MD5 Message-Digest Algorithm" in all material
00030 mentioning or referencing the derived work.
00031 
00032 RSA Data Security, Inc. makes no representations concerning either
00033 the merchantability of this software or the suitability of this
00034 software for any particular purpose. It is provided "as is"
00035 without express or implied warranty of any kind.
00036 
00037 These notices must be retained in any copies of any part of this
00038 documentation and/or software.
00039 
00040 */
00041 
00042 #ifndef _BASE_MD5_
00043 #define _BASE_MD5_
00044 
00045 #include <base/base>
00046 
00047 #include <stdio.h>
00048 #include <fstream>
00049 #include <iostream>
00050 
00051 
00052 namespace base {
00053 
00054 
00055 class MD5 
00056 {
00057 
00058 public:
00059 // methods for controlled operation:
00060   MD5              ();  // simple initializer
00061   void  update     (unsigned char *input, unsigned int input_length);
00062   void  update     (std::istream& stream);
00063   void  update     (FILE *file);
00064   void  update     (std::ifstream& stream);
00065   void  finalize   ();
00066 
00067 // constructors for special circumstances.  All these constructors finalize
00068 // the MD5 context.
00069   MD5              (unsigned char *string); // digest string, finalize
00070   MD5              (std::istream& stream);  // digest stream, finalize
00071   MD5              (FILE *file);            // digest file, close, finalize
00072   MD5              (std::ifstream& stream); // digest stream, close, finalize
00073 
00074 // methods to acquire finalized result
00075   unsigned char    *raw_digest ();  // digest as a 16-byte binary array
00076   char *            hex_digest ();  // digest as a 33-byte ascii-hex string
00077   friend std::ostream&   operator<< (std::ostream&, MD5 context);
00078 
00079 
00080 
00081 private:
00082 
00083 // first, some types:
00084   typedef unsigned       int uint4; // assumes integer is 4 words long
00085   typedef unsigned short int uint2; // assumes short integer is 2 words long
00086   typedef unsigned      char uint1; // assumes char is 1 word long
00087 
00088 // next, the private data:
00089   uint4 state[4];
00090   uint4 count[2];     // number of *bits*, mod 2^64
00091   uint1 buffer[64];   // input buffer
00092   uint1 digest[16];
00093   uint1 finalized;
00094 
00095 // last, the private methods, mostly static:
00096   void init             ();               // called by all constructors
00097   void transform        (uint1 *buffer);  // does the real update work.  Note 
00098                                           // that length is implied to be 64.
00099 
00100   static void encode    (uint1 *dest, uint4 *src, uint4 length);
00101   static void decode    (uint4 *dest, uint1 *src, uint4 length);
00102   static void memcpy    (uint1 *dest, uint1 *src, uint4 length);
00103   static void memset    (uint1 *start, uint1 val, uint4 length);
00104 
00105   static inline uint4  rotate_left (uint4 x, uint4 n);
00106   static inline uint4  F           (uint4 x, uint4 y, uint4 z);
00107   static inline uint4  G           (uint4 x, uint4 y, uint4 z);
00108   static inline uint4  H           (uint4 x, uint4 y, uint4 z);
00109   static inline uint4  I           (uint4 x, uint4 y, uint4 z);
00110   static inline void   FF  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00111                             uint4 s, uint4 ac);
00112   static inline void   GG  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00113                             uint4 s, uint4 ac);
00114   static inline void   HH  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00115                             uint4 s, uint4 ac);
00116   static inline void   II  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00117                             uint4 s, uint4 ac);
00118 
00119 };
00120 
00121 } // base
00122 
00123 #endif

Generated on Thu Jul 29 15:56:08 2004 for OpenSim by doxygen 1.3.6