00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef MD5_H
00033 #define MD5_H
00034
00035 #include <stdint.h>
00036
00037 #include "MD5_Defs.h"
00038
00039 typedef uint8_t md5_byte_t;
00040 typedef uint32_t md5_word_t;
00041
00042 class MD5
00043 {
00044 public:
00045
00046 MD5();
00047
00048
00049 void append(const void* data, int nbytes);
00050
00051
00052 void finish();
00053
00054
00055 const md5_byte_t* getDigest();
00056
00057
00058 void reset();
00059
00060 private:
00061
00062
00063 md5_word_t count[2];
00064 md5_word_t abcd[4];
00065 md5_byte_t buf[64];
00066
00067 md5_byte_t digest[16];
00068
00069 md5_word_t tmpBuf[16];
00070 const md5_word_t* X;
00071
00072 void
00073 process(const md5_byte_t data[64]);
00074
00075 md5_word_t
00076 ROTATE_LEFT(const md5_word_t x, const int n);
00077
00078 md5_word_t
00079 F(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00080
00081 md5_word_t
00082 G(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00083
00084 md5_word_t
00085 H(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00086
00087 md5_word_t
00088 I(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00089
00090 typedef md5_word_t (MD5::*md5func)(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00091
00092 void
00093 SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
00094 md5_word_t& d, const int k, const int s,
00095 const md5_word_t Ti);
00096 };
00097
00098 inline md5_word_t
00099 MD5::ROTATE_LEFT(const md5_word_t x, const int n)
00100 {
00101 return ( (x<<n) | (x>>(32-n)) );
00102 }
00103
00104 inline md5_word_t
00105 MD5::F(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00106 {
00107 return ( (x&y) | (~x&z) );
00108 }
00109
00110 inline md5_word_t
00111 MD5::G(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00112 {
00113 return ( (x&z) | (y&~z) );
00114 }
00115
00116 inline md5_word_t
00117 MD5::H(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00118 {
00119 return ( x^y^z );
00120 }
00121
00122 inline md5_word_t
00123 MD5::I(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00124 {
00125 return ( y ^ (x|~z) );
00126 }
00127
00128 inline void
00129 MD5::SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
00130 md5_word_t& d, const int k, const int s,
00131 const md5_word_t Ti)
00132 {
00133 md5_word_t t = a + (this->*func)(b,c,d) + X[k] + Ti;
00134 a = ROTATE_LEFT(t, s) + b;
00135 }
00136
00137 #endif