PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
MD5 Class Reference

#include <md5.h>

Public Types

typedef unsigned int size_type
 

Public Member Functions

 MD5 ()
 
 MD5 (const std::string &text)
 
void update (const unsigned char *buf, size_type length)
 
void update (const char *buf, size_type length)
 
MD5finalize ()
 
std::string hexdigest () const
 

Friends

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

Detailed Description

Definition at line 53 of file md5.h.

Member Typedef Documentation

◆ size_type

typedef unsigned int MD5::size_type

Definition at line 56 of file md5.h.

Constructor & Destructor Documentation

◆ MD5() [1/2]

MD5::MD5 ( )

Definition at line 105 of file md5.cpp.

106{
107 init();
108}

◆ MD5() [2/2]

MD5::MD5 ( const std::string &  text)

Definition at line 113 of file md5.cpp.

114{
115 init();
116 update(text.c_str(), (unsigned int)(text.length()));
117 finalize();
118}
MD5 & finalize()
Definition md5.cpp:301
void update(const unsigned char *buf, size_type length)

References finalize(), and update().

Member Function Documentation

◆ finalize()

MD5 & MD5::finalize ( )

Definition at line 301 of file md5.cpp.

302{
303 static unsigned char padding[64] = {
304 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
305 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
307 };
308
309 if (!finalized) {
310 // Save number of bits
311 unsigned char bits[8];
312 encode(bits, count, 8);
313
314 // pad out to 56 mod 64.
315 size_type index = count[0] / 8 % 64;
316 size_type padLen = (index < 56) ? (56 - index) : (120 - index);
317 update(padding, padLen);
318
319 // Append length (before padding)
320 update(bits, 8);
321
322 // Store state in digest
323 encode(digest, state, 16);
324
325 // Zeroize sensitive information.
326 memset(buffer, 0, sizeof buffer);
327 memset(count, 0, sizeof count);
328
329 finalized=true;
330 }
331
332 return *this;
333}
unsigned int size_type
Definition md5.h:56

References update().

Referenced by MD5().

◆ hexdigest()

std::string MD5::hexdigest ( ) const

Definition at line 338 of file md5.cpp.

339{
340 if (!finalized)
341 return "";
342
343 char buf[33];
344 for (int i=0; i<16; i++)
345 sprintf(buf+i*2, "%02x", digest[i]);
346 buf[32]=0;
347
348 return std::string(buf);
349}

◆ update() [1/2]

void MD5::update ( const char *  buf,
size_type  length 
)

◆ update() [2/2]

void MD5::update ( const unsigned char *  buf,
size_type  length 
)

Referenced by finalize(), and MD5().

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  out,
MD5  md5 
)
friend

Definition at line 353 of file md5.cpp.

354{
355 return out << md5.hexdigest();
356}
std::string md5(const std::string str)
Definition md5.cpp:360

The documentation for this class was generated from the following files: