Skip to content

Instantly share code, notes, and snippets.

@xize
Last active December 28, 2015 06:11
Show Gist options
  • Save xize/29c7e90a9b4b2bf0de13 to your computer and use it in GitHub Desktop.
Save xize/29c7e90a9b4b2bf0de13 to your computer and use it in GitHub Desktop.
version 2
<?php
class Salt {
private static $generator;
protected function __construct() {} //allow the class to be instanced only by it self.
public static function getGenerator() {
if(SELF::$generator instanceof Salt) {
return SELF::$generator;
}
SELF::$generator = new Salt();
return SELF::$generator;
}
public function createSalt($password, $bits) {
//turns password into a bytearray
$bytes = unpack("C*", $password);
//create seed by adding the bytes near each other.
$seed = '';
foreach($bytes as $byte) {
$seed .= $byte;
}
//instancing generator with seed.
srand($seed);
//shuffle current bytes on seed.
$this->shuffle($bytes);
//addbytes and reshuffle the bytes in the array!
$this->addBytes($bytes, $bits);
$salt = '';
foreach($bytes as $byte) {
$salt .= chr($byte);
}
return $salt;
}
private function shuffle(&$array) {
foreach($array as $index) {
$newarray[$index] = $array[rand(1, (count($array)-1))]; //rand() default php function uses the psuodo seed we instanced earlier, note that mt_srand is not and very slow when the password is longer, which is a sign of bad things in any like hood of encryption it makes it vulnerable when the password almost matched, because it went slower, its also not encouraged to use this script as a kind of encryption mechanism.
}
$array = $newarray;
}
private function addBytes(&$array, $bits) {
for($i = 0; $i < $bits; $i++) {
$newarray[$i] = rand(0, 127); //respectable only generate between 0 and 127 (total: 128) to hold UTF-8 compatibility
}
$array = array_merge($array, $newarray);
$this->shuffle($array);
}
public function doCheck() {
$password1 = Salt::getGenerator()->createSalt("Hello123", 2048);
$password2 = Salt::getGenerator()->createSalt("Hello", 2048);
//converting both to bytearray.
$array1 = unpack("C*", $password1);
$seed1 = '';
foreach($array1 as $byte) {
$seed1 .= $byte;
}
$seed2 = '';
$array2 = unpack("C*", $password2);
foreach($array2 as $byte) {
$seed2 .= $byte;
}
echo "manual compare:</br>";
echo "Seed1: ".$seed1."</br></br>";
echo "Seed2: ".$seed2."</br></br>";
echo "is smallest password seed longer than the longer password?: ". (strlen($seed2) > strlen($seed1) ? "definatly, how shorter the password how bigger the seed" : "nope the longest password has the longest seed and the smallest password the shortest seed")."</br></br>";
echo "does the smallest seed exist in the longer seed?: ".($this->doesExist($seed1, $seed2) ? "yes it does" : "no it doesn't");
}
private function doesExist($seed1, $seed2) {
if(strlen($seed1) > strlen($seed2)) {
//seed1 is longest seed
if(strpos($seed1, $seed2) !== false) {
return true;
}
$additional = substr($seed2,0,256);
if(strpos($seed1, $additional) !== false) {
return true;
}
} else {
//seed2 is longest seed
if(strpos($seed2, $seed1) !== false) {
return true;
}
$additional = substr($seed1,0,256);
if(strpos($seed2, $additional) !== false) {
return true;
}
}
return false;
}
}
echo "<p>Hello123:".Salt::getGenerator()->createSalt("Hello123", 2048)."</p>";
echo "<p>A repeat of password \"Hello123\":".Salt::getGenerator()->createSalt("Hello123", 2048)."</p>";
echo "<p>Hello:".Salt::getGenerator()->createSalt("Hello",2048)."</p>";
Salt::getGenerator()->doCheck();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment