Advertisement
FabioZumbi12

phpbb3 Hash Encoding - For Joomla

Jul 9th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.39 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Authentication.phpbb3auth
  5.  *
  6.  * @copyright   Copyright (C) 2013 Gerd Bartelt. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC') or die;
  11.  
  12. /**
  13.  * phpBB3 Authentication Plugin
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Authentication.phpbb3auth
  17.  * @since       1.5
  18.  */
  19. class PlgAuthenticationPhpbb3auth extends JPlugin
  20. {
  21.  
  22.     /**
  23.     * Encode hash
  24.     */
  25.     private function _hash_encode64($input, $count, &$itoa64)
  26.     {
  27.         $output = '';
  28.         $i = 0;
  29.  
  30.         do
  31.         {
  32.             $value = ord($input[$i++]);
  33.             $output .= $itoa64[$value & 0x3f];
  34.  
  35.             if ($i < $count)
  36.             {
  37.                 $value |= ord($input[$i]) << 8;
  38.             }
  39.  
  40.             $output .= $itoa64[($value >> 6) & 0x3f];
  41.  
  42.             if ($i++ >= $count)
  43.             {
  44.                 break;
  45.             }
  46.  
  47.             if ($i < $count)
  48.             {
  49.                 $value |= ord($input[$i]) << 16;
  50.             }
  51.  
  52.             $output .= $itoa64[($value >> 12) & 0x3f];
  53.  
  54.             if ($i++ >= $count)
  55.             {
  56.                 break;
  57.             }
  58.  
  59.             $output .= $itoa64[($value >> 18) & 0x3f];
  60.         }
  61.         while ($i < $count);
  62.  
  63.         return $output;
  64.     }
  65.  
  66.     /**
  67.     * The crypt function/replacement
  68.     */
  69.     private function _hash_crypt_private($password, $setting, &$itoa64)
  70.     {
  71.         $output = '*';
  72.  
  73.         // Check for correct hash
  74.         if (substr($setting, 0, 3) != '$H$' && substr($setting, 0, 3) != '$P$')
  75.         {
  76.             return $output;
  77.         }
  78.  
  79.         $count_log2 = strpos($itoa64, $setting[3]);
  80.  
  81.         if ($count_log2 < 7 || $count_log2 > 30)
  82.         {
  83.             return $output;
  84.         }
  85.  
  86.         $count = 1 << $count_log2;
  87.         $salt = substr($setting, 4, 8);
  88.  
  89.         if (strlen($salt) != 8)
  90.         {
  91.             return $output;
  92.         }
  93.  
  94.         /**
  95.         * We're kind of forced to use MD5 here since it's the only
  96.         * cryptographic primitive available in all versions of PHP
  97.         * currently in use.  To implement our own low-level crypto
  98.         * in PHP would result in much worse performance and
  99.         * consequently in lower iteration counts and hashes that are
  100.         * quicker to crack (by non-PHP code).
  101.         */
  102.         if (PHP_VERSION >= 5)
  103.         {
  104.             $hash = md5($salt . $password, true);
  105.             do
  106.             {
  107.                 $hash = md5($hash . $password, true);
  108.             }
  109.             while (--$count);
  110.         }
  111.         else
  112.         {
  113.             $hash = pack('H*', md5($salt . $password));
  114.             do
  115.             {
  116.                 $hash = pack('H*', md5($hash . $password));
  117.             }
  118.             while (--$count);
  119.         }
  120.  
  121.         $output = substr($setting, 0, 12);
  122.         $output .= $this->_hash_encode64($hash, 16, $itoa64);
  123.  
  124.         return $output;
  125.     }
  126.      
  127.     /**
  128.     * Check for correct password
  129.     *
  130.     * @param string $password The password in plain text
  131.     * @param string $hash The stored password hash
  132.     *
  133.     * @return bool Returns true if the password is correct, false if not.
  134.     */
  135.     private function phpbb_check_hash($password, $hash)
  136.     {
  137.         $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  138.         if (strlen($hash) == 34)
  139.         {
  140.             return ($this->_hash_crypt_private($password, $hash, $itoa64) === $hash) ? true : false;
  141.         }
  142.  
  143.         return (md5($password) === $hash) ? true : false;
  144.     }
  145.    
  146.      
  147.     public function onUserAuthenticate($credentials, $options, &$response)
  148.     {
  149.         // Joomla does not like blank passwords
  150.         if (empty($credentials['password']))
  151.         {
  152.             $response->status = JAuthentication::STATUS_FAILURE;
  153.             $response->error_message = JText::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
  154.             return false;
  155.         }
  156.  
  157.         // Get a database object
  158.         $db     = JFactory::getDbo();
  159.         $query  = $db->getQuery(true)
  160.             ->select('username, password, email, user_type')
  161.             ->from('f84ix_users')
  162.             ->where('username=' . $db->quote($credentials['username']));
  163.  
  164.         $db->setQuery($query);
  165.         $result = $db->loadObject();
  166.  
  167.         if ($result)
  168.         {
  169.             if (
  170.                 // Check password
  171.                 $this->phpbb_check_hash($credentials['password'],$result->user_password) &&
  172.                 // only active users
  173.                 ( ($result->user_type == 0) || ($result->user_type == 3) )
  174.                 )
  175.             {
  176.                 // Use the phpBB3 email
  177.                 $response->email = $result->user_email;
  178.                 $response->username = $credentials['username'];
  179.                 $response->fullname = $credentials['username'];
  180.  
  181.                 $response->status = JAuthentication::STATUS_SUCCESS;
  182.                 $response->error_message = '';
  183.  
  184.             } else {
  185.                 $response->status = JAuthentication::STATUS_FAILURE;
  186.                 $response->error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS');
  187.             }
  188.         }
  189.         else
  190.         {
  191.             $response->status = JAuthentication::STATUS_FAILURE;
  192.             $response->error_message = JText::_('JGLOBAL_AUTH_NO_USER');
  193.         }
  194.  
  195.         $response->type = 'phpBB3';
  196.  
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement