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
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef _CSP_EXAMPLES_MD5_H
00042 #define _CSP_EXAMPLES_MD5_H
00043
00044
00045 #include <cstdio>
00046 #include <fstream>
00047 #include <iostream>
00048
00049
00050 class MD5
00051 {
00052 public:
00054 MD5();
00055
00059 bool update(unsigned char* input, unsigned int input_length);
00060
00063 bool update(std::istream& stream);
00064
00066 bool update(FILE* file);
00067
00069 bool update(std::ifstream& stream);
00070
00073 bool finalize();
00074
00077
00079 MD5(unsigned char* asciiz);
00080
00082 MD5(std::istream& stream);
00083
00085 MD5(std::ifstream& stream);
00086
00088 MD5(FILE* file);
00089
00091
00094 unsigned char* rawDigest();
00095
00098 char* hexDigest();
00099
00100 friend std::ostream& operator<<(std::ostream&, MD5 context);
00101
00102 private:
00104 typedef unsigned int uint4;
00106 typedef unsigned short int uint2;
00107
00109 typedef unsigned char uint1;
00110
00111 uint4 state[4];
00112
00114 uint4 count[2];
00115
00117 uint1 buffer[64];
00118 uint1 digest[16];
00119 uint1 finalized;
00120
00122 void init();
00123
00125 void transform(uint1* buffer);
00126
00127 static void encode(uint1* dest, uint4* src, uint4 length);
00128 static void decode(uint4* dest, uint1* src, uint4 length);
00129 static void memcpy(uint1* dest, uint1* src, uint4 length);
00130 static void memset(uint1* start, uint1 val, uint4 length);
00131
00132 static inline uint4 rotate_left(uint4 x, uint4 n);
00133 static inline uint4 F(uint4 x, uint4 y, uint4 z);
00134 static inline uint4 G(uint4 x, uint4 y, uint4 z);
00135 static inline uint4 H(uint4 x, uint4 y, uint4 z);
00136 static inline uint4 I(uint4 x, uint4 y, uint4 z);
00137 static inline void FF(
00138 uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
00139 static inline void GG(
00140 uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
00141 static inline void HH(
00142 uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
00143 static inline void II(
00144 uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
00145 };
00146
00147
00148 #endif // _CSP_EXAMPLES_MD5_H
00149
00150
00151
00152