99 lines
1.9 KiB
PHP
99 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* Created by Pensare Software.
|
|
* User: Theo Weijmer
|
|
* Date: 26/12/2017
|
|
* Time: 12:40
|
|
*/
|
|
|
|
class PasswordRecipe {
|
|
|
|
private $password = FALSE;
|
|
private $salt = FALSE;
|
|
private $saltedPassword;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->handle();
|
|
}
|
|
|
|
public function getPassword()
|
|
{
|
|
return $this->password;
|
|
}
|
|
|
|
public function getSalt()
|
|
{
|
|
return $this->salt;
|
|
}
|
|
|
|
public function getSaltedPassword()
|
|
{
|
|
return $this->saltedPassword;
|
|
}
|
|
|
|
public function setPassword( $password )
|
|
{
|
|
$this->password = $password;
|
|
}
|
|
|
|
public function setSalt( $salt )
|
|
{
|
|
$this->salt = $salt;
|
|
}
|
|
|
|
private function handle()
|
|
{
|
|
$this->checkPassword();
|
|
|
|
$this->checkSalt();
|
|
|
|
$this->setSaltedPassword();
|
|
}
|
|
|
|
private function checkPassword()
|
|
{
|
|
if ( ! $this->password ) $this->createPassword();
|
|
|
|
if ( strlen( $this->password ) < 6 ) return;
|
|
}
|
|
|
|
private function checkSalt()
|
|
{
|
|
if ( ! $this->salt ) $this->createSalt();
|
|
}
|
|
|
|
private function createPassword()
|
|
{
|
|
$pass = $this->getRandomString( 8 );
|
|
|
|
$this->password = $pass;
|
|
}
|
|
|
|
private function createSalt()
|
|
{
|
|
$salt = $this->getRandomString( 18 );
|
|
|
|
$this->salt = sha1($salt);
|
|
}
|
|
|
|
private function setSaltedPassword()
|
|
{
|
|
$this->saltedPassword = sha1( sha1( $this->password ) . $this->salt );
|
|
}
|
|
|
|
private function getRandomString( $length = 8 )
|
|
{
|
|
$rand = '';
|
|
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
|
|
$alphaLength = strlen( $alphabet ) - 1; //put the length -1 in cache
|
|
for ( $i = 0; $i < $length; $i ++ )
|
|
{
|
|
$n = rand( 0, $alphaLength );
|
|
$rand .= $alphabet[ $n ];
|
|
}
|
|
|
|
return $rand;
|
|
}
|
|
}
|