Main Page   Compound List   File List   Compound Members   File Members  

MD5 Class Reference

#include <md5.h>

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 73 of file md5.h.

Public Member Functions

 MD5 ()
void update (unsigned char *input, unsigned int input_length)
void update (istream &stream)
void update (FILE *file)
void update (ifstream &stream)
void finalize ()
 MD5 (unsigned char *string)
 MD5 (istream &stream)
 MD5 (FILE *file)
 MD5 (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 101 of file md5.h.

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

typedef unsigned short int MD5::uint2 [private]
 

Definition at line 100 of file md5.h.

typedef unsigned int MD5::uint4 [private]
 

Definition at line 99 of file md5.h.

Referenced by decode(), and finalize().


Constructor & Destructor Documentation

MD5::MD5  ) 
 

Definition at line 65 of file md5.cpp.

References init().

00066 {
00067   init();
00068 }

MD5::MD5 unsigned char *  string  ) 
 

MD5::MD5 istream &  stream  ) 
 

Definition at line 240 of file md5.cpp.

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

00240                        {
00241 
00242   init();  // must called by all constructors
00243   update (stream);
00244   finalize();
00245 }

MD5::MD5 FILE *  file  ) 
 

Definition at line 230 of file md5.cpp.

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

00230                   {
00231 
00232   init();  // must be called be all constructors
00233   update(file);
00234   finalize ();
00235 }

MD5::MD5 ifstream &  stream  ) 
 

Definition at line 249 of file md5.cpp.

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

00249                         {
00250 
00251   init();  // must called by all constructors
00252   update (stream);
00253   finalize();
00254 }


Member Function Documentation

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

Definition at line 452 of file md5.cpp.

References uint4.

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

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

Definition at line 435 of file md5.cpp.

References uint1.

Referenced by finalize().

00435                                                         {
00436 
00437   unsigned int i, j;
00438 
00439   for (i = 0, j = 0; j < len; i++, j += 4) {
00440     output[j]   = (uint1)  (input[i] & 0xff);
00441     output[j+1] = (uint1) ((input[i] >> 8) & 0xff);
00442     output[j+2] = (uint1) ((input[i] >> 16) & 0xff);
00443     output[j+3] = (uint1) ((input[i] >> 24) & 0xff);
00444   }
00445 }

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

Definition at line 498 of file md5.cpp.

Referenced by FF().

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

void MD5::FF uint4 a,
uint4  b,
uint4  c,
uint4  d,
uint4  x,
uint4  s,
uint4  ac
[inline, static, private]
 

Definition at line 520 of file md5.cpp.

References F(), and rotate_left().

00521                                        {
00522  a += F(b, c, d) + x + ac;
00523  a = rotate_left (a, s) +b;
00524 }

void MD5::finalize  ) 
 

Definition at line 191 of file md5.cpp.

References buffer, count, digest, encode(), finalized, memset(), state, uint1, uint4, and update().

Referenced by MD5().

00191                    {
00192 
00193   unsigned char bits[8];
00194   unsigned int index, padLen;
00195   static uint1 PADDING[64]={
00196     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00197     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00198     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00199     };
00200 
00201   if (finalized){
00202     cerr << "MD5::finalize:  Already finalized this digest!" << endl;
00203     return;
00204   }
00205 
00206   // Save number of bits
00207   encode (bits, count, 8);
00208 
00209   // Pad out to 56 mod 64.
00210   index = (uint4) ((count[0] >> 3) & 0x3f);
00211   padLen = (index < 56) ? (56 - index) : (120 - index);
00212   update (PADDING, padLen);
00213 
00214   // Append length (before padding)
00215   update (bits, 8);
00216 
00217   // Store state in digest
00218   encode (digest, state, 16);
00219 
00220   // Zeroize sensitive information
00221   memset (buffer, 0, sizeof(*buffer));
00222 
00223   finalized=1;
00224 
00225 }

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

Definition at line 502 of file md5.cpp.

Referenced by GG().

00502                                                                {
00503   return (x & z) | (y & ~z);
00504 }

void MD5::GG uint4 a,
uint4  b,
uint4  c,
uint4  d,
uint4  x,
uint4  s,
uint4  ac
[inline, static, private]
 

Definition at line 526 of file md5.cpp.

References G(), and rotate_left().

00527                                       {
00528  a += G(b, c, d) + x + ac;
00529  a = rotate_left (a, s) +b;
00530 }

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

Definition at line 506 of file md5.cpp.

Referenced by HH().

00506                                                                {
00507   return x ^ y ^ z;
00508 }

QString MD5::hex_digest  ) 
 

Definition at line 274 of file md5.cpp.

References digest, and finalized.

Referenced by filesMatch(), and getMD5().

00274                        {
00275 
00276   int i;
00277   char *s= new char[33];
00278 
00279   if (!finalized){
00280     cerr << "MD5::hex_digest:  Can't get digest if you haven't "<<
00281       "finalized the digest!" <<endl;
00282     return "";
00283   }
00284 
00285   for (i=0; i<16; i++)
00286     sprintf(s+i*2, "%02x", digest[i]);
00287 
00288   s[32]='\0';
00289 
00290   QString result(s);
00291   delete s;
00292   return result;
00293 }

void MD5::HH uint4 a,
uint4  b,
uint4  c,
uint4  d,
uint4  x,
uint4  s,
uint4  ac
[inline, static, private]
 

Definition at line 532 of file md5.cpp.

References H(), and rotate_left().

00533                                       {
00534  a += H(b, c, d) + x + ac;
00535  a = rotate_left (a, s) +b;
00536 }

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

Definition at line 510 of file md5.cpp.

Referenced by II().

00510                                                                {
00511   return y ^ (x | ~z);
00512 }

void MD5::II uint4 a,
uint4  b,
uint4  c,
uint4  d,
uint4  x,
uint4  s,
uint4  ac
[inline, static, private]
 

Definition at line 538 of file md5.cpp.

References I(), and rotate_left().

00539                                                {
00540  a += I(b, c, d) + x + ac;
00541  a = rotate_left (a, s) +b;
00542 }

void MD5::init  )  [private]
 

Definition at line 300 of file md5.cpp.

References count, finalized, and state.

Referenced by MD5().

00300               {
00301   finalized=0;  // we just started!
00302 
00303   // Nothing counted, so count=0
00304   count[0] = 0;
00305   count[1] = 0;
00306 
00307   // Load magic initialization constants.
00308   state[0] = 0x67452301;
00309   state[1] = 0xefcdab89;
00310   state[2] = 0x98badcfe;
00311   state[3] = 0x10325476;
00312 }

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

Definition at line 466 of file md5.cpp.

Referenced by raw_digest().

00466                                                        {
00467 
00468   unsigned int i;
00469 
00470   for (i = 0; i < len; i++)
00471     output[i] = input[i];
00472 }

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

Definition at line 477 of file md5.cpp.

Referenced by finalize().

00477                                                       {
00478 
00479   unsigned int i;
00480 
00481   for (i = 0; i < len; i++)
00482     output[i] = value;
00483 }

unsigned char * MD5::raw_digest  ) 
 

Definition at line 258 of file md5.cpp.

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

00258                               {
00259 
00260   uint1 *s = new uint1[16];
00261 
00262   if (!finalized){
00263     cerr << "MD5::raw_digest:  Can't get digest if you haven't "<<
00264       "finalized the digest!" <<endl;
00265     return ( (unsigned char*) "");
00266   }
00267 
00268   memcpy(s, digest, 16);
00269   return s;
00270 }

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

Definition at line 489 of file md5.cpp.

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

00489                                                       {
00490   return (x << n) | (x >> (32-n))  ;
00491 }

void MD5::transform uint1 buffer  )  [private]
 

void MD5::update ifstream &  stream  ) 
 

Definition at line 169 of file md5.cpp.

References buffer, and update().

00169                                 {
00170 
00171   unsigned char buffer[1024];
00172   int len;
00173 
00174   while (stream.good()){
00175     stream.read((char*)buffer, 1024); // note that return value of read is unusable.
00176     len=stream.gcount();
00177     update(buffer, len);
00178   }
00179 
00180 }

void MD5::update FILE *  file  ) 
 

Definition at line 122 of file md5.cpp.

References buffer, and update().

00122                           {
00123 
00124   unsigned char buffer[1024];
00125   int len;
00126 
00127   while (true)
00128   {
00129     len=fread(buffer, 1, 1024, file);
00130     if(!len)
00131     {  break; }
00132       
00133     update(buffer, len);
00134    }
00135 
00136   fclose (file);
00137 
00138 }

void MD5::update istream &  stream  ) 
 

Definition at line 148 of file md5.cpp.

References buffer, and update().

00148                                {
00149 
00150   unsigned char buffer[1024];
00151   int len;
00152 
00153   while (stream.good()){
00154     stream.read((char*)buffer, 1024); // note that return value of read is unusable.
00155     len=stream.gcount();
00156     update(buffer, len);
00157   }
00158 
00159 }

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 106 of file md5.h.

Referenced by finalize(), and update().

uint4 MD5::count[2] [private]
 

Definition at line 105 of file md5.h.

Referenced by finalize(), and init().

uint1 MD5::digest[16] [private]
 

Definition at line 107 of file md5.h.

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

uint1 MD5::finalized [private]
 

Definition at line 108 of file md5.h.

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

uint4 MD5::state[4] [private]
 

Definition at line 104 of file md5.h.

Referenced by finalize(), and init().


The documentation for this class was generated from the following files:
Generated on Tue Jun 10 23:41:22 2003 for AlbumShaper by doxygen 1.3.1