Advertisement
ozh

testing phpass on PHP 5.2.x

ozh
Dec 30th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.23 KB | None | 0 0
  1. // testing on http://sandbox.onlinephpfunctions.com/
  2.  
  3. function yourls_phpass_instance( $iteration = 8, $portable = false ) {
  4.     $iteration = yourls_apply_filter( 'phpass_new_instance_iteration', $iteration );
  5.     $portable  = yourls_apply_filter( 'phpass_new_instance_portable', $portable );
  6.  
  7.     static $instance = false;
  8.     if( $instance == false ) {
  9.         $instance = new PasswordHash( $iteration, $portable );
  10.     }
  11.    
  12.     return $instance;
  13. }
  14.  
  15. function yourls_apply_filter( $input ) {
  16. return $input;
  17. }
  18.  
  19. function yourls_phpass_hash( $password ) {
  20.     $hasher = yourls_phpass_instance();
  21.     return $hasher->HashPassword( $password );
  22. }
  23.  
  24. function yourls_phpass_check( $password, $hash ) {
  25.     $hasher = yourls_phpass_instance();
  26.     return $hasher->CheckPassword( $password, $hash );
  27. }
  28.  
  29.  
  30. function yourls_check_password_hash( $user, $submitted_password ) {
  31.     global $yourls_user_passwords;
  32.  
  33. echo "******* internal BEGIN\n";
  34. var_dump( $user );
  35. var_dump( $submitted_password );
  36. var_dump( isset( $yourls_user_passwords[ $user ] ) );
  37. var_dump( yourls_has_phpass_password( $user ) );
  38. list( $null, $hash ) = explode( ':', $yourls_user_passwords[ $user ] );
  39. var_dump( $hash );
  40. $hash = str_replace( '!', '$', $hash );
  41. var_dump( $hash );
  42. var_dump( yourls_phpass_check( $submitted_password, $hash ) );
  43. echo "******* internal END\n\n";
  44.  
  45.    
  46.     if( !isset( $yourls_user_passwords[ $user ] ) )
  47.         return false;
  48.    
  49.     if ( yourls_has_phpass_password( $user ) ) {
  50.         // Stored password is hashed with phpass
  51.         list( $null, $hash ) = explode( ':', $yourls_user_passwords[ $user ] );
  52.         $hash = str_replace( '!', '$', $hash );
  53.         return ( yourls_phpass_check( $submitted_password, $hash ) );
  54.     } else if( yourls_has_md5_password( $user ) ) {
  55.         // Stored password is a salted md5 hash: "md5:<$r = rand(10000,99999)>:<md5($r.'thepassword')>"
  56.         list( $null, $salt, $null ) = explode( ':', $yourls_user_passwords[ $user ] );
  57.         return( $yourls_user_passwords[ $user ] == 'md5:'.$salt.':'.md5( $salt . $submitted_password ) );
  58.     } else {
  59.         // Password stored in clear text
  60.         return( $yourls_user_passwords[ $user ] == $submitted_password );
  61.     }
  62. }
  63.  
  64. function yourls_hash_passwords_now( $config_file ) {
  65.     if( !is_readable( $config_file ) )
  66.         return 'cannot read file'; // not sure that can actually happen...
  67.        
  68.     if( !is_writable( $config_file ) )
  69.         return 'cannot write file';
  70.    
  71.     // Include file to read value of $yourls_user_passwords
  72.     // Temporary suppress error reporting to avoid notices about redeclared constants
  73.     $errlevel = error_reporting();
  74.     error_reporting( 0 );
  75.     require $config_file;
  76.     error_reporting( $errlevel );
  77.    
  78.     $configdata = file_get_contents( $config_file );
  79.     if( $configdata == false )
  80.         return 'could not read file';
  81.  
  82.     $to_hash = 0; // keep track of number of passwords that need hashing
  83.     foreach ( $yourls_user_passwords as $user => $password ) {
  84.         if ( !yourls_has_phpass_password( $user ) && !yourls_has_md5_password( $user ) ) {
  85.             $to_hash++;
  86.             $hash = yourls_phpass_hash( $password );
  87.             // PHP would interpret $ as a variable, so replace it in storage.
  88.             $hash = str_replace( '$', '!', $hash );
  89.             $quotes = "'" . '"';
  90.             $pattern = "/[$quotes]${user}[$quotes]\s*=>\s*[$quotes]" . preg_quote( $password, '-' ) . "[$quotes]/";
  91.             $replace = "'$user' => 'phpass:$hash' /* Password encrypted by YOURLS */ ";
  92.             $count = 0;
  93.             $configdata = preg_replace( $pattern, $replace, $configdata, -1, $count );
  94.             // There should be exactly one replacement. Otherwise, fast fail.
  95.             if ( $count != 1 ) {
  96.                 yourls_debug_log( "Problem with preg_replace for password hash of user $user" );
  97.                 return 'preg_replace problem';
  98.             }
  99.         }
  100.     }
  101.    
  102.     if( $to_hash == 0 )
  103.         return 0; // There was no password to encrypt
  104.    
  105.     $success = file_put_contents( $config_file, $configdata );
  106.     if ( $success === FALSE ) {
  107.         yourls_debug_log( 'Failed writing to ' . $config_file );
  108.         return 'could not write file';
  109.     }
  110.     return true;
  111. }
  112.  
  113. function yourls_has_phpass_password( $user ) {
  114.     global $yourls_user_passwords;
  115.     return( isset( $yourls_user_passwords[ $user ] )
  116.             && substr( $yourls_user_passwords[ $user ], 0, 7 ) == 'phpass:'
  117.     );
  118. }
  119.  
  120. $yourls_user_passwords = array(
  121.     'yourls'  => 'travis-ci-test',
  122.     'clear'   => 'somepassword',
  123.     'md5'     => 'md5:31712:f6cae1f032b9ae81b233866f4aa791af', // password: "md5"
  124.     'phpass'  => 'phpass:$2a$08$UbOIKE2oyh.shrjSkOJ3Au7zN2vqTkrhsmAFgaMPomfeS0S6xHjG6', // password: "phpass"
  125.     'phpass2' => 'phpass:!2a!08$zzwkOxZHwup7qsfSuxdFXOzRBEOtKu4b15gXqceYJ23GOJtRq.yvO', // password: also "phpass" with YOURLS' internal char substitution
  126. );
  127.  
  128. class PasswordHash {
  129.     var $itoa64;
  130.     var $iteration_count_log2;
  131.     var $portable_hashes;
  132.     var $random_state;
  133.  
  134.     function PasswordHash($iteration_count_log2, $portable_hashes)
  135.     {
  136.         $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  137.  
  138.         if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
  139.             $iteration_count_log2 = 8;
  140.         $this->iteration_count_log2 = $iteration_count_log2;
  141.  
  142.         $this->portable_hashes = $portable_hashes;
  143.  
  144.         $this->random_state = microtime();
  145.         if (function_exists('getmypid'))
  146.             $this->random_state .= getmypid();
  147.     }
  148.  
  149.     function get_random_bytes($count)
  150.     {
  151.         $output = '';
  152.         if (is_readable('/dev/urandom') &&
  153.             ($fh = @fopen('/dev/urandom', 'rb'))) {
  154.             $output = fread($fh, $count);
  155.             fclose($fh);
  156.         }
  157.  
  158.         if (strlen($output) < $count) {
  159.             $output = '';
  160.             for ($i = 0; $i < $count; $i += 16) {
  161.                 $this->random_state =
  162.                     md5(microtime() . $this->random_state);
  163.                 $output .=
  164.                     pack('H*', md5($this->random_state));
  165.             }
  166.             $output = substr($output, 0, $count);
  167.         }
  168.  
  169.         return $output;
  170.     }
  171.  
  172.     function encode64($input, $count)
  173.     {
  174.         $output = '';
  175.         $i = 0;
  176.         do {
  177.             $value = ord($input[$i++]);
  178.             $output .= $this->itoa64[$value & 0x3f];
  179.             if ($i < $count)
  180.                 $value |= ord($input[$i]) << 8;
  181.             $output .= $this->itoa64[($value >> 6) & 0x3f];
  182.             if ($i++ >= $count)
  183.                 break;
  184.             if ($i < $count)
  185.                 $value |= ord($input[$i]) << 16;
  186.             $output .= $this->itoa64[($value >> 12) & 0x3f];
  187.             if ($i++ >= $count)
  188.                 break;
  189.             $output .= $this->itoa64[($value >> 18) & 0x3f];
  190.         } while ($i < $count);
  191.  
  192.         return $output;
  193.     }
  194.  
  195.     function gensalt_private($input)
  196.     {
  197.         $output = '$P$';
  198.         $output .= $this->itoa64[min($this->iteration_count_log2 +
  199.             ((PHP_VERSION >= '5') ? 5 : 3), 30)];
  200.         $output .= $this->encode64($input, 6);
  201.  
  202.         return $output;
  203.     }
  204.  
  205.     function crypt_private($password, $setting)
  206.     {
  207.         $output = '*0';
  208.         if (substr($setting, 0, 2) == $output)
  209.             $output = '*1';
  210.  
  211.         $id = substr($setting, 0, 3);
  212.         # We use "$P$", phpBB3 uses "$H$" for the same thing
  213.         if ($id != '$P$' && $id != '$H$')
  214.             return $output;
  215.  
  216.         $count_log2 = strpos($this->itoa64, $setting[3]);
  217.         if ($count_log2 < 7 || $count_log2 > 30)
  218.             return $output;
  219.  
  220.         $count = 1 << $count_log2;
  221.  
  222.         $salt = substr($setting, 4, 8);
  223.         if (strlen($salt) != 8)
  224.             return $output;
  225.  
  226.         # We're kind of forced to use MD5 here since it's the only
  227.         # cryptographic primitive available in all versions of PHP
  228.         # currently in use.  To implement our own low-level crypto
  229.         # in PHP would result in much worse performance and
  230.         # consequently in lower iteration counts and hashes that are
  231.         # quicker to crack (by non-PHP code).
  232.         if (PHP_VERSION >= '5') {
  233.             $hash = md5($salt . $password, TRUE);
  234.             do {
  235.                 $hash = md5($hash . $password, TRUE);
  236.             } while (--$count);
  237.         } else {
  238.             $hash = pack('H*', md5($salt . $password));
  239.             do {
  240.                 $hash = pack('H*', md5($hash . $password));
  241.             } while (--$count);
  242.         }
  243.  
  244.         $output = substr($setting, 0, 12);
  245.         $output .= $this->encode64($hash, 16);
  246.  
  247.         return $output;
  248.     }
  249.  
  250.     function gensalt_extended($input)
  251.     {
  252.         $count_log2 = min($this->iteration_count_log2 + 8, 24);
  253.         # This should be odd to not reveal weak DES keys, and the
  254.         # maximum valid value is (2**24 - 1) which is odd anyway.
  255.         $count = (1 << $count_log2) - 1;
  256.  
  257.         $output = '_';
  258.         $output .= $this->itoa64[$count & 0x3f];
  259.         $output .= $this->itoa64[($count >> 6) & 0x3f];
  260.         $output .= $this->itoa64[($count >> 12) & 0x3f];
  261.         $output .= $this->itoa64[($count >> 18) & 0x3f];
  262.  
  263.         $output .= $this->encode64($input, 3);
  264.  
  265.         return $output;
  266.     }
  267.  
  268.     function gensalt_blowfish($input)
  269.     {
  270.         # This one needs to use a different order of characters and a
  271.         # different encoding scheme from the one in encode64() above.
  272.         # We care because the last character in our encoded string will
  273.         # only represent 2 bits.  While two known implementations of
  274.         # bcrypt will happily accept and correct a salt string which
  275.         # has the 4 unused bits set to non-zero, we do not want to take
  276.         # chances and we also do not want to waste an additional byte
  277.         # of entropy.
  278.         $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  279.  
  280.         $output = '$2a$';
  281.         $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
  282.         $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
  283.         $output .= '$';
  284.  
  285.         $i = 0;
  286.         do {
  287.             $c1 = ord($input[$i++]);
  288.             $output .= $itoa64[$c1 >> 2];
  289.             $c1 = ($c1 & 0x03) << 4;
  290.             if ($i >= 16) {
  291.                 $output .= $itoa64[$c1];
  292.                 break;
  293.             }
  294.  
  295.             $c2 = ord($input[$i++]);
  296.             $c1 |= $c2 >> 4;
  297.             $output .= $itoa64[$c1];
  298.             $c1 = ($c2 & 0x0f) << 2;
  299.  
  300.             $c2 = ord($input[$i++]);
  301.             $c1 |= $c2 >> 6;
  302.             $output .= $itoa64[$c1];
  303.             $output .= $itoa64[$c2 & 0x3f];
  304.         } while (1);
  305.  
  306.         return $output;
  307.     }
  308.  
  309.     function HashPassword($password)
  310.     {
  311.         $random = '';
  312.  
  313.         if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
  314.             $random = $this->get_random_bytes(16);
  315.             $hash =
  316.                 crypt($password, $this->gensalt_blowfish($random));
  317.             if (strlen($hash) == 60)
  318.                 return $hash;
  319.         }
  320.  
  321.         if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
  322.             if (strlen($random) < 3)
  323.                 $random = $this->get_random_bytes(3);
  324.             $hash =
  325.                 crypt($password, $this->gensalt_extended($random));
  326.             if (strlen($hash) == 20)
  327.                 return $hash;
  328.         }
  329.  
  330.         if (strlen($random) < 6)
  331.             $random = $this->get_random_bytes(6);
  332.         $hash =
  333.             $this->crypt_private($password,
  334.             $this->gensalt_private($random));
  335.         if (strlen($hash) == 34)
  336.             return $hash;
  337.  
  338.         # Returning '*' on error is safe here, but would _not_ be safe
  339.         # in a crypt(3)-like function used _both_ for generating new
  340.         # hashes and for validating passwords against existing hashes.
  341.         return '*';
  342.     }
  343.  
  344.     function CheckPassword($password, $stored_hash)
  345.     {
  346.  
  347. echo "///////////////// BEGIN\n";
  348. var_dump($password, $stored_hash);
  349. $hash = $this->crypt_private($password, $stored_hash);
  350. var_dump( $hash );
  351. var_dump( crypt($password, $stored_hash) );
  352. echo "///////////////// END\n";
  353.  
  354.         $hash = $this->crypt_private($password, $stored_hash);
  355.         if ($hash[0] == '*')
  356.             $hash = crypt($password, $stored_hash);
  357.  
  358.         return $hash == $stored_hash;
  359.     }
  360. }
  361.  
  362. echo "should be true:\n";
  363. /**
  364. var_dump( yourls_has_phpass_password( 'phpass' ) );
  365. var_dump( yourls_has_phpass_password( 'phpass2' ) );
  366. /**/
  367. var_dump(  yourls_check_password_hash( 'phpass', 'phpass' ) );
  368. var_dump(  yourls_check_password_hash( 'phpass2', 'phpass' ) );
  369.  
  370. /**
  371. echo "\n--------------\n\nshould be false:\n";
  372. var_dump( yourls_check_password_hash( 'unknown', 'phpass' ) );
  373. var_dump( yourls_check_password_hash( 'phpass', 'notphpass' ) );
  374. var_dump( yourls_check_password_hash( 'phpass2', 'notphpass' ) );
  375. /**/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement