Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/05/2010, 22:49
Avatar de HiToGoRoShi
HiToGoRoShi
 
Fecha de Ingreso: abril-2008
Mensajes: 849
Antigüedad: 16 años
Puntos: 31
Comparar contraseñas del phpbb3?

Que tal amigos, tengo un problema lo que pasa es que me estoy separando del phpbb3 a mi propio sistema de foro, el problema esta en que phpbb3 usa un algoritmo para encriptar sus contraseñas y todo lo que busque fue que usaban un framework, el archivo es este

Código PHP:
Ver original
  1. <?php
  2. class PasswordHash {
  3.     var $itoa64;
  4.     var $iteration_count_log2;
  5.     var $portable_hashes;
  6.     var $random_state;
  7.  
  8.     function PasswordHash($iteration_count_log2, $portable_hashes)
  9.     {
  10.         $this->itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  11.  
  12.         if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
  13.             $iteration_count_log2 = 8;
  14.         $this->iteration_count_log2 = $iteration_count_log2;
  15.  
  16.         $this->portable_hashes = $portable_hashes;
  17.  
  18.         $this->random_state = microtime();
  19.         if (function_exists('getmypid'))
  20.             $this->random_state .= getmypid();
  21.     }
  22.  
  23.     function get_random_bytes($count)
  24.     {
  25.         $output = '';
  26.         if (is_readable('/dev/urandom') &&
  27.             ($fh = @fopen('/dev/urandom', 'rb'))) {
  28.             $output = fread($fh, $count);
  29.             fclose($fh);
  30.         }
  31.  
  32.         if (strlen($output) < $count) {
  33.             $output = '';
  34.             for ($i = 0; $i < $count; $i += 16) {
  35.                 $this->random_state =
  36.                     md5(microtime() . $this->random_state);
  37.                 $output .=
  38.                     pack('H*', md5($this->random_state));
  39.             }
  40.             $output = substr($output, 0, $count);
  41.         }
  42.  
  43.         return $output;
  44.     }
  45.  
  46.     function encode64($input, $count)
  47.     {
  48.         $output = '';
  49.         $i = 0;
  50.         do {
  51.             $value = ord($input[$i++]);
  52.             $output .= $this->itoa64[$value & 0x3f];
  53.             if ($i < $count)
  54.                 $value |= ord($input[$i]) << 8;
  55.             $output .= $this->itoa64[($value >> 6) & 0x3f];
  56.             if ($i++ >= $count)
  57.                 break;
  58.             if ($i < $count)
  59.                 $value |= ord($input[$i]) << 16;
  60.             $output .= $this->itoa64[($value >> 12) & 0x3f];
  61.             if ($i++ >= $count)
  62.                 break;
  63.             $output .= $this->itoa64[($value >> 18) & 0x3f];
  64.         } while ($i < $count);
  65.  
  66.         return $output;
  67.     }
  68.  
  69.     function gensalt_private($input)
  70.     {
  71.         $output = '$P$';
  72.         $output .= $this->itoa64[min($this->iteration_count_log2 +
  73.             ((PHP_VERSION >= '5') ? 5 : 3), 30)];
  74.         $output .= $this->encode64($input, 6);
  75.  
  76.         return $output;
  77.     }
  78.  
  79.     function crypt_private($password, $setting)
  80.     {
  81.         $output = '*0';
  82.         if (substr($setting, 0, 2) == $output)
  83.             $output = '*1';
  84.  
  85.         $id = substr($setting, 0, 3);
  86.         # We use "$P$", phpBB3 uses "$H$" for the same thing
  87.         if ($id != '$P$' && $id != '$H$')
  88.             return $output;
  89.  
  90.         $count_log2 = strpos($this->itoa64, $setting[3]);
  91.         if ($count_log2 < 7 || $count_log2 > 30)
  92.             return $output;
  93.  
  94.         $count = 1 << $count_log2;
  95.  
  96.         $salt = substr($setting, 4, 8);
  97.         if (strlen($salt) != 8)
  98.             return $output;
  99.  
  100.         # We're kind of forced to use MD5 here since it's the only
  101.         # cryptographic primitive available in all versions of PHP
  102.         # currently in use.  To implement our own low-level crypto
  103.         # in PHP would result in much worse performance and
  104.         # consequently in lower iteration counts and hashes that are
  105.         # quicker to crack (by non-PHP code).
  106.         if (PHP_VERSION >= '5') {
  107.             $hash = md5($salt . $password, TRUE);
  108.             do {
  109.                 $hash = md5($hash . $password, TRUE);
  110.             } while (--$count);
  111.         } else {
  112.             $hash = pack('H*', md5($salt . $password));
  113.             do {
  114.                 $hash = pack('H*', md5($hash . $password));
  115.             } while (--$count);
  116.         }
  117.  
  118.         $output = substr($setting, 0, 12);
  119.         $output .= $this->encode64($hash, 16);
  120.  
  121.         return $output;
  122.     }
  123.  
  124.     function gensalt_extended($input)
  125.     {
  126.         $count_log2 = min($this->iteration_count_log2 + 8, 24);
  127.         # This should be odd to not reveal weak DES keys, and the
  128.         # maximum valid value is (2**24 - 1) which is odd anyway.
  129.         $count = (1 << $count_log2) - 1;
  130.  
  131.         $output = '_';
  132.         $output .= $this->itoa64[$count & 0x3f];
  133.         $output .= $this->itoa64[($count >> 6) & 0x3f];
  134.         $output .= $this->itoa64[($count >> 12) & 0x3f];
  135.         $output .= $this->itoa64[($count >> 18) & 0x3f];
  136.  
  137.         $output .= $this->encode64($input, 3);
  138.  
  139.         return $output;
  140.     }
  141.  
  142.     function gensalt_blowfish($input)
  143.     {
  144.         # This one needs to use a different order of characters and a
  145.         # different encoding scheme from the one in encode64() above.
  146.         # We care because the last character in our encoded string will
  147.         # only represent 2 bits.  While two known implementations of
  148.         # bcrypt will happily accept and correct a salt string which
  149.         # has the 4 unused bits set to non-zero, we do not want to take
  150.         # chances and we also do not want to waste an additional byte
  151.         # of entropy.
  152.         $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  153.  
  154.         $output = '$2a$';
  155.         $output .= chr(ord('0') + $this->iteration_count_log2 / 10);
  156.         $output .= chr(ord('0') + $this->iteration_count_log2 % 10);
  157.         $output .= '$';
  158.  
  159.         $i = 0;
  160.         do {
  161.             $c1 = ord($input[$i++]);
  162.             $output .= $itoa64[$c1 >> 2];
  163.             $c1 = ($c1 & 0x03) << 4;
  164.             if ($i >= 16) {
  165.                 $output .= $itoa64[$c1];
  166.                 break;
  167.             }
  168.  
  169.             $c2 = ord($input[$i++]);
  170.             $c1 |= $c2 >> 4;
  171.             $output .= $itoa64[$c1];
  172.             $c1 = ($c2 & 0x0f) << 2;
  173.  
  174.             $c2 = ord($input[$i++]);
  175.             $c1 |= $c2 >> 6;
  176.             $output .= $itoa64[$c1];
  177.             $output .= $itoa64[$c2 & 0x3f];
  178.         } while (1);
  179.  
  180.         return $output;
  181.     }
  182.  
  183.     function HashPassword($password)
  184.     {
  185.         $random = '';
  186.  
  187.         if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) {
  188.             $random = $this->get_random_bytes(16);
  189.             $hash =
  190.                 crypt($password, $this->gensalt_blowfish($random));
  191.             if (strlen($hash) == 60)
  192.                 return $hash;
  193.         }
  194.  
  195.         if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) {
  196.             if (strlen($random) < 3)
  197.                 $random = $this->get_random_bytes(3);
  198.             $hash =
  199.                 crypt($password, $this->gensalt_extended($random));
  200.             if (strlen($hash) == 20)
  201.                 return $hash;
  202.         }
  203.  
  204.         if (strlen($random) < 6)
  205.             $random = $this->get_random_bytes(6);
  206.         $hash =
  207.             $this->crypt_private($password,
  208.             $this->gensalt_private($random));
  209.         if (strlen($hash) == 34)
  210.             return $hash;
  211.  
  212.         # Returning '*' on error is safe here, but would _not_ be safe
  213.         # in a crypt(3)-like function used _both_ for generating new
  214.         # hashes and for validating passwords against existing hashes.
  215.         return '*';
  216.     }
  217.  
  218.     function CheckPassword($password, $stored_hash)
  219.     {
  220.         $hash = $this->crypt_private($password, $stored_hash);
  221.         if ($hash[0] == '*')
  222.             $hash = crypt($password, $stored_hash);
  223.  
  224.         return $hash == $stored_hash;
  225.     }
  226. }
  227.  
  228. ?>

Bueno mi pregunta en si es, alguien descubrio una manera sencilla de comparar las contraseñas del phpbb3? o conoce este framework... ya que mi ultima opcion y la menos deseable searia generar una contraseña a los usuarios y que la active mediante e-mail xD pero aveces no llegan los mails, pero ese no es el caso...

Saludos