#include <md5.hpp>
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) |
Definition at line 50 of file md5.hpp.
MD5::MD5 | ( | ) |
MD5::MD5 | ( | uint8_t * | string | ) |
digest string, finalize
MD5::MD5 | ( | std::istream & | stream | ) |
MD5::MD5 | ( | FILE * | file | ) |
MD5::MD5 | ( | std::ifstream & | stream | ) |
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 | ) |
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 | ( | ) |
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] |
void MD5::memset | ( | uint8_t * | start, | |
uint8_t | val, | |||
uint32_t | length | |||
) | [static, private] |
unsigned int MD5::rotate_left | ( | uint32_t | x, | |
uint32_t | n | |||
) | [inline, static, private] |
unsigned int MD5::F | ( | uint32_t | x, | |
uint32_t | y, | |||
uint32_t | z | |||
) | [inline, static, private] |
unsigned int MD5::G | ( | uint32_t | x, | |
uint32_t | y, | |||
uint32_t | z | |||
) | [inline, static, private] |
unsigned int MD5::H | ( | uint32_t | x, | |
uint32_t | y, | |||
uint32_t | z | |||
) | [inline, static, private] |
unsigned int MD5::I | ( | uint32_t | x, | |
uint32_t | y, | |||
uint32_t | z | |||
) | [inline, static, private] |
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 }
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 }
uint32_t MD5::state[4] [private] |
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] |
uint8_t MD5::digest[16] [private] |
bool MD5::finalized [private] |