scire
Sadh's C++ Impromptu Routines Ensemble
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scire::MD5< SzType, Ui32t, ByteT, SByteT > Class Template Reference

MD5 Message-Digest Algorithm. More...

#include <md5.hpp>

Inheritance diagram for scire::MD5< SzType, Ui32t, ByteT, SByteT >:
scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >

Public Types

enum  Info {
  DigestSize_bits = 128, DigestSize_bytes = 16, DigestSize_words = 4, BlockSize_bits = 512,
  BlockSize_bytes = 64, BlockSize_words = 16, Rounds = 4
}
 
enum  Magic { MagicWordA = 0x01234567, MagicWordB = 0x89abcdef, MagicWordC = 0xfedcba98, MagicWordD = 0X76543210 }
 

Public Member Functions

 MD5 ()
 allocate MD5 context and init More...
 
 MD5 (std::string message)
 Construct MD5 object with a string message. More...
 
 MD5 (char message[])
 Construct MD5 object with a string message. More...
 
void Init ()
 MD5 initialization. More...
 
bool Update (const ByteT *input, SzType lenght)
 MD5 block update operation. More...
 
bool Update (const SByteT *input, SzType lenght)
 MD5 block update operation. More...
 
void Final ()
 MD5 finalization. More...
 
bool Digest (ByteT digest[DigestSize_bytes]) const
 get digest bytes in More...
 
std::string Fingerprint () const
 fingerprint is the 128bit message-digest More...
 
std::string StatePhrase () const
 String representation of Four State Words (ABCD) More...
 
std::string ToString () const
 string reprsentation | fingerpring on finalize, else status words More...
 
void Reset ()
 Reset object, similar to doing Init() More...
 

Protected Member Functions

void transform (const ByteT block[BlockSize_bytes])
 the phenomenal transform function More...
 

Static Protected Member Functions

static Ui32t F (Ui32t x, Ui32t y, Ui32t z)
 F(X,Y,Z) = XY v not(X) Z. More...
 
static Ui32t G (Ui32t x, Ui32t y, Ui32t z)
 G(X,Y,Z) = XZ v Y not(Z) More...
 
static Ui32t H (Ui32t x, Ui32t y, Ui32t z)
 H(X,Y,Z) = X xor Y xor Z. More...
 
static Ui32t I (Ui32t x, Ui32t y, Ui32t z)
 I(X,Y,Z) = Y xor (X v not(Z)) More...
 

Related Functions

(Note that these are not member functions.)

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
std::ostream & operator<< (std::ostream &out, const MD5< SzType, Ui32t, ByteT, SByteT > &md5)
 

Detailed Description

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
class scire::MD5< SzType, Ui32t, ByteT, SByteT >

MD5 Message-Digest Algorithm.

scire adaptation of the MD5 algorithm, adopted from RFC 1321. http://tools.ietf.org/html/rfc1321

This implemenation is templatic to facilitate machine portability. It takes four <type> parameters:

  • SzType : size type, integer; default: size_t
  • Ui32t : unsigned 32bit integer, default: uint32_t of <cstdint>
  • ByteT : unsigned byte (8bit int), default: uint8_t of <cstdint>
  • SByteT : signed byte (8bit int), default: int8_t of <cstdint>

In most cases it will simply suffice to use MD5<>

Example usage:

MD5<> md5;
md5.Update(message, msglen); //call Update once or as many times necessary
md5.Final(); // digest is ready, any subsequent updates will be ignored
md5.Digest(digest); // fill 'digest' byte array with digest values

Client should create an MD5 object. The construcor calls Init inside it, so it is redundant to call Init() upor first use of the object. To compute MD5 checksum of a buffer, call Update() once or several times with chunks of the message. A call to Final() indicated MD5 object that, message is complete. Then, MD5 object computes finalized the digest. Client can receive digest bytes by passing a byte array to fill digest values with it. Digest size is 16 byte. After a call to Final(), all subsequent Update() calls will be ignored.
MD5 object can be reused by invoking Reset() and then using it in a similar fashion.

Definition at line 87 of file md5.hpp.

Member Enumeration Documentation

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
enum scire::MD5::Info
Enumerator
DigestSize_bits 

MD5 digest size in bits.

DigestSize_bytes 

MD5 digest size in bytes.

DigestSize_words 

MD5 digest size in words (32bit)

BlockSize_bits 

MD5 process message in chunks each of 512bit.

BlockSize_bytes 

block size in bytes

BlockSize_words 

block size in words (32bit)

Rounds 

MD5 has 4 rounds.

Definition at line 94 of file md5.hpp.

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
enum scire::MD5::Magic
Enumerator
MagicWordA 

0x01234567

MagicWordB 

0x89abcdef

MagicWordC 

0xfedcba98

MagicWordD 

0X76543210

Definition at line 103 of file md5.hpp.

Constructor & Destructor Documentation

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
scire::MD5< SzType, Ui32t, ByteT, SByteT >::MD5 ( )

allocate MD5 context and init

Definition at line 327 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
scire::MD5< SzType, Ui32t, ByteT, SByteT >::MD5 ( std::string  message)

Construct MD5 object with a string message.

This will cause full stack of the algorithm to execute. Hence, Initi(), Update() and Final() shall be called within and the object is ready to yield digest.

Definition at line 346 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
scire::MD5< SzType, Ui32t, ByteT, SByteT >::MD5 ( char  message[])

Construct MD5 object with a string message.

This will cause full stack of the algorithm to execute. Hence, Initi(), Update() and Final() shall be called within and the object is ready to yield digest.

Definition at line 336 of file md5.hpp.

Member Function Documentation

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
bool scire::MD5< SzType, Ui32t, ByteT, SByteT >::Digest ( ByteT  digest[DigestSize_bytes]) const

get digest bytes in

Returns
false if Final has yet not been called
Parameters
digesta 16 byte array to read digest values in

Definition at line 412 of file md5.hpp.

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
static Ui32t scire::MD5< SzType, Ui32t, ByteT, SByteT >::F ( Ui32t  x,
Ui32t  y,
Ui32t  z 
)
inlinestaticprotected

F(X,Y,Z) = XY v not(X) Z.

from RFC 1321:
In each bit position F acts as a conditional: if X then Y else Z. The function F could have been defined using + instead of v since XY and not(X)Z will never have 1's in the same bit position.) It is interesting to note that if the bits of X, Y, and Z are independent and unbiased, the each bit of F(X,Y,Z) will be independent and unbiased.

Definition at line 198 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
void scire::MD5< SzType, Ui32t, ByteT, SByteT >::Final ( )
virtual

MD5 finalization.

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

Implements scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >.

Definition at line 471 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
std::string scire::MD5< SzType, Ui32t, ByteT, SByteT >::Fingerprint ( ) const
virtual

fingerprint is the 128bit message-digest

Implements scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >.

Definition at line 360 of file md5.hpp.

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
static Ui32t scire::MD5< SzType, Ui32t, ByteT, SByteT >::G ( Ui32t  x,
Ui32t  y,
Ui32t  z 
)
inlinestaticprotected

G(X,Y,Z) = XZ v Y not(Z)

from RFC 1321:
The functions G, H, and I are similar to the function F, in that they act in "bitwise parallel" to produce their output from the bits of X, Y, and Z, in such a manner that if the corresponding bits of X, Y, and Z are independent and unbiased, then each bit of G(X,Y,Z), H(X,Y,Z), and I(X,Y,Z) will be independent and unbiased. Note that the function H is the bit-wise "xor" or "parity" function of its inputs.

See also
MD5::F

Definition at line 215 of file md5.hpp.

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
static Ui32t scire::MD5< SzType, Ui32t, ByteT, SByteT >::H ( Ui32t  x,
Ui32t  y,
Ui32t  z 
)
inlinestaticprotected

H(X,Y,Z) = X xor Y xor Z.

See also
MD5::G

Definition at line 220 of file md5.hpp.

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
static Ui32t scire::MD5< SzType, Ui32t, ByteT, SByteT >::I ( Ui32t  x,
Ui32t  y,
Ui32t  z 
)
inlinestaticprotected

I(X,Y,Z) = Y xor (X v not(Z))

See also
MD5::G

Definition at line 225 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
void scire::MD5< SzType, Ui32t, ByteT, SByteT >::Init ( )
virtual

MD5 initialization.

Begins an MD5 operation, writing a new context (i.e. init class object).

Implements scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >.

Definition at line 396 of file md5.hpp.

template<typename SzType = size_t, typename Ui32t = uint32_t, typename ByteT = uint8_t, typename SByteT = int8_t>
void scire::MD5< SzType, Ui32t, ByteT, SByteT >::Reset ( )
inline

Reset object, similar to doing Init()

Definition at line 180 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
std::string scire::MD5< SzType, Ui32t, ByteT, SByteT >::StatePhrase ( ) const
virtual

String representation of Four State Words (ABCD)

Implements scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >.

Definition at line 372 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
std::string scire::MD5< SzType, Ui32t, ByteT, SByteT >::ToString ( ) const
virtual

string reprsentation | fingerpring on finalize, else status words

Implements scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >.

Definition at line 385 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
void scire::MD5< SzType, Ui32t, ByteT, SByteT >::transform ( const ByteT  block[BlockSize_bytes])
protected

the phenomenal transform function

Definition at line 507 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
bool scire::MD5< SzType, Ui32t, ByteT, SByteT >::Update ( const ByteT *  input,
SzType  lenght 
)
virtual

MD5 block update operation.

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

Parameters
inputan array of bytes
lenghtnumber of bytes in input chunk

Implements scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >.

Definition at line 434 of file md5.hpp.

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
bool scire::MD5< SzType, Ui32t, ByteT, SByteT >::Update ( const SByteT *  input,
SzType  lenght 
)
virtual

MD5 block update operation.

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

Parameters
inputan array of bytes
lenghtnumber of bytes in input chunk

Implements scire::IHashAlgo< SzType, Ui32t, ByteT, SByteT >.

Definition at line 426 of file md5.hpp.

Friends And Related Function Documentation

template<typename SzType , typename Ui32t , typename ByteT , typename SByteT >
std::ostream & operator<< ( std::ostream &  out,
const MD5< SzType, Ui32t, ByteT, SByteT > &  md5 
)
related

if MD5 finalized then output digest, else current state words to ostream

Definition at line 315 of file md5.hpp.


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