Advertisement
Guest User

Testing randomness of MD5 hashes

a guest
Sep 29th, 2010
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. // Need this to remove warnings while using MD5
  5. #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
  6.  
  7. #include "rng.h"
  8. #include "md5.h"
  9.  
  10. using namespace std;
  11. using namespace CryptoPP;
  12.  
  13. int main( int argc, char** argv )
  14. {
  15.     if( argc < 2 )
  16.     {
  17.         cout << "Usage: ./program [iterations]";
  18.         return 0;
  19.     }
  20.  
  21.     int runs = atoi( argv[1] );
  22.  
  23.     MaurerRandomnessTest mt;
  24.     Weak::MD5 md5;
  25.  
  26.     byte* digest = new byte[16];
  27.     byte* message = new byte[4];
  28.  
  29.     stringstream first, middle, last;
  30.    
  31.     for( int i = 0; i < runs; ++i )
  32.     {
  33.         // Convert integer to byte array.
  34.         for( int j = 0; j < 4; ++j )
  35.             message[j] = (byte) ( ( i >> ( j * 8 ) ) & 0x000000FF );
  36.        
  37.         // Feed MD5 with our message (iteration count) and receive our digest.
  38.         md5.Update( message, 4 );
  39.         md5.Final( digest );
  40.  
  41.  
  42.     // Split the digest into three portions. 8 Bytes samples from the beginning,
  43.     // the middle, and the end, respectively.
  44.         for( int j = 0; j <= 7; j++ )
  45.             first << digest[j];
  46.        
  47.         for( int j = 4; j <= 11; j++ )
  48.             middle << digest[j];
  49.        
  50.         for( int j = 8; j <= 15; j++ )
  51.             last << digest[j];
  52.     }
  53.  
  54.     StringStore str( first.str() );
  55.     str.TransferAllTo( mt );
  56.  
  57.     cout << "First: ";
  58.     if( mt.BytesNeeded() > 0 )
  59.         cout << "Not enough data.";
  60.     else
  61.         cout << mt.GetTestValue();
  62.     cout << endl;
  63.  
  64.     str = StringStore( middle.str() );
  65.     str.TransferAllTo( mt );
  66.  
  67.     cout << "Middle: ";
  68.     if( mt.BytesNeeded() > 0 )
  69.         cout << "Not enough data.";
  70.     else
  71.         cout << mt.GetTestValue();
  72.     cout << endl;
  73.  
  74.     str = StringStore( last.str() );
  75.     str.TransferAllTo( mt );
  76.  
  77.     cout << "Last: ";
  78.     if( mt.BytesNeeded() > 0 )
  79.         cout << "Not enough data.";
  80.     else
  81.         cout << mt.GetTestValue();
  82.     cout << endl;
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement