Advertisement
eao197

md5_bruteforce solution with SObjectizer-5.5.4

Apr 8th, 2015
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 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 m_end;
  45.  
  46.     process_range( const password & start, const password & 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. struct get_next_range : public so_5::rt::signal_t {};
  60.  
  61. void generate_next_range(
  62.     password & last,
  63.     const so_5::rt::mbox_t & channel )
  64. {
  65.     auto start = last;
  66.     last[ 0 ] = next_byte( last[ 0 ] );
  67.  
  68.     so_5::send< process_range >( channel, start, last );
  69. }
  70.  
  71. void worker(
  72.     const so_5::rt::mbox_t & channel,
  73.     const md5_cpp11::digest & etalon,
  74.     const process_range & evt )
  75. {
  76.     auto pw = evt.m_start;
  77.     while( pw != evt.m_end && md5_cpp11::make_digest( pw ) != etalon )
  78.         next_pass( pw );
  79.  
  80.     if( pw == evt.m_end )
  81.         so_5::send< get_next_range >( channel );
  82.     else
  83.         so_5::send< found >( channel, pw );
  84. }
  85.  
  86. void do_brute_force( so_5::rt::environment_t & env )
  87. {
  88.     const auto hash = md5_cpp11::from_hex_string(
  89.             "95ebc3c7b3b9f1d2c40fec14415d3cb8" ); // "zzzzz"
  90.  
  91.     using namespace so_5::disp::adv_thread_pool;
  92.  
  93.     auto workers = thread::hardware_concurrency();
  94.     auto coop = env.create_coop( so_5::autoname,
  95.             create_private_disp( env, workers )->binder(
  96.                     params_t{}.fifo( fifo_t::individual ) ) );
  97.  
  98.     const auto channel = env.create_local_mbox();
  99.  
  100.     // State for generator.
  101.     auto last = make_shared< password >();
  102.     last->fill( '0' );
  103.     // Generator itself.
  104.     coop->define_agent()
  105.         .on_start( [channel, workers] {
  106.                 for( size_t i = 0; i != workers; ++i )
  107.                     so_5::send< get_next_range >( channel );
  108.             } )
  109.         .event< get_next_range >( channel,
  110.             [last, channel] {
  111.                 generate_next_range( *last, channel );
  112.             } );
  113.  
  114.     // Worker.
  115.     coop->define_agent().event( channel,
  116.             [channel, hash]( const process_range & evt ) {
  117.                 worker( channel, hash, evt );
  118.             },
  119.             so_5::thread_safe );
  120.  
  121.     // Result printer.
  122.     coop->define_agent().event( channel,
  123.             [&env]( const found & evt ) {
  124.                 cout << "password: " << evt.m_result << endl;
  125.                 env.stop();
  126.             } );
  127.  
  128.     env.register_coop( move( coop ) );
  129. }
  130.  
  131. double time_spent( const steady_clock::time_point s )
  132. {
  133.     const auto e = steady_clock::now();
  134.     return duration_cast< milliseconds >( e - s ).count() / 1000.0;
  135. }
  136.  
  137. int main()
  138. {
  139.     try
  140.     {
  141.         const auto started_at = steady_clock::now();
  142.  
  143.         so_5::launch( do_brute_force );
  144.  
  145.         cout << "time: " << time_spent( started_at ) << "s" << endl;
  146.  
  147.         return 0;
  148.     }
  149.     catch( const exception & x )
  150.     {
  151.         cerr << "Exception: " << x.what() << endl;
  152.     }
  153.  
  154.     return 2;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement