MaketRandevu/application/helpers/password_helper.php

61 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
2015-10-18 20:46:16 +03:00
/* ----------------------------------------------------------------------------
2024-01-01 01:14:38 +03:00
* MaketRandevu - MAKET Randevu Portalı
2015-10-18 20:46:16 +03:00
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
2015-10-18 20:46:16 +03:00
* @since v1.0.0
* ---------------------------------------------------------------------------- */
/**
* Generate a hash of password string.
*
* For user security, all system passwords are stored in hash string into the database. Use this method to produce the
* hashed password.
2015-10-18 20:46:16 +03:00
*
* @param string $salt Salt value for current user. This value is stored on the database and is used when generating
* the password hashes.
2015-10-18 20:46:16 +03:00
* @param string $password Given string password.
*
2015-10-18 20:46:16 +03:00
* @return string Returns the hash string of the given password.
2022-05-10 00:26:39 +03:00
*
* @throws Exception
2015-10-18 20:46:16 +03:00
*/
function hash_password(string $salt, string $password): string
{
if (strlen($password) > MAX_PASSWORD_LENGTH) {
throw new InvalidArgumentException('The provided password is too long, please use a shorter value.');
2022-05-10 00:26:39 +03:00
}
$half = (int) (strlen($salt) / 2);
$hash = hash('sha256', substr($salt, 0, $half) . $password . substr($salt, $half));
2015-10-18 20:46:16 +03:00
for ($i = 0; $i < 100000; $i++) {
2015-10-18 20:46:16 +03:00
$hash = hash('sha256', $hash);
}
return $hash;
}
/**
* Generate a new password salt.
*
* This method will not check if the salt is unique in database. This must be done
* from the calling procedure.
*
* @return string Returns a salt string.
*/
function generate_salt(): string
{
2015-10-18 20:46:16 +03:00
$max_length = 100;
$salt = hash('sha256', uniqid(rand(), true));
2015-10-18 20:46:16 +03:00
return substr($salt, 0, $max_length);
}