Advertisement
eao197

md5_bruteforce solution with SObjectizer-5.5.4 (TheDeemon)

Apr 9th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  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 process_range : public so_5::rt::message_t
  42. {
  43.     const password m_start;
  44.     const password::value_type m_end;
  45.  
  46.     process_range( const password & start, const password::value_type end )
  47.         :   m_start( start )
  48.         ,   m_end( end )
  49.     {}
  50. };
  51.  
  52. struct found : public so_5::rt::message_t
  53. {
  54.     const password m_result;
  55.  
  56.     found( const password & result ) : m_result( result ) {}
  57. };
  58.  
  59. void do_brute_force( so_5::rt::environment_t & env )
  60. {
  61.     const auto hash = md5_cpp11::from_hex_string(
  62.             "95ebc3c7b3b9f1d2c40fec14415d3cb8" );
  63.  
  64.     using namespace so_5::disp::adv_thread_pool;
  65.  
  66.     auto coop = env.create_coop( so_5::autoname,
  67.             create_private_disp( env, thread::hardware_concurrency() )->binder(
  68.                     params_t{}.fifo( fifo_t::individual ) ) );
  69.  
  70.     const auto channel = env.create_local_mbox();
  71.  
  72.     // Worker.
  73.     coop->define_agent()
  74.         .on_start( [channel]() {
  75.                 // Create and send all ranges at once.
  76.                 password start; start.fill( '0' );
  77.                 do {
  78.                     auto end = next_byte( start[ 0 ] );
  79.                     so_5::send< process_range >( channel, start, end );
  80.                     start[ 0 ] = end;
  81.                 } while( start[ 0 ] != '0' );
  82.             } )
  83.         .event( channel,
  84.             [channel, hash] ( const process_range & evt ) {
  85.                 auto pw = evt.m_start;
  86.                 while( pw[ 0 ] != evt.m_end && md5_cpp11::make_digest( pw ) != hash )
  87.                     next_pass( pw );
  88.  
  89.                 if( pw[ 0 ] != evt.m_end )
  90.                     so_5::send< found >( channel, pw );
  91.             },
  92.             so_5::thread_safe );
  93.  
  94.     // Result printer.
  95.     coop->define_agent( so_5::rt::create_default_disp_binder() )
  96.             .event( channel, [&env]( const found & evt ) {
  97.                 cout << "password: " << evt.m_result << endl;
  98.                 env.stop();
  99.             } );
  100.  
  101.     env.register_coop( move( coop ) );
  102. }
  103.  
  104. double time_spent( const steady_clock::time_point s )
  105. {
  106.     const auto e = steady_clock::now();
  107.     return duration_cast< milliseconds >( e - s ).count() / 1000.0;
  108. }
  109.  
  110. int main()
  111. {
  112.     try
  113.     {
  114.         const auto started_at = steady_clock::now();
  115.  
  116.         so_5::launch( do_brute_force );
  117.  
  118.         cout << "time: " << time_spent( started_at ) << "s" << endl;
  119.  
  120.         return 0;
  121.     }
  122.     catch( const exception & x )
  123.     {
  124.         cerr << "Exception: " << x.what() << endl;
  125.     }
  126.  
  127.     return 2;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement