CoreFiles by mySites.guru
SHA256Handler.php97 lines, 2,253 bytes, from Joomla_4.0.0-alpha2-Alpha-Full_Package.zip
Raw History Compare with 3.10.0-alpha9
md5
11b291f4b04afea150f2de0dcf256da8
sha1
1aaf95b87636c957e20de417436e9077f9b8e039
sha256
dd17340314dfa10425eb416d5de12a284c12ec9953bad33b5f9f5a6c7e7176a3
<?php
/**
 * Joomla! Content Management System
 *
 * @copyright  Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Authentication\Password;

defined('JPATH_PLATFORM') or die;

use Joomla\Authentication\Password\HandlerInterface;
use Joomla\CMS\Crypt\Crypt;
use Joomla\CMS\User\UserHelper;

/**
 * Password handler for SHA256 hashed passwords
 *
 * @since       __DEPLOY_VERSION__
 * @deprecated  5.0  Support for SHA256 hashed passwords will be removed
 */
class SHA256Handler implements HandlerInterface, CheckIfRehashNeededHandlerInterface
{
	/**
	 * Check if the password requires rehashing
	 *
	 * @param   string  $hash  The password hash to check
	 *
	 * @return  boolean
	 *
	 * @since   __DEPLOY_VERSION__
	 */
	public function checkIfRehashNeeded(string $hash): bool
	{
		return true;
	}

	/**
	 * Generate a hash for a plaintext password
	 *
	 * @param   string  $plaintext  The plaintext password to validate
	 * @param   array   $options    Options for the hashing operation
	 *
	 * @return  string
	 *
	 * @since   __DEPLOY_VERSION__
	 */
	public function hashPassword($plaintext, array $options = [])
	{
		$salt = $options['salt'] ?? '';

		if ($salt !== '')
		{
			$salt = preg_replace('|^{sha256}|i', '', $salt);
		}
		else
		{
			$salt = UserHelper::genRandomPassword(16);
		}

		$encrypted = ($salt) ? hash('sha256', $plaintext . $salt) . ':' . $salt : hash('sha256', $plaintext);

		return '{SHA256}' . $encrypted;
	}

	/**
	 * Check that the password handler is supported in this environment
	 *
	 * @return  boolean
	 *
	 * @since   __DEPLOY_VERSION__
	 */
	public static function isSupported()
	{
		return true;
	}

	/**
	 * Validate a password
	 *
	 * @param   string  $plaintext  The plain text password to validate
	 * @param   string  $hashed     The password hash to validate against
	 *
	 * @return  boolean
	 *
	 * @since   __DEPLOY_VERSION__
	 */
	public function validatePassword($plaintext, $hashed)
	{
		$parts     = explode(':', $hash);
		$salt      = @$parts[1];
		$testcrypt = $this->hashPassword($password, ['salt' => $salt]);

		return Crypt::timingSafeCompare($hashed, $testcrypt);
	}
}

No other release has this exact content. See every version of this file.