CoreFiles by mySites.guru
SHA256Handler.php97 lines, 2,192 bytes, from Joomla_4.0.0-alpha11-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha10
md5
7460e272b5c31fc09af43ece3eed0781
sha1
e25569a90bba39350a3cd0fc6b9e4e6f9c6a3699
sha256
d24ceb976a18524855310161cf93d68feb8b44d414a986515d1aeabdc7d9b0d0
<?php
/**
 * Joomla! Content Management System
 *
 * @copyright  Copyright (C) 2005 - 2019 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       4.0.0
 * @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   4.0.0
	 */
	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   4.0.0
	 */
	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   4.0.0
	 */
	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   4.0.0
	 */
	public function validatePassword($plaintext, $hashed)
	{
		$parts     = explode(':', $hashed);
		$salt      = @$parts[1];
		$testcrypt = $this->hashPassword($plaintext, ['salt' => $salt]);

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

The same content is in 2 releases, Joomla 4.0.0-alpha11 to 4.0.0-alpha12. See them all, or every version of this file.