<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* -----------------------------------------------------------------------
* Editor : PhpStorm
* Date : 1/20/2019
* Time : 10:50 AM
* Authors : Raymond L King Sr.
* -----------------------------------------------------------------------
*
* Class Key_generator
*
* @project ci3admin
* @author Raymond L King Sr.
* @link http://www.procoversfx.com
* @copyright Copyright (c) 2009 - 2019 Custom Software Designers, LLC.
* @license http://www.procoversfx.com/license
* -----------------------------------------------------------------------
*/
class Key_generator extends CI_Controller
{
/**
* Class properties and methods go here.
* -------------------------------------------------------------------
*
* public, private, protected and static.
*/
/**
* @var
*/
private $password_hash;
private $userName;
private $password;
// -------------------------------------------------------------------
/**
* __construct ()
* -------------------------------------------------------------------
*
* Class Constructor
*
* NOTE: Not needed if not setting values or extending a Class.
*/
public function __construct()
{
parent::__construct();
log_message('debug', "Key_generator Controller Class Initialized");
}
// -------------------------------------------------------------------
/**
* index ()
* -------------------------------------------------------------------
*
*/
public function index()
{
//$this->userName = 'Your User Name';
//$this->password = 'Your Password';
$this->userName = 'admin';
$this->password = 'password';
$this->setPassword($this->password);
echo $this->password_hash . '<br><br>';
$result = $this->verifyPassword($this->password_hash);
echo $result;
}
// -------------------------------------------------------------------
/**
* setPassword ()
* -------------------------------------------------------------------
*
* Automatically hashes the password when set.
*
* @see https://paragonie.com/blog/2015/04/secure-authentication-php-with-long-term-persistence
*
* @param string $password
*/
public function setPassword(string $password)
{
$this->password_hash = password_hash(
base64_encode(
hash('sha384', $password, true)
),
PASSWORD_DEFAULT,
['cost' => 10]
);
}
// -------------------------------------------------------------------
/**
* verifyPassword ()
* -------------------------------------------------------------------
*
* @param string $password
* @return bool
*/
public function verifyPassword(string $password)
{
// Now, try matching the passwords.
$result = password_verify(base64_encode(
hash('sha384', $password, true)
), $this->password_hash);
if (! $result)
{
return false;
}
/**
* Check to see if the password needs to be rehashed.
* This would be due to the hash algorithm or hash
* cost changing since the last time that a user
* logged in.
*/
if (password_needs_rehash($this->password_hash, PASSWORD_DEFAULT))
{
$this->password_hash = $this->setPassword($password);
// save the users record to the database here
}
return true;
}
// -------------------------------------------------------------------
} // End of Key_generator Controller Class.
/**
* -----------------------------------------------------------------------
* Filename: Key_generator.php
* Location: ./application/controllers/Key_generator.php
* -----------------------------------------------------------------------
*/