Last active
November 29, 2022 10:16
-
-
Save lucablackwell/46fbe70e84fab033fbbd67505518bcca to your computer and use it in GitHub Desktop.
php password generator, one-liner or interactive - substitutes letters for numbers, makes sure there are caps and punctuation
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 | |
# generates password | |
# Colours | |
function blue($text) { | |
return "\e[;36m$text\e[0m"; | |
} | |
function blue_bold($text) { | |
return "\e[1;36m$text\e[0m"; | |
} | |
function white_bold($text) { | |
return "\e[1;37m$text\e[0m"; | |
} | |
function yellow($text) { | |
return "\e[0;33m$text\e[0m"; | |
} | |
# Show and take input for choices | |
function show_choice($choices) { | |
# Initial spacer | |
echo " "; | |
# For each choice | |
for ($i = 0; $i < count($choices); $i++) { | |
# Show the choice | |
echo white_bold($i+1 . ". ") . yellow($choices[$i]) . "\n"; | |
# If it is not the last | |
if ($i != count($choices)-1) { | |
# Intermediate spacer | |
echo " "; | |
} | |
} | |
# Take user input | |
$input = readline('> '); | |
$forward = false; | |
$to_return = null; | |
while (!$forward) { | |
# Loop through each choice | |
foreach ($choices as $choice) { | |
# If the input is only numbers, is within the range of choices and is equal to the current choice | |
if (preg_match('/[0-9]/', $input) && !preg_match('/[a-z]/', $input) && $input <= count($choices) && $choices[$input-1] == $choice) { | |
# Set the current choice to be returned | |
$to_return = $choice; | |
# If the input is the same as the current choice | |
} elseif (strtolower($input) == strtolower($choice)) { | |
# Set the current choice to be returned | |
$to_return = $choice; | |
} | |
} | |
# If there is something to return | |
if ($to_return) { | |
# End the loop | |
$forward = true; | |
# If there isn't something to return (i.e. the input is invalid) | |
} else { | |
slow_print("\nInvalid input.", true); | |
# Take user input again | |
$input = readline('> '); | |
} | |
} | |
# Return the assigned value | |
return $to_return; | |
} | |
# Define options | |
$input = getopt('hp::sui', ['help', 'phrase::', 'symbols', 'under', 'interactive']); | |
# If no flags exist | |
if (empty($input)) { | |
echo "Use ".yellow("-h")." or ".yellow("--help")." to access the help menu.\n"; | |
exit; | |
} | |
# Help text | |
if (array_key_exists('help', $input) || array_key_exists('h', $input)) { | |
echo "\n ".blue_bold("HELP MENU")."\nSHORT LONG DESCRIPTION\n-h --help Display this text.\n\n ".blue("GENERATION")."\n-u --under Replaces spaces with underscores (optional)\n-p --phrase Sets the phrase to turn into a password (required)\n-s --symbols Removes symbols from the phrase & password (optional)\n-i --interactive Guides the user through an interactive generation experience (optional)\n\n"; | |
exit; | |
} | |
# If interactive | |
if (array_key_exists('interactive', $input) || array_key_exists('i', $input)) { | |
$phrase = ''; | |
echo "Enter phrase:\n"; | |
$phrase = readline('> '); | |
if (strlen($phrase) <= 2) { | |
echo "Please enter a phrase longer than 2 characters.\n"; | |
while (strlen($phrase) <= 2) { | |
$phrase = readline('> '); | |
} | |
} | |
echo blue($phrase)."\n"; | |
echo "Replace spaces with underscores?\n"; | |
$under = show_choice(['Yes', 'No']); | |
if ($under == 'Yes') { | |
$under = true; | |
} else { | |
$under = false; | |
} | |
echo "Disable the addition of symbols?\n"; | |
$symbols = show_choice(['Yes', 'No']); | |
if ($symbols == 'Yes') { | |
$symbols = true; | |
} else { | |
$symbols = false; | |
} | |
} else { | |
# Set phrase | |
$phrase = false; | |
# If phrase has been chosen and isn't empty | |
if (array_key_exists('phrase', $input) && !empty($input['phrase'])) { | |
$phrase = $input['phrase']; | |
# If p has been chosen and isn't empty | |
} elseif (array_key_exists('p', $input) && !empty($input['p'])){ | |
$phrase = $input['p']; | |
# If either are empty | |
} elseif (empty($input['phrase']) || empty($input['p'])) { | |
echo "Please enter a phrase.\n"; | |
exit; | |
} | |
# If phrase is less than or equal to 2 characters | |
if (strlen($phrase) <= 2) { | |
echo "Please enter a phrase longer than 2 characters.\n"; | |
exit; | |
} | |
echo blue($phrase)."\n"; | |
$under = false; | |
# If the short or long flags for underscores exist | |
if (array_key_exists('under', $input) || array_key_exists('u', $input)) { | |
$under = true; | |
# If no flags for underscores exist | |
} elseif (!array_key_exists('under', $input) && !array_key_exists('u', $input)) { | |
$under = false; | |
} | |
$symbols = false; | |
# If the short or long flags for symbols exist | |
if (array_key_exists('symbols', $input) || array_key_exists('s', $input)) { | |
$symbols = true; | |
# If no flags for symbols exist | |
} elseif (!array_key_exists('symbols', $input) && !array_key_exists('s', $input)) { | |
$symbols = false; | |
} | |
} | |
# Symbol Handler | |
# Find the first space | |
$first = strpos($phrase, ' '); | |
# Define the symbols | |
$symbol_arr = ['!', ',', '?', '+', '@', '.', '_', '"', '£', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=']; | |
# If symbols are enabled (default) | |
if (!$symbols) { | |
# If the phrase has a space | |
if ($first) { | |
$phrase[$first] = $symbol_arr[array_rand($symbol_arr)]; | |
# If the phrase does not have a space | |
} else { | |
# Define array to count length | |
$length_arr = []; | |
# For the length of the phrase -1 | |
for ($i = 1; $i < strlen($phrase); $i++) { | |
# Add the current number to the length | |
$length_arr[] = $i; | |
} | |
# Assign a random symbol to a random character in the phrase | |
$phrase[array_rand($length_arr)] = $symbol_arr[array_rand($symbol_arr)]; | |
} | |
} | |
# If underscores are enabled | |
if ($under) { | |
# Replace spaces with underscores | |
$phrase = str_replace(' ', '_', $phrase); | |
# If underscores are disabled (default) | |
} else { | |
# Remove spaces | |
$phrase = str_replace(' ', '', $phrase); | |
} | |
# Capitalise First | |
# Find all matches for the alphabet in the phrase | |
preg_match_all('/[a-z]/', $phrase, $matches, PREG_OFFSET_CAPTURE); | |
# Replace all occurences of the first letter with its capitalised version | |
$phrase = str_replace($matches[0][0][0], strtoupper($matches[0][0][0]), $phrase); | |
# Capitalise Most Common | |
# Turn the phrase into an array | |
$phrase_arr = str_split($phrase); | |
# Count the occurences of each letter in the phrase | |
$values = array_count_values($phrase_arr); | |
# Sort the occurence data | |
arsort($values); | |
# Find the letter with the highest occurences | |
$highest = array_slice(array_keys($values), 0, 5, true); | |
# Loop through each common letter | |
foreach ($highest as $char) { | |
# If the highest character is not a space | |
if ($char != ' ') { | |
# Replace all occurences of the most common letter with its capitalised version | |
$phrase = str_replace($char, strtoupper($char), $phrase); | |
break; | |
} | |
} | |
# Numberise | |
# Turn the phrase into an array | |
$phrase_arr = str_split($phrase); | |
# Create the array that will hold the final phrase | |
$final_arr = []; | |
# Loop through the phrase array, randomly change letters to their number/symbol | |
foreach ($phrase_arr as $char) { | |
switch ($char) { | |
case ('a'): | |
$final_arr[] = [4, 4, 'a'][array_rand([1, 2, 3])]; | |
break; | |
case ('e'): | |
$final_arr[] = [3, 3, 'e'][array_rand([1, 2, 3])]; | |
break; | |
case ('i'): | |
$final_arr[] = [1, 1, 'i'][array_rand([1, 2, 3])]; | |
break; | |
case ('o'): | |
$final_arr[] = [0, 0, 'o'][array_rand([1, 2, 3])]; | |
break; | |
case ('u'): | |
if ($symbols) { | |
$final_arr[] = 'u'; | |
} else { | |
$final_arr[] = ['^', 'u'][array_rand([1, 2])]; | |
} | |
break; | |
case ('s'): | |
$final_arr[] = [5, 5, 's'][array_rand([1, 2, 3])]; | |
break; | |
case ('t'): | |
$final_arr[] = [7, 7, 't'][array_rand([1, 2, 3])]; | |
break; | |
case ('A'): | |
$final_arr[] = [4, 4, 'A', 'A', 'a'][array_rand([1, 2, 3, 4, 5])]; | |
break; | |
case ('E'): | |
$final_arr[] = [3, 3, 'E', 'E', 'e'][array_rand([1, 2, 3, 4, 5])]; | |
break; | |
case ('I'): | |
$final_arr[] = [1, 1, 'I', 'I', 'i'][array_rand([1, 2, 3, 4, 5])]; | |
break; | |
case ('O'): | |
$final_arr[] = [0, 0, 'O', 'O', 'o'][array_rand([1, 2, 3, 4, 5])]; | |
break; | |
case ('U'): | |
if ($symbols) { | |
$final_arr[] = 'U'; | |
} else { | |
$final_arr[] = ['^', '^', 'U', 'U', 'u'][array_rand([1, 2, 3, 4, 5])]; | |
} | |
break; | |
case ('S'): | |
$final_arr[] = [5, 5, 'S', 'S', 's'][array_rand([1, 2, 3, 4, 5])]; | |
break; | |
case ('T'): | |
$final_arr[] = [7, 7, 'T', 'T', 'T'][array_rand([1, 2, 3, 4, 5])]; | |
break; | |
default: | |
$final_arr[] = $char; | |
break; | |
} | |
} | |
# Turn the array back into a string | |
$phrase = implode('', $final_arr); | |
echo "$phrase\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment