MD5 Class Reference

#include <md5.hpp>

List of all members.

Public Member Functions

 MD5 ()
 MD5 (uint8_t *string)
 digest string, finalize
 MD5 (std::istream &stream)
 digest stream, finalize
 MD5 (FILE *file)
 digest file, close, finalize
 MD5 (std::ifstream &stream)
 digest stream, close, finalize
void update (uint8_t *input, unsigned int input_length)
 MD5 block update operation.
void update (std::istream &stream)
void update (FILE *file)
void update (std::ifstream &stream)
uint8_t * raw_digest ()
 digest as a 16-byte binary array
std::string hex_digest ()
 digest as a 33-byte ascii-hex string

Private Member Functions

void init ()
 called by all constructors
void finalize ()
 MD5 finalization.
void transform (uint8_t *buffer)
 MD5 basic transformation.

Static Private Member Functions

static void encode (uint8_t *dest, uint32_t *src, uint32_t length)
 Encodes input (uint32_t) into output (uint8_t).
static void decode (uint32_t *dest, uint8_t *src, uint32_t length)
 Decodes input (uint8_t) into output (uint32_t).
static void memcpy (uint8_t *dest, uint8_t *src, uint32_t length)
static void memset (uint8_t *start, uint8_t val, uint32_t length)
static uint32_t rotate_left (uint32_t x, uint32_t n)
static uint32_t F (uint32_t x, uint32_t y, uint32_t z)
static uint32_t G (uint32_t x, uint32_t y, uint32_t z)
static uint32_t H (uint32_t x, uint32_t y, uint32_t z)
static uint32_t I (uint32_t x, uint32_t y, uint32_t z)
static void FF (uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac)
 FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
static void GG (uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac)
static void HH (uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac)
static void II (uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t x, uint32_t s, uint32_t ac)

Private Attributes

uint32_t state [4]
uint32_t count [2]
 number of _bits_, mod 2^64
uint8_t buffer [64]
 input buffer
uint8_t digest [16]
bool finalized

Friends

std::ostream & operator<< (std::ostream &, MD5 context)


Detailed Description

Definition at line 50 of file md5.hpp.


Constructor & Destructor Documentation

MD5::MD5 (  ) 

Definition at line 49 of file md5.cpp.

References init().

00049          :
00050   finalized()
00051 {
00052   init();
00053 }

MD5::MD5 ( uint8_t *  string  ) 

digest string, finalize

MD5::MD5 ( std::istream &  stream  ) 

digest stream, finalize

Definition at line 130 of file md5.cpp.

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

00130                            : 
00131   finalized()
00132 {
00133   init(); // must called by all constructors
00134   update (stream);
00135   finalize();
00136 }

MD5::MD5 ( FILE *  file  ) 

digest file, close, finalize

Definition at line 122 of file md5.cpp.

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

00122                    :
00123   finalized()
00124 {
00125   init(); // must be called be all constructors
00126   update(file);
00127   finalize ();
00128 }

MD5::MD5 ( std::ifstream &  stream  ) 

digest stream, close, finalize

Definition at line 138 of file md5.cpp.

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

00138                             :
00139   finalized()
00140 {
00141   init(); // must called by all constructors
00142   update (stream);
00143   finalize();
00144 }


Member Function Documentation

void MD5::update ( uint8_t *  input,
unsigned int  input_length 
)

MD5 block update operation.

Continues an MD5 message-digest operation, processing another message block, and updating the context.

Referenced by finalize(), Addon::get_md5(), MD5(), and update().

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

Definition at line 100 of file md5.cpp.

References buffer, and update().

00100                                    {
00101   uint8_t buffer[1024];
00102   int len;
00103 
00104   while (stream.good()) {
00105     stream.read((char*)buffer, 1024); // note that return value of read is unusable.
00106     len=stream.gcount();
00107     update(buffer, len);
00108   }
00109 }

void MD5::update ( FILE *  file  ) 

Definition at line 91 of file md5.cpp.

References buffer, and update().

00091                            {
00092   uint8_t buffer[1024];
00093   int len;
00094 
00095   while ((len=fread(buffer, 1, 1024, file))) update(buffer, len);
00096 
00097   fclose (file);
00098 }

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

Definition at line 111 of file md5.cpp.

References buffer, and update().

00111                                     {
00112   uint8_t buffer[1024];
00113   int len;
00114 
00115   while (stream.good()) {
00116     stream.read((char*)buffer, 1024); // note that return value of read is unusable.
00117     len=stream.gcount();
00118     update(buffer, len);
00119   }
00120 }

uint8_t * MD5::raw_digest (  ) 

digest as a 16-byte binary array

Definition at line 146 of file md5.cpp.

References digest, finalize(), and memcpy().

00146                          {
00147   uint8_t* s = new uint8_t[16];
00148 
00149   finalize();
00150 
00151   memcpy(s, digest, 16);
00152   return s;
00153 }

std::string MD5::hex_digest (  ) 

digest as a 33-byte ascii-hex string

Definition at line 155 of file md5.cpp.

References digest, and finalize().

Referenced by Addon::get_md5(), and operator<<().

00155                           {
00156   int i;
00157   char* s= new char[33];
00158 
00159   finalize();
00160 
00161   for (i=0; i<16; i++) sprintf(s+i*2, "%02x", digest[i]);
00162 
00163   s[32]='\0';
00164 
00165   return s;
00166 }

void MD5::init (  )  [private]

called by all constructors

Definition at line 175 of file md5.cpp.

References count, finalized, and state.

Referenced by MD5().

00175                {
00176   finalized=false;
00177 
00178   // Nothing counted, so count=0
00179   count[0] = 0;
00180   count[1] = 0;
00181 
00182   // Load magic initialization constants.
00183   state[0] = 0x67452301;
00184   state[1] = 0xefcdab89;
00185   state[2] = 0x98badcfe;
00186   state[3] = 0x10325476;
00187 }

void MD5::finalize (  )  [private]

MD5 finalization.

Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context.

Definition at line 189 of file md5.cpp.

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

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

00189                    {
00190   if (finalized) return;
00191 
00192   uint8_t bits[8];
00193   unsigned int index, padLen;
00194   static uint8_t PADDING[64]={
00195     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00196     0, 0, 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
00198   };
00199 
00200   // Save number of bits
00201   encode (bits, count, 8);
00202 
00203   // Pad out to 56 mod 64.
00204   index = (uint32_t) ((count[0] >> 3) & 0x3f);
00205   padLen = (index < 56) ? (56 - index) : (120 - index);
00206   update (PADDING, padLen);
00207 
00208   // Append length (before padding)
00209   update (bits, 8);
00210 
00211   // Store state in digest
00212   encode (digest, state, 16);
00213 
00214   // Zeroize sensitive information
00215   memset (buffer, 0, sizeof(*buffer));
00216 
00217   finalized=true;
00218 }

void MD5::transform ( uint8_t *  buffer  )  [private]

MD5 basic transformation.

Transforms state based on block. Does the real update work. Note that length is implied to be 64.

void MD5::encode ( uint8_t *  dest,
uint32_t *  src,
uint32_t  length 
) [static, private]

Encodes input (uint32_t) into output (uint8_t).

Assumes len is a multiple of 4.

Definition at line 329 of file md5.cpp.

Referenced by finalize().

00329                                                                 {
00330   unsigned int i, j;
00331 
00332   for (i = 0, j = 0; j < len; i++, j += 4) {
00333     output[j]   = (uint8_t) (input[i] & 0xff);
00334     output[j+1] = (uint8_t) ((input[i] >> 8) & 0xff);
00335     output[j+2] = (uint8_t) ((input[i] >> 16) & 0xff);
00336     output[j+3] = (uint8_t) ((input[i] >> 24) & 0xff);
00337   }
00338 }

void MD5::decode ( uint32_t *  dest,
uint8_t *  src,
uint32_t  length 
) [static, private]

Decodes input (uint8_t) into output (uint32_t).

Assumes len is a multiple of 4.

Definition at line 340 of file md5.cpp.

00340                                                                 {
00341   unsigned int i, j;
00342 
00343   for (i = 0, j = 0; j < len; i++, j += 4) {
00344     output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) | (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
00345   }
00346 }

void MD5::memcpy ( uint8_t *  dest,
uint8_t *  src,
uint32_t  length 
) [static, private]

Definition at line 349 of file md5.cpp.

Referenced by raw_digest().

00349                                                                {
00350   unsigned int i;
00351 
00352   for (i = 0; i < len; i++) output[i] = input[i];
00353 }

void MD5::memset ( uint8_t *  start,
uint8_t  val,
uint32_t  length 
) [static, private]

Definition at line 356 of file md5.cpp.

Referenced by finalize().

00356                                                               {
00357   unsigned int i;
00358 
00359   for (i = 0; i < len; i++) output[i] = value;
00360 }

unsigned int MD5::rotate_left ( uint32_t  x,
uint32_t  n 
) [inline, static, private]

Definition at line 362 of file md5.cpp.

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

00362                                                            {
00363   return (x << n) | (x >> (32-n));
00364 }

unsigned int MD5::F ( uint32_t  x,
uint32_t  y,
uint32_t  z 
) [inline, static, private]

Definition at line 366 of file md5.cpp.

Referenced by FF().

00366                                                              {
00367   return (x & y) | (~x & z);
00368 }

unsigned int MD5::G ( uint32_t  x,
uint32_t  y,
uint32_t  z 
) [inline, static, private]

Definition at line 370 of file md5.cpp.

Referenced by GG().

00370                                                              {
00371   return (x & z) | (y & ~z);
00372 }

unsigned int MD5::H ( uint32_t  x,
uint32_t  y,
uint32_t  z 
) [inline, static, private]

Definition at line 374 of file md5.cpp.

Referenced by HH().

00374                                                              {
00375   return x ^ y ^ z;
00376 }

unsigned int MD5::I ( uint32_t  x,
uint32_t  y,
uint32_t  z 
) [inline, static, private]

Definition at line 378 of file md5.cpp.

Referenced by II().

00378                                                              {
00379   return y ^ (x | ~z);
00380 }

void MD5::FF ( uint32_t &  a,
uint32_t  b,
uint32_t  c,
uint32_t  d,
uint32_t  x,
uint32_t  s,
uint32_t  ac 
) [inline, static, private]

FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.

Rotation is separate from addition to prevent recomputation.

Definition at line 382 of file md5.cpp.

References F(), and rotate_left().

00382                                                                                                         {
00383   a += F(b, c, d) + x + ac;
00384   a = rotate_left (a, s) +b;
00385 }

void MD5::GG ( uint32_t &  a,
uint32_t  b,
uint32_t  c,
uint32_t  d,
uint32_t  x,
uint32_t  s,
uint32_t  ac 
) [inline, static, private]

Definition at line 387 of file md5.cpp.

References G(), and rotate_left().

00387                                                                                                         {
00388   a += G(b, c, d) + x + ac;
00389   a = rotate_left (a, s) +b;
00390 }

void MD5::HH ( uint32_t &  a,
uint32_t  b,
uint32_t  c,
uint32_t  d,
uint32_t  x,
uint32_t  s,
uint32_t  ac 
) [inline, static, private]

Definition at line 392 of file md5.cpp.

References H(), and rotate_left().

00392                                                                                                         {
00393   a += H(b, c, d) + x + ac;
00394   a = rotate_left (a, s) +b;
00395 }

void MD5::II ( uint32_t &  a,
uint32_t  b,
uint32_t  c,
uint32_t  d,
uint32_t  x,
uint32_t  s,
uint32_t  ac 
) [inline, static, private]

Definition at line 397 of file md5.cpp.

References I(), and rotate_left().

00397                                                                                                         {
00398   a += I(b, c, d) + x + ac;
00399   a = rotate_left (a, s) +b;
00400 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  stream,
MD5  context 
) [friend]

Definition at line 168 of file md5.cpp.

00168                                                         {
00169   stream << context.hex_digest();
00170   return stream;
00171 }


Member Data Documentation

uint32_t MD5::state[4] [private]

Definition at line 69 of file md5.hpp.

Referenced by finalize(), and init().

uint32_t MD5::count[2] [private]

number of _bits_, mod 2^64

Definition at line 70 of file md5.hpp.

Referenced by finalize(), and init().

uint8_t MD5::buffer[64] [private]

input buffer

Definition at line 71 of file md5.hpp.

Referenced by finalize(), and update().

uint8_t MD5::digest[16] [private]

Definition at line 72 of file md5.hpp.

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

bool MD5::finalized [private]

Definition at line 73 of file md5.hpp.

Referenced by finalize(), and init().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:32 2014 for SuperTux by  doxygen 1.5.1