md5.h

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <fstream>
00003 #include <iostream>
00004 using namespace std;
00005 
00006 /*** unsigned char for stream parameter is not working for some reason
00007  * I have to fix a version that will work with char stream
00008  */
00009 
00010 class MD5 {
00011 
00012 public:
00013 // methods for controlled operation:
00014   MD5();  // simple initializer
00015   void update(unsigned char *input, unsigned int input_length);
00016   void update(char* input);  // added by kemin for convience
00017   void update(const char input[]);
00018   void update(istream& stream);
00019   void update(FILE *file);
00020   void update(ifstream& stream);
00021   void finalize();  // after finalize cannot update anymore
00022 
00023 // constructors for special circumstances.  All these constructors finalize
00024 // the MD5 context.
00025   MD5(unsigned char *string); // digest string, finalize
00026   MD5(char *string); // digest string, finalize
00027   MD5(istream& stream);       // digest stream, finalize
00028   MD5(FILE *file);            // digest file, close, finalize
00029   MD5(ifstream& stream);      // digest stream, close, finalize
00030 
00031 // methods to acquire finalized result
00032         /* the caller must delete the returned pointer */
00033   unsigned char* raw_digest ();  // digest as a 16-byte binary array
00034   /* caller needs to delete the returned pointer */
00035   char *hex_digest();  // digest as a 33-byte ascii-hex string
00036   // output the hex_digest string
00037   friend ostream&   operator<<(ostream&, MD5 context);
00038 
00039 private:
00040   char result[33];  // holds the digest result
00041 
00042 // first, some types:
00043   typedef unsigned       int uint4; // assumes integer is 4 words long
00044   typedef unsigned short int uint2; // assumes short integer is 2 words long
00045   typedef unsigned      char uint1; // assumes char is 1 word long
00046 
00047 // next, the private data:
00048   uint4 state[4];
00049   uint4 count[2];     // number of *bits*, mod 2^64
00050   uint1 buffer[64];   // input buffer
00051   uint1 digest[16];
00052   uint1 finalized;
00053 
00054 // last, the private methods, mostly static:
00055   void init();               // called by all constructors
00056   void transform(uint1 *buffer);  // does the real update work.  Note 
00057                                           // that length is implied to be 64.
00058 
00059   static void encode(uint1 *dest, uint4 *src, uint4 length);
00060   static void decode(uint4 *dest, uint1 *src, uint4 length);
00061   static void memcpy(uint1 *dest, uint1 *src, uint4 length);
00062   static void memset(uint1 *start, uint1 val, uint4 length);
00063 
00064   static inline uint4  rotate_left (uint4 x, uint4 n);
00065   static inline uint4  F           (uint4 x, uint4 y, uint4 z);
00066   static inline uint4  G           (uint4 x, uint4 y, uint4 z);
00067   static inline uint4  H           (uint4 x, uint4 y, uint4 z);
00068   static inline uint4  I           (uint4 x, uint4 y, uint4 z);
00069   static inline void   FF  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00070                             uint4 s, uint4 ac);
00071   static inline void   GG  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00072                             uint4 s, uint4 ac);
00073   static inline void   HH  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00074                             uint4 s, uint4 ac);
00075   static inline void   II  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, 
00076                             uint4 s, uint4 ac);
00077 };

Generated on Wed Aug 10 11:56:50 2011 for Softwares from Orpara by  doxygen 1.5.6