Foros del Web » Creando para Internet » Sistemas de gestión de contenidos »

Comparar contraseñas del phpbb3?

Estas en el tema de Comparar contraseñas del phpbb3? en el foro de Sistemas de gestión de contenidos en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 24/05/2010, 22:49
Avatar de 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
  #2 (permalink)  
Antiguo 25/05/2010, 10:33
Avatar de HiToGoRoShi  
Fecha de Ingreso: abril-2008
Mensajes: 849
Antigüedad: 16 años
Puntos: 31
Respuesta: Comparar contraseñas del phpbb3?

gracias por la ayuda ¬¬ .......

Jajaja es broma ya encontre la solucion, y quiisirea que algun genio me explique el porque no funcionaba pero antes voy a explicarle a todos como hacerlo.

Primero creamos nuestro archivo

hash.php
Código PHP:
Ver original
  1. <?php
  2. /**
  3. * Check for correct password
  4. *
  5. * @param string $password The password in plain text
  6. * @param string $hash The stored password hash
  7. *
  8. * @return bool Returns true if the password is correct, false if not.
  9. */
  10. function phpbb_check_hash($password, $hash)
  11. {
  12.     $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  13.     if (strlen($hash) == 34)
  14.     {
  15.         return (_hash_crypt_private($password, $hash, $itoa64) === $hash) ? true : false;
  16.     }
  17.  
  18.     return (md5($password) === $hash) ? true : false;
  19. }
  20.  
  21. /**
  22. * Generate salt for hash generation
  23. */
  24. function _hash_gensalt_private($input, &$itoa64, $iteration_count_log2 = 6)
  25. {
  26.     if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31)
  27.     {
  28.         $iteration_count_log2 = 8;
  29.     }
  30.  
  31.     $output = '$H$';
  32.     $output .= $itoa64[min($iteration_count_log2 + ((PHP_VERSION >= 5) ? 5 : 3), 30)];
  33.     $output .= _hash_encode64($input, 6, $itoa64);
  34.  
  35.     return $output;
  36. }
  37.  
  38. /**
  39. * Encode hash
  40. */
  41. function _hash_encode64($input, $count, &$itoa64)
  42. {
  43.     $output = '';
  44.     $i = 0;
  45.  
  46.     do
  47.     {
  48.         $value = ord($input[$i++]);
  49.         $output .= $itoa64[$value & 0x3f];
  50.  
  51.         if ($i < $count)
  52.         {
  53.             $value |= ord($input[$i]) << 8;
  54.         }
  55.  
  56.         $output .= $itoa64[($value >> 6) & 0x3f];
  57.  
  58.         if ($i++ >= $count)
  59.         {
  60.             break;
  61.         }
  62.  
  63.         if ($i < $count)
  64.         {
  65.             $value |= ord($input[$i]) << 16;
  66.         }
  67.  
  68.         $output .= $itoa64[($value >> 12) & 0x3f];
  69.  
  70.         if ($i++ >= $count)
  71.         {
  72.             break;
  73.         }
  74.  
  75.         $output .= $itoa64[($value >> 18) & 0x3f];
  76.     }
  77.     while ($i < $count);
  78.  
  79.     return $output;
  80. }
  81.  
  82. /**
  83. * The crypt function/replacement
  84. */
  85. function _hash_crypt_private($password, $setting, &$itoa64)
  86. {
  87.     $output = '*';
  88.  
  89.     // Check for correct hash
  90.     if (substr($setting, 0, 3) != '$H$')
  91.     {
  92.         return $output;
  93.     }
  94.  
  95.     $count_log2 = strpos($itoa64, $setting[3]);
  96.  
  97.     if ($count_log2 < 7 || $count_log2 > 30)
  98.     {
  99.         return $output;
  100.     }
  101.  
  102.     $count = 1 << $count_log2;
  103.     $salt = substr($setting, 4, 8);
  104.  
  105.     if (strlen($salt) != 8)
  106.     {
  107.         return $output;
  108.     }
  109.  
  110.     /**
  111.     * We're kind of forced to use MD5 here since it's the only
  112.     * cryptographic primitive available in all versions of PHP
  113.     * currently in use.  To implement our own low-level crypto
  114.     * in PHP would result in much worse performance and
  115.     * consequently in lower iteration counts and hashes that are
  116.     * quicker to crack (by non-PHP code).
  117.     */
  118.     if (PHP_VERSION >= 5)
  119.     {
  120.         $hash = md5($salt . $password, true);
  121.         do
  122.         {
  123.             $hash = md5($hash . $password, true);
  124.         }
  125.         while (--$count);
  126.     }
  127.     else
  128.     {
  129.         $hash = pack('H*', md5($salt . $password));
  130.         do
  131.         {
  132.             $hash = pack('H*', md5($hash . $password));
  133.         }
  134.         while (--$count);
  135.     }
  136.  
  137.     $output = substr($setting, 0, 12);
  138.     $output .= _hash_encode64($hash, 16, $itoa64);
  139.  
  140.     return $output;
  141. }
  142. ?>

Nuestro archivo para comparar los password

validarPassword.php
Código PHP:
Ver original
  1. include_once("hash.php");
  2.  
  3. if (phpbb_check_hash('PASSWORD ORIGINAL', 'PASSWORD HASH'))
  4.     print "GOOD";
  5. else
  6.     print "BAD";

Mi error era que este no acepta comillas, solo apostrofes el cual no tnego idea porque, si alguien me explica seria genial..

Saludos

Etiquetas: comparar, contraseñas, phpbb
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:16.