#include <md5.h>
Collaboration diagram for MD5:
Definition at line 65 of file md5.h.
Public Member Functions | |
MD5 () | |
void | update (unsigned char *input, unsigned int input_length) |
void | update (std::istream &stream) |
void | update (FILE *file) |
void | update (std::ifstream &stream) |
void | finalize () |
MD5 (unsigned char *string) | |
MD5 (std::istream &stream) | |
MD5 (FILE *file) | |
MD5 (std::ifstream &stream) | |
unsigned char * | raw_digest () |
QString | hex_digest () |
Private Types | |
typedef unsigned int | uint4 |
typedef unsigned short int | uint2 |
typedef unsigned char | uint1 |
Private Member Functions | |
void | init () |
void | transform (uint1 *buffer) |
Static Private Member Functions | |
static void | encode (uint1 *dest, uint4 *src, uint4 length) |
static void | decode (uint4 *dest, uint1 *src, uint4 length) |
static void | memcpy (uint1 *dest, uint1 *src, uint4 length) |
static void | memset (uint1 *start, uint1 val, uint4 length) |
static uint4 | rotate_left (uint4 x, uint4 n) |
static uint4 | F (uint4 x, uint4 y, uint4 z) |
static uint4 | G (uint4 x, uint4 y, uint4 z) |
static uint4 | H (uint4 x, uint4 y, uint4 z) |
static uint4 | I (uint4 x, uint4 y, uint4 z) |
static void | FF (uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) |
static void | GG (uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) |
static void | HH (uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) |
static void | II (uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) |
Private Attributes | |
uint4 | state [4] |
uint4 | count [2] |
uint1 | buffer [64] |
uint1 | digest [16] |
uint1 | finalized |
typedef unsigned int MD5::uint4 [private] |
typedef unsigned short int MD5::uint2 [private] |
typedef unsigned char MD5::uint1 [private] |
MD5::MD5 | ( | unsigned char * | string | ) |
MD5::MD5 | ( | std::istream & | stream | ) |
MD5::MD5 | ( | FILE * | file | ) |
MD5::MD5 | ( | std::ifstream & | stream | ) |
void MD5::update | ( | unsigned char * | input, | |
unsigned int | input_length | |||
) |
Definition at line 71 of file md5.cpp.
References buffer, count, finalized, memcpy(), and transform().
Referenced by finalize(), MD5(), and update().
00071 { 00072 00073 uint4 input_index, buffer_index; 00074 uint4 buffer_space; // how much space is left in buffer 00075 00076 if (finalized){ // so we can't update! 00077 std::cerr << "MD5::update: Can't update a finalized digest!" << std::endl; 00078 return; 00079 } 00080 00081 // Compute number of bytes mod 64 00082 buffer_index = (unsigned int)((count[0] >> 3) & 0x3F); 00083 00084 // Update number of bits 00085 if ( (count[0] += ((uint4) input_length << 3))<((uint4) input_length << 3) ) 00086 count[1]++; 00087 00088 count[1] += ((uint4)input_length >> 29); 00089 00090 00091 buffer_space = 64 - buffer_index; // how much space is left in buffer 00092 00093 // Transform as many times as possible. 00094 if (input_length >= buffer_space) { // ie. we have enough to fill the buffer 00095 // fill the rest of the buffer and transform 00096 memcpy (buffer + buffer_index, input, buffer_space); 00097 transform (buffer); 00098 00099 // now, transform each 64-byte piece of the input, bypassing the buffer 00100 for (input_index = buffer_space; input_index + 63 < input_length; 00101 input_index += 64) 00102 transform (input+input_index); 00103 00104 buffer_index = 0; // so we can buffer remaining 00105 } 00106 else 00107 input_index=0; // so we can buffer the whole input 00108 00109 00110 // and here we do the buffering: 00111 memcpy(buffer+buffer_index, input+input_index, input_length-input_index); 00112 }
void MD5::update | ( | std::istream & | stream | ) |
Definition at line 145 of file md5.cpp.
References buffer, and update().
00145 { 00146 00147 unsigned char buffer[1024]; 00148 int len; 00149 00150 while (stream.good()){ 00151 stream.read((char*)buffer, 1024); // note that return value of read is unusable. 00152 len=stream.gcount(); 00153 update(buffer, len); 00154 } 00155 00156 }
void MD5::update | ( | FILE * | file | ) |
Definition at line 119 of file md5.cpp.
References buffer, and update().
00119 { 00120 00121 unsigned char buffer[1024]; 00122 int len; 00123 00124 while (true) 00125 { 00126 len=fread(buffer, 1, 1024, file); 00127 if(!len) 00128 { break; } 00129 00130 update(buffer, len); 00131 } 00132 00133 fclose (file); 00134 00135 }
void MD5::update | ( | std::ifstream & | stream | ) |
Definition at line 166 of file md5.cpp.
References buffer, and update().
00166 { 00167 00168 unsigned char buffer[1024]; 00169 int len; 00170 00171 while (stream.good()){ 00172 stream.read((char*)buffer, 1024); // note that return value of read is unusable. 00173 len=stream.gcount(); 00174 update(buffer, len); 00175 } 00176 00177 }
void MD5::finalize | ( | ) |
Definition at line 188 of file md5.cpp.
References buffer, count, digest, encode(), finalized, memset(), state, and update().
Referenced by MD5().
00188 { 00189 00190 unsigned char bits[8]; 00191 unsigned int index, padLen; 00192 static uint1 PADDING[64]={ 00193 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 00196 }; 00197 00198 if (finalized){ 00199 std::cerr << "MD5::finalize: Already finalized this digest!" << std::endl; 00200 return; 00201 } 00202 00203 // Save number of bits 00204 encode (bits, count, 8); 00205 00206 // Pad out to 56 mod 64. 00207 index = (uint4) ((count[0] >> 3) & 0x3f); 00208 padLen = (index < 56) ? (56 - index) : (120 - index); 00209 update (PADDING, padLen); 00210 00211 // Append length (before padding) 00212 update (bits, 8); 00213 00214 // Store state in digest 00215 encode (digest, state, 16); 00216 00217 // Zeroize sensitive information 00218 memset (buffer, 0, sizeof(*buffer)); 00219 00220 finalized=1; 00221 00222 }
unsigned char * MD5::raw_digest | ( | ) |
Definition at line 255 of file md5.cpp.
References digest, finalized, and memcpy().
00255 { 00256 00257 uint1 *s = new uint1[16]; 00258 00259 if (!finalized){ 00260 std::cerr << "MD5::raw_digest: Can't get digest if you haven't "<< 00261 "finalized the digest!" << std::endl; 00262 return ( (unsigned char*) ""); 00263 } 00264 00265 memcpy(s, digest, 16); 00266 return s; 00267 }
QString MD5::hex_digest | ( | ) |
Definition at line 271 of file md5.cpp.
References digest, and finalized.
Referenced by filesMatch(), and getMD5().
00271 { 00272 00273 int i; 00274 char *s= new char[33]; 00275 00276 if (!finalized){ 00277 std::cerr << "MD5::hex_digest: Can't get digest if you haven't "<< 00278 "finalized the digest!" << std::endl; 00279 return ""; 00280 } 00281 00282 for (i=0; i<16; i++) 00283 sprintf(s+i*2, "%02x", digest[i]); 00284 00285 s[32]='\0'; 00286 00287 QString result(s); 00288 delete s; 00289 return result; 00290 }
void MD5::init | ( | ) | [private] |
Definition at line 297 of file md5.cpp.
References count, finalized, and state.
Referenced by MD5().
00297 { 00298 finalized=0; // we just started! 00299 00300 // Nothing counted, so count=0 00301 count[0] = 0; 00302 count[1] = 0; 00303 00304 // Load magic initialization constants. 00305 state[0] = 0x67452301; 00306 state[1] = 0xefcdab89; 00307 state[2] = 0x98badcfe; 00308 state[3] = 0x10325476; 00309 }
Definition at line 432 of file md5.cpp.
Referenced by finalize().
00432 { 00433 00434 unsigned int i, j; 00435 00436 for (i = 0, j = 0; j < len; i++, j += 4) { 00437 output[j] = (uint1) (input[i] & 0xff); 00438 output[j+1] = (uint1) ((input[i] >> 8) & 0xff); 00439 output[j+2] = (uint1) ((input[i] >> 16) & 0xff); 00440 output[j+3] = (uint1) ((input[i] >> 24) & 0xff); 00441 } 00442 }
Definition at line 463 of file md5.cpp.
Referenced by raw_digest(), and update().
00463 { 00464 00465 unsigned int i; 00466 00467 for (i = 0; i < len; i++) 00468 output[i] = input[i]; 00469 }
Definition at line 474 of file md5.cpp.
Referenced by finalize().
00474 { 00475 00476 unsigned int i; 00477 00478 for (i = 0; i < len; i++) 00479 output[i] = value; 00480 }
void MD5::FF | ( | uint4 & | a, | |
uint4 | b, | |||
uint4 | c, | |||
uint4 | d, | |||
uint4 | x, | |||
uint4 | s, | |||
uint4 | ac | |||
) | [inline, static, private] |
Definition at line 517 of file md5.cpp.
References F(), and rotate_left().
00518 { 00519 a += F(b, c, d) + x + ac; 00520 a = rotate_left (a, s) +b; 00521 }
void MD5::GG | ( | uint4 & | a, | |
uint4 | b, | |||
uint4 | c, | |||
uint4 | d, | |||
uint4 | x, | |||
uint4 | s, | |||
uint4 | ac | |||
) | [inline, static, private] |
Definition at line 523 of file md5.cpp.
References G(), and rotate_left().
00524 { 00525 a += G(b, c, d) + x + ac; 00526 a = rotate_left (a, s) +b; 00527 }
void MD5::HH | ( | uint4 & | a, | |
uint4 | b, | |||
uint4 | c, | |||
uint4 | d, | |||
uint4 | x, | |||
uint4 | s, | |||
uint4 | ac | |||
) | [inline, static, private] |
Definition at line 529 of file md5.cpp.
References H(), and rotate_left().
00530 { 00531 a += H(b, c, d) + x + ac; 00532 a = rotate_left (a, s) +b; 00533 }
void MD5::II | ( | uint4 & | a, | |
uint4 | b, | |||
uint4 | c, | |||
uint4 | d, | |||
uint4 | x, | |||
uint4 | s, | |||
uint4 | ac | |||
) | [inline, static, private] |
Definition at line 535 of file md5.cpp.
References I(), and rotate_left().
00536 { 00537 a += I(b, c, d) + x + ac; 00538 a = rotate_left (a, s) +b; 00539 }
uint4 MD5::state[4] [private] |
uint4 MD5::count[2] [private] |
uint1 MD5::buffer[64] [private] |
uint1 MD5::digest[16] [private] |
uint1 MD5::finalized [private] |
Definition at line 100 of file md5.h.
Referenced by finalize(), hex_digest(), init(), raw_digest(), and update().