-
-
Save kimseler14/72a6a729b7ff6f6296e346771c90a3c6 to your computer and use it in GitHub Desktop.
connected2 anon nick & password generator
This file contains hidden or 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 | |
##################################################### | |
# USAGE # | |
# include(CreateAnonNickPass.php) # | |
# CreateAnonNickPass::getAnonNickPassword(); # | |
##################################################### | |
class SecureRandomException extends Exception { | |
} | |
class CreateAnonNickPass | |
{ | |
const DEFAULT_BYTE_LENGTH = 32; | |
public static function bytes($byte_length = self::DEFAULT_BYTE_LENGTH) | |
{ | |
if (function_exists('openssl_random_pseudo_bytes')) { | |
return openssl_random_pseudo_bytes($byte_length); | |
} else { | |
$openssl_path = exec("which openssl", $_trash); | |
if ($openssl_path == false) { | |
throw new SecureRandomException("OpenSSL not found."); | |
} else { | |
$fp = popen("$openssl_path rand $byte_length", "r"); | |
if ($fp === false) { | |
throw new SecureRandomException("OpenSSL not available."); | |
} else { | |
$bytes = stream_get_contents($fp); | |
pclose($fp); | |
return $bytes; | |
} | |
} | |
} | |
} | |
public static function hex($byte_length = self::DEFAULT_BYTE_LENGTH) | |
{ | |
return bin2hex(self::bytes($byte_length)); | |
} | |
public static function base64($byte_length = self::DEFAULT_BYTE_LENGTH) | |
{ | |
return base64_encode(self::bytes($byte_length)); | |
} | |
public static function computeHash($text){ | |
$hash_str = hash("sha256",$text,true); | |
$hash_bytes = unpack('C*', $hash_str); | |
$res =""; | |
for($i=1;$i<=count($hash_bytes);$i++){ | |
if(intval($hash_bytes[$i])>256){ | |
$res.=dechex(strval(intval($hash_bytes[$i])-256)); | |
} | |
$res.=dechex($hash_bytes[$i]); | |
} | |
return $res; | |
} | |
public static function getAnonNickFromPassword($passwd){ | |
$hash_str = self::computeHash($passwd); | |
$nick = "anon-"; | |
$nick .= substr($hash_str,0,15); | |
return $nick; | |
} | |
public static function getAnonNickPassword(){ | |
$passwd = self::hex(); | |
$nick = self::getAnonNickFromPassword($passwd); | |
$array["nick"]=$nick; | |
$array["password"]=$passwd; | |
return json_encode($array,true); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment