eao197

md5_bruteforce solution with SObjectizer-5.5.4 (SepAgents)

Apr 9th, 2015
237
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <chrono>
  3. #include <array>
  4. #include <iterator>
  5.  
  6. #include <so_5/all.hpp>
  7.  
  8. #include <md5_cpp11/all.hpp>
  9.  
  10. using namespace std;
  11. using namespace std::chrono;
  12.  
  13. using password = array< char, 5 >;
  14.  
  15. ostream & operator<<( ostream & to, const password & pw )
  16. {
  17.     copy( begin(pw), end(pw), ostream_iterator< password::value_type >(to) );
  18.     return to;
  19. }
  20.  
  21. password::value_type next_byte( password::value_type b )
  22. {
  23.     switch( b )
  24.     {
  25.         case 'z' : return '0';
  26.         case '9' : return 'a';
  27.         default : return b + 1;
  28.     }
  29. }
  30.  
  31. void next_pass( password & p )
  32. {
  33.     for( auto e = p.rbegin(); e != p.rend(); ++e )
  34.     {
  35.         *e = next_byte( *e );
  36.         if( '0' != *e )
  37.             break;
  38.     }
  39. }
  40.  
  41. struct found : public so_5::rt::message_t
  42. {
  43.     const password m_result;
  44.  
  45.     found( const password & result ) : m_result( result ) {}
  46. };
  47.  
  48. void do_brute_force( so_5::rt::environment_t & env )
  49. {
  50.     const auto hash = md5_cpp11::from_hex_string(
  51.             "95ebc3c7b3b9f1d2c40fec14415d3cb8" );
  52.  
  53.     using namespace so_5::disp::thread_pool;
  54.  
  55.     auto coop = env.create_coop( so_5::autoname,
  56.             create_private_disp( env, thread::hardware_concurrency() )->binder(
  57.                     params_t{}.fifo( fifo_t::individual ) ) );
  58.  
  59.     const auto channel = env.create_local_mbox();
  60.  
  61.     // Result printer.
  62.     coop->define_agent( so_5::rt::create_default_disp_binder() )
  63.             .event( channel, [&env]( const found & evt ) {
  64.                 cout << "password: " << evt.m_result << endl;
  65.                 env.stop();
  66.             } );
  67.  
  68.     // Workers.
  69.     // Create a separate worker for every range.
  70.     password start; start.fill( '0' );
  71.     do {
  72.         auto end = next_byte( start[ 0 ] );
  73.         coop->define_agent().on_start( [channel, hash, start, end] {
  74.                 auto pw = start;
  75.                 while( pw[ 0 ] != end && md5_cpp11::make_digest( pw ) != hash )
  76.                     next_pass( pw );
  77.  
  78.                 if( pw[ 0 ] != end )
  79.                     so_5::send< found >( channel, pw );
  80.             } );
  81.         start[ 0 ] = end;
  82.     } while( start[ 0 ] != '0' );
  83.  
  84.     env.register_coop( move( coop ) );
  85. }
  86.  
  87. double time_spent( const steady_clock::time_point s )
  88. {
  89.     const auto e = steady_clock::now();
  90.     return duration_cast< milliseconds >( e - s ).count() / 1000.0;
  91. }
  92.  
  93. int main()
  94. {
  95.     try
  96.     {
  97.         const auto started_at = steady_clock::now();
  98.  
  99.         so_5::launch( do_brute_force );
  100.  
  101.         cout << "time: " << time_spent( started_at ) << "s" << endl;
  102.  
  103.         return 0;
  104.     }
  105.     catch( const exception & x )
  106.     {
  107.         cerr << "Exception: " << x.what() << endl;
  108.     }
  109.  
  110.     return 2;
  111. }
RAW Paste Data