Gdn_PasswordHash::checkPassword PHP Method

checkPassword() public method

The stored password can be plain, a md5 hash or a phpass hash. If the password wasn't a phppass hash, the Weak property is set to **true**.
public checkPassword ( string $Password, string $StoredHash, boolean | string $Method = false ) : boolean
$Password string The plaintext password to check.
$StoredHash string The password hash stored in the database.
$Method boolean | string The password hashing method.
return boolean Returns **true** if the password matches the hash or **false** if it doesn't.
    public function checkPassword($Password, $StoredHash, $Method = false)
    {
        $Result = false;
        if (empty($Password) || empty($StoredHash)) {
            // We don't care if there is a strong password hash. Empty passwords are not cool
            return false;
        }
        switch (strtolower($Method)) {
            case 'crypt':
                $Result = crypt($Password, $StoredHash) === $StoredHash;
                break;
            case 'django':
                $Result = $this->getAlgorithm('Django')->verify($Password, $StoredHash);
                break;
            case 'drupal':
                require_once PATH_LIBRARY . '/vendors/drupal/password.inc.php';
                $Result = Drupal\user_check_password($Password, $StoredHash);
                break;
            case 'ipb':
                $Result = $this->getAlgorithm('Ipb')->verify($Password, $StoredHash);
                break;
            case 'joomla':
                $Result = $this->getAlgorithm('Joomla')->verify($Password, $StoredHash);
                break;
            case 'mybb':
                $Result = $this->getAlgorithm('Mybb')->verify($Password, $StoredHash);
                break;
            case 'phpass':
                $Result = $this->getAlgorithm('Phpass')->verify($Password, $StoredHash);
                break;
            case 'phpbb':
                $Result = $this->getAlgorithm('Phpbb')->verify($Password, $StoredHash);
                break;
            case 'punbb':
                $Result = $this->getAlgorithm('Punbb')->verify($Password, $StoredHash);
                break;
            case 'reset':
                $ResetUrl = url('entry/passwordrequest' . (Gdn::request()->get('display') ? '?display=' . urlencode(Gdn::request()->get('display')) : ''));
                throw new Gdn_UserException(sprintf(T('You need to reset your password.', 'You need to reset your password. This is most likely because an administrator recently changed your account information. Click <a href="%s">here</a> to reset your password.'), $ResetUrl));
                break;
            case 'random':
                $ResetUrl = url('entry/passwordrequest' . (Gdn::request()->get('display') ? '?display=' . urlencode(Gdn::request()->get('display')) : ''));
                throw new Gdn_UserException(sprintf(T('You don\'t have a password.', 'Your account does not have a password assigned to it yet. Click <a href="%s">here</a> to reset your password.'), $ResetUrl));
                break;
            case 'smf':
                $Result = $this->getAlgorithm('Smf')->verify($Password, $StoredHash);
                break;
            case 'vbulletin':
                $Result = $this->getAlgorithm('Vbulletin')->verify($Password, $StoredHash);
                break;
            case 'vbulletin5':
                // Since 5.1
                // md5 sum the raw password before crypt. Nice work as usual vb.
                $Result = $StoredHash === crypt(md5($Password), $StoredHash);
                break;
            case 'xenforo':
                $Result = $this->getAlgorithm('Xenforo')->verify($Password, $StoredHash);
                break;
            case 'yaf':
                $Result = $this->checkYAF($Password, $StoredHash);
                break;
            case 'webwiz':
                require_once PATH_LIBRARY . '/vendors/misc/functions.webwizhash.php';
                $Result = ww_CheckPassword($Password, $StoredHash);
                break;
            case 'vanilla':
            default:
                $this->Weak = $this->getAlgorithm('Vanilla')->needsRehash($StoredHash);
                $Result = $this->getAlgorithm('Vanilla')->verify($Password, $StoredHash);
        }
        return $Result;
    }

Usage Example

 /**
  * Signin process that multiple authentication methods.
  *
  * @access public
  * @since 2.0.0
  * @author Tim Gunter
  *
  * @param string $Method
  * @param array $Arg1
  * @return string Rendered XHTML template.
  */
 public function signIn($Method = false, $Arg1 = false)
 {
     if (!$this->Request->isPostBack()) {
         $this->checkOverride('SignIn', $this->target());
     }
     Gdn::session()->ensureTransientKey();
     $this->addJsFile('entry.js');
     $this->setData('Title', t('Sign In'));
     $this->Form->addHidden('Target', $this->target());
     $this->Form->addHidden('ClientHour', date('Y-m-d H:00'));
     // Use the server's current hour as a default.
     // Additional signin methods are set up with plugins.
     $Methods = array();
     $this->setData('Methods', $Methods);
     $this->setData('FormUrl', url('entry/signin'));
     $this->fireEvent('SignIn');
     if ($this->Form->isPostBack()) {
         $this->Form->validateRule('Email', 'ValidateRequired', sprintf(t('%s is required.'), t(UserModel::signinLabelCode())));
         $this->Form->validateRule('Password', 'ValidateRequired');
         if (!$this->Request->isAuthenticatedPostBack() && !c('Garden.Embed.Allow')) {
             $this->Form->addError('Please try again.');
         }
         // Check the user.
         if ($this->Form->errorCount() == 0) {
             $Email = $this->Form->getFormValue('Email');
             $User = Gdn::userModel()->GetByEmail($Email);
             if (!$User) {
                 $User = Gdn::userModel()->GetByUsername($Email);
             }
             if (!$User) {
                 $this->Form->addError('@' . sprintf(t('User not found.'), strtolower(t(UserModel::SigninLabelCode()))));
                 Logger::event('signin_failure', Logger::INFO, '{signin} failed to sign in. User not found.', array('signin' => $Email));
             } else {
                 // Check the password.
                 $PasswordHash = new Gdn_PasswordHash();
                 $Password = $this->Form->getFormValue('Password');
                 try {
                     $PasswordChecked = $PasswordHash->checkPassword($Password, val('Password', $User), val('HashMethod', $User));
                     // Rate limiting
                     Gdn::userModel()->rateLimit($User, $PasswordChecked);
                     if ($PasswordChecked) {
                         // Update weak passwords
                         $HashMethod = val('HashMethod', $User);
                         if ($PasswordHash->Weak || $HashMethod && strcasecmp($HashMethod, 'Vanilla') != 0) {
                             $Pw = $PasswordHash->hashPassword($Password);
                             Gdn::userModel()->setField(val('UserID', $User), array('Password' => $Pw, 'HashMethod' => 'Vanilla'));
                         }
                         Gdn::session()->start(val('UserID', $User), true, (bool) $this->Form->getFormValue('RememberMe'));
                         if (!Gdn::session()->checkPermission('Garden.SignIn.Allow')) {
                             $this->Form->addError('ErrorPermission');
                             Gdn::session()->end();
                         } else {
                             $ClientHour = $this->Form->getFormValue('ClientHour');
                             $HourOffset = Gdn::session()->User->HourOffset;
                             if (is_numeric($ClientHour) && $ClientHour >= 0 && $ClientHour < 24) {
                                 $HourOffset = $ClientHour - date('G', time());
                             }
                             if ($HourOffset != Gdn::session()->User->HourOffset) {
                                 Gdn::userModel()->setProperty(Gdn::session()->UserID, 'HourOffset', $HourOffset);
                             }
                             Gdn::userModel()->fireEvent('AfterSignIn');
                             $this->_setRedirect();
                         }
                     } else {
                         $this->Form->addError('Invalid password.');
                         Logger::event('signin_failure', Logger::WARNING, '{username} failed to sign in.  Invalid password.', array('InsertName' => $User->Name));
                     }
                 } catch (Gdn_UserException $Ex) {
                     $this->Form->addError($Ex);
                 }
             }
         }
     } else {
         if ($Target = $this->Request->get('Target')) {
             $this->Form->addHidden('Target', $Target);
         }
         $this->Form->setValue('RememberMe', true);
     }
     return $this->render();
 }
All Usage Examples Of Gdn_PasswordHash::checkPassword