CoreFiles by mySites.guru
SHA256Handler.php97 lines, 2,191 bytes, from Joomla_4.0.0-alpha8-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha7
md5
68ed6a509f1d4f2f643a1343eae1c486
sha1
bd6e479c0c5a332705fecef100992e1828c3d089
sha256
c9e4c597855ae392928b23c90b2c4a6720804f88995f1e9f0b2ecbe588661e3b
<?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 4 releases, Joomla 4.0.0-alpha7 to 4.0.0-alpha10. See them all, or every version of this file.