Last active
February 9, 2018 17:46
-
-
Save nhalstead/38e1dd858cac19e98236680c28fc1faf to your computer and use it in GitHub Desktop.
Generate Keys with Delimiter characters to whatever you want or let it figure that out for you! With this you get the Key back with the hash for the key to store in your database!
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 | |
/** | |
* Create Key Function | |
* Generate a Key that you need with a specific length and a split character. | |
* | |
* @param Int Length of the Key, Total Length | |
* @param Int|Boolean If False, Disabled (default) and the int will provide the spacing for the char. | |
* @param Char|String The Separation Character for the Key every x characters. | |
* @return Array Return of 'key' and 'hash' and 'hash_delimiter' | |
* You can use 'hash' from the output to store in the database. | |
* You will get 2 Hashes: | |
* 'hash' The Text Hash of the key | |
* 'hash_delimiter' The Text Hash that has $splitChar every $splitLoc | |
*/ | |
function createKey($len = 5, $splitLoc = false, $splitChar = "-") { | |
// If Split Location Set True (Auto Locaiton) | |
if($splitLoc === true){ | |
$step = range(12, 0); | |
foreach($step as $k => $v){ | |
if( $len%$v == 0 ){ | |
$splitLoc = $v; | |
break; | |
} | |
} | |
} | |
$chars = "23456789ABCDEFGHJKLMNPRSTUVWXYZ"; | |
$i = 0; | |
$roll = $splitLoc; | |
$pass = ""; | |
$pasd = ""; // Has $splitChar | |
// Shuffle the Characters' order | |
$chars = str_split($chars); | |
shuffle($chars); | |
shuffle($chars); | |
$chars = implode($chars); | |
// Generate the Key | |
while ($i < $len) { | |
if($splitLoc !== false && $roll == $i) { | |
// Insert the Split Character | |
$roll = $roll+$splitLoc; | |
$pasd .= $splitChar; | |
} | |
$num = rand() % (strlen($chars)-1); | |
$c = substr($chars, $num, 1); | |
$pass .= $c; | |
$pasd .= $c; | |
$i++; | |
} | |
return array('key' => $pasd, 'plain' => $pass, 'hash' => hash('sha256', $pass), 'hash_delimiter' => hash('sha256', $pasd)); | |
} | |
print_r(createKey(25, 5)); | |
echo "<br><br>"; | |
print_r(createKey(128, true)); | |
echo "<br><br>"; | |
print_r(createKey(128)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment