Created
October 15, 2015 13:28
-
-
Save xize/f5f69df7a352a056c673 to your computer and use it in GitHub Desktop.
my first algorithm!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WARNING: This class is only to make layers of salts to a password, its definitely not secure for encryption, it's my first attempted algorithm ever. | |
* | |
* @author xize | |
* Class salt | |
*/ | |
class Salt { | |
private $password; | |
private $bits; | |
public function __construct($password, $bits) { | |
$this->password = $password; | |
$this->bits = $bits; | |
} | |
/** | |
* shuffles the array elements. | |
* | |
* @author xize | |
* @param $array - the array | |
* @param $seed - the seed | |
*/ | |
private function shuffle(&$array, $seed) { | |
foreach($array as $index) { | |
$newarray[$index] = $array[mt_rand(1, (count($array)-1))]; | |
} | |
$array = $newarray; | |
} | |
/** | |
* adds random utf-8 encoded psuodo bytes based on the seed given in | |
* | |
* @author xize | |
* @param $array - the array | |
* @param $seed - the seed | |
*/ | |
private function addBytes(&$array, $seed) { | |
for($i = 0; $i < $this->bits; $i++) { | |
$newarray[$i] = mt_rand(0, 128); //respectable only generate between 0 and 128 to hold UTF-8 compatibility | |
} | |
$array = array_merge($array, $newarray); | |
$this->shuffle($array, $seed); | |
} | |
/** | |
* returns integer | |
* | |
* @author xize | |
* @param $array - the array | |
* @return int | |
*/ | |
private function valuesToString($array) { | |
$data = ""; | |
foreach(array_values($array) as $value) { | |
$data .= $value; | |
} | |
return (int)$data; | |
} | |
/** | |
* generates the salt based on the password and bit. | |
* | |
* @author xize | |
* @return string | |
*/ | |
public function generateSalt() { | |
//make the password a bit base encoded to make it a bit confusing. | |
$encoded = base64_encode($this->password); | |
//convert the encoded text into a byte array. | |
$array = unpack("C*", $encoded); | |
//generate a unique UUID based on a litteral count of the chars hexadecimal count. so char + char and not (char + char) ;-) | |
$seed = $this->valuesToString($array); | |
//now we use the seed in php's random generator. | |
mt_srand($seed); | |
//now we add random psuodo bytes based on the seed, the bytes will be respectable be under 128 for UTF-8 compatibility. | |
$this->addBytes($array, $seed); | |
//now we shuffle all bytes based on the seed. | |
$this->shuffle($array, $seed); | |
$salt = ""; | |
//now we add each hexadecimal into a char till we get a new text. | |
foreach($array as $char) { | |
$salt .= chr($char); | |
} | |
return $salt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment