BWAPI
|
00001 /* MD5 00002 converted to C++ class by Frank Thilo (thilo@unix-ag.org) 00003 for bzflag (http://www.bzflag.org) 00004 00005 based on: 00006 00007 md5.h and md5.c 00008 reference implementation of RFC 1321 00009 00010 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 00011 rights reserved. 00012 00013 License to copy and use this software is granted provided that it 00014 is identified as the "RSA Data Security, Inc. MD5 Message-Digest 00015 Algorithm" in all material mentioning or referencing this software 00016 or this function. 00017 00018 License is also granted to make and use derivative works provided 00019 that such works are identified as "derived from the RSA Data 00020 Security, Inc. MD5 Message-Digest Algorithm" in all material 00021 mentioning or referencing the derived work. 00022 00023 RSA Data Security, Inc. makes no representations concerning either 00024 the merchantability of this software or the suitability of this 00025 software for any particular purpose. It is provided "as is" 00026 without express or implied warranty of any kind. 00027 00028 These notices must be retained in any copies of any part of this 00029 documentation and/or software. 00030 00031 */ 00032 00033 #ifndef BZF_MD5_H 00034 #define BZF_MD5_H 00035 00036 #include <string> 00037 #include <iostream> 00038 00039 00040 // a small class for calculating MD5 hashes of strings or byte arrays 00041 // it is not meant to be fast or secure 00042 // 00043 // usage: 1) feed it blocks of uchars with update() 00044 // 2) finalize() 00045 // 3) get hexdigest() string 00046 // or 00047 // MD5(std::string).hexdigest() 00048 // 00049 // assumes that char is 8 bit and int is 32 bit 00050 class MD5 00051 { 00052 public: 00053 typedef unsigned int size_type; // must be 32bit 00054 00055 MD5(); 00056 MD5(const std::string& text); 00057 void update(const unsigned char *buf, size_type length); 00058 void update(const char *buf, size_type length); 00059 MD5& finalize(); 00060 std::string hexdigest() const; 00061 friend std::ostream& operator<<(std::ostream&, MD5 md5); 00062 00063 private: 00064 void init(); 00065 typedef unsigned char uint1; // 8bit 00066 typedef unsigned int uint4; // 32bit 00067 enum {blocksize = 64}; // VC6 won't eat a const static int here 00068 00069 void transform(const uint1 block[blocksize]); 00070 static void decode(uint4 output[], const uint1 input[], size_type len); 00071 static void encode(uint1 output[], const uint4 input[], size_type len); 00072 00073 bool finalized; 00074 uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk 00075 uint4 count[2]; // 64bit counter for number of bits (lo, hi) 00076 uint4 state[4]; // digest so far 00077 uint1 digest[16]; // the result 00078 00079 // low level logic operations 00080 static inline uint4 F(uint4 x, uint4 y, uint4 z); 00081 static inline uint4 G(uint4 x, uint4 y, uint4 z); 00082 static inline uint4 H(uint4 x, uint4 y, uint4 z); 00083 static inline uint4 I(uint4 x, uint4 y, uint4 z); 00084 static inline uint4 rotate_left(uint4 x, int n); 00085 static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 00086 static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 00087 static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 00088 static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 00089 }; 00090 00091 std::string md5(const std::string str); 00092 00093 #endif