Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

MD5 Class Reference

#include <md5.h>

Collaboration diagram for MD5:

[legend]
List of all members.

Detailed Description

MD5 Support allows checksums to be computed for images, determing if they have changed since the album was saved out, and if so need to be loaded and rescaled.

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

void encode (uint1 *dest, uint4 *src, uint4 length)
void decode (uint4 *dest, uint1 *src, uint4 length)
void memcpy (uint1 *dest, uint1 *src, uint4 length)
void memset (uint1 *start, uint1 val, uint4 length)
uint4 rotate_left (uint4 x, uint4 n)
uint4 F (uint4 x, uint4 y, uint4 z)
uint4 G (uint4 x, uint4 y, uint4 z)
uint4 H (uint4 x, uint4 y, uint4 z)
uint4 I (uint4 x, uint4 y, uint4 z)
void FF (uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
void GG (uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
void HH (uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
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


Member Typedef Documentation

typedef unsigned char MD5::uint1 [private]
 

Definition at line 93 of file md5.h.

Referenced by encode(), finalize(), and raw_digest().

typedef unsigned short int MD5::uint2 [private]
 

Definition at line 92 of file md5.h.

typedef unsigned int MD5::uint4 [private]
 

Definition at line 91 of file md5.h.

Referenced by decode(), and finalize().


Constructor & Destructor Documentation

MD5::MD5  ) 
 

Definition at line 62 of file md5.cpp.

References init().

00063 { 00064 init(); 00065 }

MD5::MD5 unsigned char *  string  ) 
 

MD5::MD5 std::istream &  stream  ) 
 

Definition at line 237 of file md5.cpp.

References finalize(), init(), and update().

00237 { 00238 00239 init(); // must called by all constructors 00240 update (stream); 00241 finalize(); 00242 }

MD5::MD5 FILE *  file  ) 
 

Definition at line 227 of file md5.cpp.

References finalize(), init(), and update().

00227 { 00228 00229 init(); // must be called be all constructors 00230 update(file); 00231 finalize (); 00232 }

MD5::MD5 std::ifstream &  stream  ) 
 

Definition at line 246 of file md5.cpp.

References finalize(), init(), and update().

00246 { 00247 00248 init(); // must called by all constructors 00249 update (stream); 00250 finalize(); 00251 }


Member Function Documentation

void MD5::decode uint4 dest,
uint1 src,
uint4  length
[static, private]
 

Definition at line 449 of file md5.cpp.

References decode(), and uint4.

Referenced by decode().

00449 { 00450 00451 unsigned int i, j; 00452 00453 for (i = 0, j = 0; j < len; i++, j += 4) 00454 output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) | 00455 (((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24); 00456 }

void MD5::encode uint1 dest,
uint4 src,
uint4  length
[static, private]
 

Definition at line 432 of file md5.cpp.

References encode(), and uint1.

Referenced by encode(), and 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 }

unsigned int MD5::F uint4  x,
uint4  y,
uint4  z
[inline, static, private]
 

Definition at line 495 of file md5.cpp.

References F().

Referenced by F(), and FF().

00495 { 00496 return (x & y) | (~x & z); 00497 }

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 b, F(), FF(), and rotate_left().

Referenced by FF().

00518 { 00519 a += F(b, c, d) + x + ac; 00520 a = rotate_left (a, s) +b; 00521 }

void MD5::finalize  ) 
 

Definition at line 188 of file md5.cpp.

References buffer, count, digest, encode(), finalized, memset(), state, uint1, uint4, 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 int MD5::G uint4  x,
uint4  y,
uint4  z
[inline, static, private]
 

Definition at line 499 of file md5.cpp.

References G().

Referenced by G(), and GG().

00499 { 00500 return (x & z) | (y & ~z); 00501 }

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 b, G(), GG(), and rotate_left().

Referenced by GG().

00524 { 00525 a += G(b, c, d) + x + ac; 00526 a = rotate_left (a, s) +b; 00527 }

unsigned int MD5::H uint4  x,
uint4  y,
uint4  z
[inline, static, private]
 

Definition at line 503 of file md5.cpp.

References H().

Referenced by H(), and HH().

00503 { 00504 return x ^ y ^ z; 00505 }

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::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 b, H(), HH(), and rotate_left().

Referenced by HH().

00530 { 00531 a += H(b, c, d) + x + ac; 00532 a = rotate_left (a, s) +b; 00533 }

unsigned int MD5::I uint4  x,
uint4  y,
uint4  z
[inline, static, private]
 

Definition at line 507 of file md5.cpp.

References I().

Referenced by I(), and II().

00507 { 00508 return y ^ (x | ~z); 00509 }

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 b, I(), II(), and rotate_left().

Referenced by II().

00536 { 00537 a += I(b, c, d) + x + ac; 00538 a = rotate_left (a, s) +b; 00539 }

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 }

void MD5::memcpy uint1 dest,
uint1 src,
uint4  length
[static, private]
 

Definition at line 463 of file md5.cpp.

References memcpy().

Referenced by memcpy(), and raw_digest().

00463 { 00464 00465 unsigned int i; 00466 00467 for (i = 0; i < len; i++) 00468 output[i] = input[i]; 00469 }

void MD5::memset uint1 start,
uint1  val,
uint4  length
[static, private]
 

Definition at line 474 of file md5.cpp.

References memset().

Referenced by finalize(), and memset().

00474 { 00475 00476 unsigned int i; 00477 00478 for (i = 0; i < len; i++) 00479 output[i] = value; 00480 }

unsigned char * MD5::raw_digest  ) 
 

Definition at line 255 of file md5.cpp.

References digest, finalized, memcpy(), and uint1.

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 }

unsigned int MD5::rotate_left uint4  x,
uint4  n
[inline, static, private]
 

Definition at line 486 of file md5.cpp.

References rotate_left().

Referenced by FF(), GG(), HH(), II(), and rotate_left().

00486 { 00487 return (x << n) | (x >> (32-n)) ; 00488 }

void MD5::transform uint1 buffer  )  [private]
 

void MD5::update std::ifstream &  stream  ) 
 

Definition at line 166 of file md5.cpp.

References buffer, 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::update FILE *  file  ) 
 

Definition at line 119 of file md5.cpp.

References buffer, 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::istream &  stream  ) 
 

Definition at line 145 of file md5.cpp.

References buffer, 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 unsigned char *  input,
unsigned int  input_length
 

Referenced by finalize(), MD5(), and update().


Member Data Documentation

uint1 MD5::buffer[64] [private]
 

Definition at line 98 of file md5.h.

Referenced by finalize(), and update().

uint4 MD5::count[2] [private]
 

Definition at line 97 of file md5.h.

Referenced by finalize(), and init().

uint1 MD5::digest[16] [private]
 

Definition at line 99 of file md5.h.

Referenced by finalize(), hex_digest(), and raw_digest().

uint1 MD5::finalized [private]
 

Definition at line 100 of file md5.h.

Referenced by finalize(), hex_digest(), init(), and raw_digest().

uint4 MD5::state[4] [private]
 

Definition at line 96 of file md5.h.

Referenced by finalize(), and init().


The documentation for this class was generated from the following files:
Generated on Sun Mar 4 19:43:07 2007 for AlbumShaper by doxygen 1.3.7