80 lines
1.6 KiB
PHP
80 lines
1.6 KiB
PHP
<?php
|
|
/**
|
|
* Created by Pensare Software.
|
|
* User: Theo Weijmer
|
|
* Date: 26/12/2017
|
|
* Time: 12:40
|
|
*/
|
|
|
|
class PasswordRecipe {
|
|
|
|
private $nakedPassword = false;
|
|
private $salt = false;
|
|
private $saltedPassword;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->handle();
|
|
}
|
|
|
|
public function setNakedPassword( $nakedPassword )
|
|
{
|
|
$this->nakedPassword = $nakedPassword;
|
|
}
|
|
|
|
public function setSalt( $salt )
|
|
{
|
|
$this->salt = $salt;
|
|
}
|
|
|
|
private function handle()
|
|
{
|
|
$this->checkNakedPassword();
|
|
|
|
$this->checkSalt();
|
|
|
|
$this->setSaltedPassword();
|
|
}
|
|
|
|
private function checkNakedPassword()
|
|
{
|
|
if( ! $this->nakedPassword ) $this->createNakedPassword();
|
|
|
|
if( strlen($this->nakedPassword) < 6 ) return;
|
|
}
|
|
|
|
private function checkSalt()
|
|
{
|
|
if( ! $this->salt ) $this->createSalt();
|
|
}
|
|
|
|
private function createNakedPassword()
|
|
{
|
|
$pass = $this->getRandomString(8);
|
|
|
|
$this->nakedPassword = $pass;
|
|
}
|
|
|
|
private function createSalt()
|
|
{
|
|
$salt = $this->getRandomString(8);
|
|
|
|
$this->salt = $salt;
|
|
}
|
|
|
|
private function setSaltedPassword()
|
|
{
|
|
$this->saltedPassword = sha1($this->)
|
|
}
|
|
|
|
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];
|
|
}
|
|
}
|
|
} |