Sirikata
libcore/include/sirikata/core/util/Md5.hpp
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 /*
00043 
00044 Sirikata notes: the license given above is fine, but see
00045 http://fedoraproject.org/wiki/Licensing:FAQ#What_about_the_RSA_license_on_their_MD5_implementation.3F_Isn.27t_that_GPL-incompatible.3F
00046 for why the original RSA isn't really relevant. We can use that license as the
00047 modifications are provided 'as-is'.
00048 
00049 Modifications made for Sirikata are provided under the same BSD license as the
00050 Sirikata code.
00051 
00052 */
00053 
00054 #include <sirikata/core/util/Platform.hpp>
00055 #include <fstream>
00056 #include <iostream>
00057 
00058 #define MD5_DIGEST_LENGTH 16
00059 
00060 namespace Sirikata {
00061 class SIRIKATA_EXPORT MD5 {
00062 
00063 public:
00064 // methods for controlled operation:
00065     MD5              ();  // simple initializer
00066     void  update     (unsigned char *input, unsigned int input_length);
00067     void  update     (std::istream& stream);
00068     void  update     (std::ifstream& stream);
00069     void  finalize   ();
00070 
00071 // constructors for special circumstances.  All these constructors finalize
00072 // the MD5 context.
00073     MD5              (unsigned char *string); // digest string, finalize
00074     MD5              (unsigned char *string, unsigned int input_length); // digest string, finalize
00075     MD5              (std::istream& stream);       // digest stream, finalize
00076     MD5              (std::ifstream& stream);      // digest stream, close, finalize
00077 
00078 // methods to acquire finalized result
00079     unsigned char    *raw_digest ();  // digest as a 16-byte binary array
00080     char *            hex_digest ();  // digest as a 33-byte ascii-hex string
00081     friend std::ostream&   operator<< (std::ostream&, MD5 context);
00082 
00083 
00084 
00085 private:
00086 
00087 // first, some types:
00088     typedef unsigned       int uint4; // assumes integer is 4 words long
00089     typedef unsigned short int uint2; // assumes short integer is 2 words long
00090     typedef unsigned      char uint1; // assumes char is 1 word long
00091 
00092 // next, the private data:
00093     uint4 state[4];
00094     uint4 count[2];     // number of *bits*, mod 2^64
00095     uint1 buffer[64];   // input buffer
00096     uint1 digest[16];
00097     uint1 finalized;
00098     char _hex_digest[33];
00099 
00100 // last, the private methods, mostly static:
00101     void init             ();               // called by all constructors
00102     void transform        (uint1 *buffer);  // does the real update work.  Note
00103     // that length is implied to be 64.
00104 
00105     static void encode    (uint1 *dest, uint4 *src, uint4 length);
00106     static void decode    (uint4 *dest, uint1 *src, uint4 length);
00107     static void memcpy    (uint1 *dest, uint1 *src, uint4 length);
00108     static void memset    (uint1 *start, uint1 val, uint4 length);
00109 
00110     static inline uint4  rotate_left (uint4 x, uint4 n);
00111     static inline uint4  F           (uint4 x, uint4 y, uint4 z);
00112     static inline uint4  G           (uint4 x, uint4 y, uint4 z);
00113     static inline uint4  H           (uint4 x, uint4 y, uint4 z);
00114     static inline uint4  I           (uint4 x, uint4 y, uint4 z);
00115     static inline void   FF  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
00116         uint4 s, uint4 ac);
00117     static inline void   GG  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
00118         uint4 s, uint4 ac);
00119     static inline void   HH  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
00120         uint4 s, uint4 ac);
00121     static inline void   II  (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
00122         uint4 s, uint4 ac);
00123 
00124 };
00125 
00126 } // namespace Sirikata