31 typedef G::Md5::small_t small_t ;
32 typedef G::Md5::big_t big_t ;
53 explicit digest(
const std::string & s ) ;
69 void add(
const block & ) ;
73 typedef big_t (*aux_fn_t)( big_t , big_t , big_t ) ;
74 enum Permutation { ABCD , DABC , CDAB , BCDA } ;
78 void add(
const digest & ) ;
80 void calculate(
const block & ) ;
81 static big_t T( small_t i ) ;
82 static big_t rot32( small_t places , big_t n ) ;
83 void operator()(
const block & , aux_fn_t , Permutation , small_t , small_t , small_t ) ;
84 static big_t op(
const block & , aux_fn_t , big_t , big_t , big_t , big_t , small_t , small_t , small_t ) ;
85 void round1(
const block & ) ;
86 void round2(
const block & ) ;
87 void round3(
const block & ) ;
88 void round4(
const block & ) ;
89 static big_t F( big_t x , big_t y , big_t z ) ;
90 static big_t
G( big_t x , big_t y , big_t z ) ;
91 static big_t H( big_t x , big_t y , big_t z ) ;
92 static big_t I( big_t x , big_t y , big_t z ) ;
105 static std::string encode(
const digest_state & , big_t n ) ;
109 static digest_state decode(
const std::string & , small_t & ) ;
124 block(
const std::string & s , small_t block_offset , big_t end_value ) ;
140 static big_t end( small_t data_length ) ;
146 static small_t blocks( small_t data_length ) ;
152 big_t X( small_t )
const ;
157 void operator=(
const block & ) ;
158 small_t x( small_t )
const ;
159 static small_t rounded( small_t n ) ;
162 const std::string & m_s ;
169 md5::digest::digest()
174 md5::digest::digest(
const std::string & s )
177 small_t n = block::blocks( s.length() ) ;
178 for( small_t i = 0U ; i < n ; ++i )
180 block blk( s , i , block::end(s.length()) ) ;
185 md5::digest::digest( digest_state d_in )
193 void md5::digest::init()
201 md5::digest::digest_state md5::digest::state()
const
204 small_t thirty_two = 32U ;
205 small_t sizeof_thirty_two_bits = 4U ;
206 if(
sizeof(mask) > sizeof_thirty_two_bits )
209 mask <<= thirty_two ;
211 digest_state result = { a & ~mask , b & ~mask , c & ~mask , d & ~mask } ;
215 void md5::digest::add(
const block & blk )
217 digest old( *
this ) ;
225 void md5::digest::add(
const digest & other )
233 void md5::digest::round1(
const block & m )
236 r(m,F,ABCD, 0, 7, 1); r(m,F,DABC, 1,12, 2); r(m,F,CDAB, 2,17, 3); r(m,F,BCDA, 3,22, 4);
237 r(m,F,ABCD, 4, 7, 5); r(m,F,DABC, 5,12, 6); r(m,F,CDAB, 6,17, 7); r(m,F,BCDA, 7,22, 8);
238 r(m,F,ABCD, 8, 7, 9); r(m,F,DABC, 9,12,10); r(m,F,CDAB,10,17,11); r(m,F,BCDA,11,22,12);
239 r(m,F,ABCD,12, 7,13); r(m,F,DABC,13,12,14); r(m,F,CDAB,14,17,15); r(m,F,BCDA,15,22,16);
242 void md5::digest::round2(
const block & m )
245 r(m,
G,ABCD, 1, 5,17); r(m,
G,DABC, 6, 9,18); r(m,
G,CDAB,11,14,19); r(m,
G,BCDA, 0,20,20);
246 r(m,
G,ABCD, 5, 5,21); r(m,
G,DABC,10, 9,22); r(m,
G,CDAB,15,14,23); r(m,
G,BCDA, 4,20,24);
247 r(m,
G,ABCD, 9, 5,25); r(m,
G,DABC,14, 9,26); r(m,
G,CDAB, 3,14,27); r(m,
G,BCDA, 8,20,28);
248 r(m,
G,ABCD,13, 5,29); r(m,
G,DABC, 2, 9,30); r(m,
G,CDAB, 7,14,31); r(m,
G,BCDA,12,20,32);
251 void md5::digest::round3(
const block & m )
254 r(m,H,ABCD, 5, 4,33); r(m,H,DABC, 8,11,34); r(m,H,CDAB,11,16,35); r(m,H,BCDA,14,23,36);
255 r(m,H,ABCD, 1, 4,37); r(m,H,DABC, 4,11,38); r(m,H,CDAB, 7,16,39); r(m,H,BCDA,10,23,40);
256 r(m,H,ABCD,13, 4,41); r(m,H,DABC, 0,11,42); r(m,H,CDAB, 3,16,43); r(m,H,BCDA, 6,23,44);
257 r(m,H,ABCD, 9, 4,45); r(m,H,DABC,12,11,46); r(m,H,CDAB,15,16,47); r(m,H,BCDA, 2,23,48);
260 void md5::digest::round4(
const block & m )
263 r(m,I,ABCD, 0, 6,49); r(m,I,DABC, 7,10,50); r(m,I,CDAB,14,15,51); r(m,I,BCDA, 5,21,52);
264 r(m,I,ABCD,12, 6,53); r(m,I,DABC, 3,10,54); r(m,I,CDAB,10,15,55); r(m,I,BCDA, 1,21,56);
265 r(m,I,ABCD, 8, 6,57); r(m,I,DABC,15,10,58); r(m,I,CDAB, 6,15,59); r(m,I,BCDA,13,21,60);
266 r(m,I,ABCD, 4, 6,61); r(m,I,DABC,11,10,62); r(m,I,CDAB, 2,15,63); r(m,I,BCDA, 9,21,64);
269 void md5::digest::operator()(
const block & m , aux_fn_t aux , Permutation p , small_t k , small_t s , small_t i )
271 if( p == ABCD ) a = op( m , aux , a , b , c , d , k , s , i ) ;
272 if( p == DABC ) d = op( m , aux , d , a , b , c , k , s , i ) ;
273 if( p == CDAB ) c = op( m , aux , c , d , a , b , k , s , i ) ;
274 if( p == BCDA ) b = op( m , aux , b , c , d , a , k , s , i ) ;
277 md5::big_t md5::digest::op(
const block & m , aux_fn_t aux , big_t a , big_t b , big_t c , big_t d ,
278 small_t k , small_t s , small_t i )
280 return b + rot32( s , ( a + (*aux)( b , c , d ) + m.X(k) + T(i) ) ) ;
283 md5::big_t md5::digest::rot32( small_t places , big_t n )
286 big_t overflow_mask = ( 1UL << places ) - 1UL ;
287 big_t overflow = ( n >> ( 32U - places ) ) ;
288 return ( n << places ) | ( overflow & overflow_mask ) ;
291 md5::big_t md5::digest::F( big_t x , big_t y , big_t z )
293 return ( x & y ) | ( ~x & z ) ;
296 md5::big_t md5::digest::G( big_t x , big_t y , big_t z )
298 return ( x & z ) | ( y & ~z ) ;
301 md5::big_t md5::digest::H( big_t x , big_t y , big_t z )
306 md5::big_t md5::digest::I( big_t x , big_t y , big_t z )
308 return y ^ ( x | ~z ) ;
311 md5::big_t md5::digest::T( small_t i )
315 static big_t t_map[] =
381 return t_map[i-1UL] ;
386 std::string md5::format::encode(
const digest_state & state )
388 const big_t state_array[] = { state.a , state.b , state.c , state.d } ;
392 std::string md5::format::encode(
const digest_state & state , big_t n )
394 const big_t state_array[] = { state.a , state.b , state.c , state.d } ;
400 big_t state_array[] = { 0 , 0 , 0 , 0 } ;
402 digest_state result ;
403 result.a = state_array[0] ;
404 result.b = state_array[1] ;
405 result.c = state_array[2] ;
406 result.d = state_array[3] ;
412 md5::block::block(
const std::string & s , small_t block , big_t end_value ) :
415 m_end_value(end_value)
419 md5::big_t md5::block::end( small_t length )
421 big_t result = length ;
426 md5::small_t md5::block::rounded( small_t raw_byte_count )
428 small_t n = raw_byte_count + 64U ;
429 return n - ( ( raw_byte_count + 8U ) % 64U ) ;
432 md5::small_t md5::block::blocks( small_t raw_byte_count )
434 small_t byte_count = rounded(raw_byte_count) + 8U ;
435 return byte_count / 64UL ;
438 md5::big_t md5::block::X( small_t dword_index )
const
440 small_t byte_index = ( m_block * 64U ) + ( dword_index * 4U ) ;
441 big_t result = x( byte_index + 3U ) ;
442 result <<= 8U ; result += x( byte_index + 2U ) ;
443 result <<= 8U ; result += x( byte_index + 1U ) ;
444 result <<= 8U ; result += x( byte_index + 0U ) ;
448 md5::small_t md5::block::x( small_t i )
const
450 small_t length = m_s.length() ;
453 return static_cast<unsigned char>(m_s[i]) ;
455 else if( i < rounded(length) )
457 return i == length ? 128U : 0U ;
461 small_t byte_shift = i - rounded(length) ;
462 if( byte_shift >=
sizeof(big_t) )
468 small_t bit_shift = byte_shift * 8U ;
470 big_t end_value = m_end_value >> bit_shift ;
471 return static_cast<small_t
>( end_value & 0xffUL ) ;
487 G_ASSERT( str_state.size() == (valuesize()+4U) ) ;
488 m_d = md5::format::decode( str_state , m_n ) ;
493 G_ASSERT( md5::format::encode(m_d,m_n).size() == (valuesize()+4U) ) ;
494 return md5::format::encode( m_d , m_n ) ;
502 m_n += data.length() ;
503 while( m_s.length() >= 64U )
507 m_s.erase( 0U , 64U ) ;
515 md5::block blk( m_s , 0U , md5::block::end(m_n) ) ;
519 return md5::format::encode( m_d ) ;
525 return md5::format::encode( dd.state() ) ;
528 std::string
G::Md5::digest(
const std::string & input_1 ,
const std::string & input_2 )
536 std::string
G::Md5::digest2(
const std::string & input_1 ,
const std::string & input_2 )
538 return digest( input_1 , input_2 ) ;
545 G_ASSERT( input.size() == blocksize() ) ;
546 return x.
state().substr(0U,valuesize()) ;
551 std::string _64(
"\x40\0\0\0" , 4U ) ;
552 std::string state_i = state_pair.substr( 0U , state_pair.size()/2 ) + _64 ;
553 std::string state_o = state_pair.substr( state_pair.size()/2 ) + _64 ;
std::string state() const
Returns the current intermediate state as a 20 character string, although this requires the size of t...
void add(const std::string &data)
Adds more data.
static size_t blocksize()
Returns the block size in bytes (64).
static void decode(const std::string &, uint_type *, size_type &)
Converts an encode()d string back into a hash state of N integers and a data size returned by referen...
Md5()
Default constructor.
A helper class used by the md5::digest implementation to represent a 64-character data block...
std::string value()
Returns the hash value as a 16-character string.
static std::string postdigest(const std::string &state_pair, const std::string &message)
A convenience function that returns the value() from an outer digest that is initialised with the sec...
A class that calculates an md5 digest from one or more 64-byte blocks of data using the algorithm des...
static std::string digest(const std::string &input)
A convenience function that returns a digest from one input.
static std::string predigest(const std::string &padded_key)
A convenience function that add()s the given string of length blocksize() (typically a padded key) an...
static size_t valuesize()
Returns the value() size in bytes (16).
static std::string encode(const uint_type *)
Returns the hash state as a string, typically containing non-printing characters. ...
static size_t statesize()
Returns the size of the state() string (20).
static std::string digest2(const std::string &input_1, const std::string &input_2)
A non-overloaded name for the digest() overload taking two parameters.
MD5 message digest class.
Holds the four parts of the md5 state.