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

MD5 Class Reference

#include <md5.h>

Collaboration diagram for MD5:

Collaboration graph
[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 71 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 99 of file md5.h.

Referenced by decode(), encode(), finalize(), memcpy(), memset(), and raw_digest().

typedef unsigned short int MD5::uint2 [private]
 

Definition at line 98 of file md5.h.

typedef unsigned int MD5::uint4 [private]
 

Definition at line 97 of file md5.h.

Referenced by decode(), encode(), F(), FF(), finalize(), G(), GG(), H(), HH(), I(), II(), memcpy(), memset(), and rotate_left().


Constructor & Destructor Documentation

MD5::MD5  ) 
 

Definition at line 67 of file md5.cpp.

References init().

00068 {
00069   init();
00070 }

MD5::MD5 unsigned char *  string  ) 
 

MD5::MD5 std::istream &  stream  ) 
 

Definition at line 242 of file md5.cpp.

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

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

MD5::MD5 FILE *  file  ) 
 

Definition at line 232 of file md5.cpp.

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

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

MD5::MD5 std::ifstream &  stream  ) 
 

Definition at line 251 of file md5.cpp.

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

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


Member Function Documentation

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

Definition at line 454 of file md5.cpp.

References uint1, and uint4.

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

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

Definition at line 437 of file md5.cpp.

References uint1, and uint4.

Referenced by finalize().

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

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

Definition at line 500 of file md5.cpp.

References uint4.

Referenced by FF().

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

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

Definition at line 522 of file md5.cpp.

References F(), rotate_left(), and uint4.

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

void MD5::finalize  ) 
 

Definition at line 193 of file md5.cpp.

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

Referenced by MD5().

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

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

Definition at line 504 of file md5.cpp.

References uint4.

Referenced by GG().

00504                                                                {
00505   return (x & z) | (y & ~z);
00506 }

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

Definition at line 528 of file md5.cpp.

References G(), rotate_left(), and uint4.

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

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

Definition at line 508 of file md5.cpp.

References uint4.

Referenced by HH().

00508                                                                {
00509   return x ^ y ^ z;
00510 }

QString MD5::hex_digest  ) 
 

Definition at line 276 of file md5.cpp.

References digest, and finalized.

Referenced by filesMatch(), and getMD5().

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

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

Definition at line 534 of file md5.cpp.

References H(), rotate_left(), and uint4.

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

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

Definition at line 512 of file md5.cpp.

References uint4.

Referenced by II().

00512                                                                {
00513   return y ^ (x | ~z);
00514 }

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

Definition at line 540 of file md5.cpp.

References I(), rotate_left(), and uint4.

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

void MD5::init  )  [private]
 

Definition at line 302 of file md5.cpp.

References count, finalized, and state.

Referenced by MD5().

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

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

Definition at line 468 of file md5.cpp.

References uint1, and uint4.

Referenced by raw_digest().

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

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

Definition at line 479 of file md5.cpp.

References uint1, and uint4.

Referenced by finalize().

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

unsigned char * MD5::raw_digest  ) 
 

Definition at line 260 of file md5.cpp.

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

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

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

Definition at line 491 of file md5.cpp.

References uint4.

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

00491                                                       {
00492   return (x << n) | (x >> (32-n))  ;
00493 }

void MD5::transform uint1 buffer  )  [private]
 

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

Definition at line 171 of file md5.cpp.

References buffer, and update().

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

void MD5::update FILE *  file  ) 
 

Definition at line 124 of file md5.cpp.

References buffer, and update().

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

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

Definition at line 150 of file md5.cpp.

References buffer, and update().

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

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

Referenced by finalize(), and update().

uint4 MD5::count[2] [private]
 

Definition at line 103 of file md5.h.

Referenced by finalize(), and init().

uint1 MD5::digest[16] [private]
 

Definition at line 105 of file md5.h.

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

uint1 MD5::finalized [private]
 

Definition at line 106 of file md5.h.

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

uint4 MD5::state[4] [private]
 

Definition at line 102 of file md5.h.

Referenced by finalize(), and init().


The documentation for this class was generated from the following files:
Generated on Thu Nov 13 00:11:10 2003 for AlbumShaper by doxygen 1.3.4