Skip to content

Instantly share code, notes, and snippets.

@marcocesarato
Last active May 20, 2019 14:02
Show Gist options
  • Save marcocesarato/16926b38cae4a86a57ff36586238375a to your computer and use it in GitHub Desktop.
Save marcocesarato/16926b38cae4a86a57ff36586238375a to your computer and use it in GitHub Desktop.
User friendly password generator
<?php
/**
* Generate user friendly password
* @author Marco Cesarato <cesarato.developer@gmail.com>
* @copyright Copyright (c) 2014-2018
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @link https://github.com/marcocesarato/PHP-AIO-Security-Class
*
* @param $string
* @param bool $strong_lv (0-2)
* @return mixed|string
*
* @example generateFriendlyPassword("Marco Cesarato 2018"); // RANDOM OUTPUT: Ce$Ar4t0_m4RCo_2018
*/
function generateFriendlyPassword($string, $strong_lv = 1) {
$alpha_replace = array(
'A' => '4',
'B' => '8',
'E' => '3',
'S' => '$',
'I' => '1',
'O' => '0',
'T' => '7',
'L' => '2',
'G' => '6',
);
$numeric_replace = array(
'0' => 'O',
'1' => '!',
'4' => 'A',
'5' => 'S',
'6' => 'G',
'7' => 'T',
'8' => 'B',
);
$special = '_=-+#@%&*!?';
$string = strtolower($string);
$estr = explode(' ', $string);
foreach ($estr as &$str) {
$astr = str_split($str);
$new_astr = array();
foreach ($astr as $i => $char) {
$char = rand(0, 100) > 50 ? strtoupper($char) : $char;
if ($strong_lv > 0 &&
(!empty($astr[$i - 1]) && ($new_astr[$i - 1] == $astr[$i - 1] || $astr[$i] == $astr[$i - 1]) ||
!empty($astr[$i + 1]) && $astr[$i] == $astr[$i + 1])) {
if($strong_lv > 1) $char = str_replace(array_keys($numeric_replace), $numeric_replace, $char);
if(strtolower($astr[$i]) == strtolower($char)) $char = str_replace(array_keys($alpha_replace), $alpha_replace, strtoupper($char));
}
$new_astr[] = $char;
}
$str = implode('', $new_astr);
}
shuffle($estr);
$string = implode(' ', $estr);
$string = str_replace(' ', $special[rand(0, strlen($special) - 1)], $string);
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment