Skip to content

Instantly share code, notes, and snippets.

@MINORITYmaN
Last active August 13, 2020 19:31
Show Gist options
  • Save MINORITYmaN/12a6a6af0622ebbeeccc50f732a116f7 to your computer and use it in GitHub Desktop.
Save MINORITYmaN/12a6a6af0622ebbeeccc50f732a116f7 to your computer and use it in GitHub Desktop.
keep it simple stupid password generator function in php - read-friendly, quoted_printable safe, console safe and entropy level based password generation
<?php
function kiss_password( $entropy_level = 4, $length = 16 ) {
/** CC0 1.0 Universal License **/
/** Created: 2017 by Stefano Kocka **/
/** Purpose: Generates read-friendly, quoted_printable safe, console safe and entropy level based password **/
/** Caution: This function does not generate cryptographically secure values,
and should not be used for cryptographic purposes. **/
/** Note: If you expect short as possible output
with all 4 types of entropy then min $length=4 is recommended.
For max there is no limit. **/
/** Note: I recommend to keep all $chars[*] at the same length for best possible results. **/
$chars = [
'ABCDEFGHJKLMNPQRSTUVWXYZ', /** 1=24 **/
'234567892345678923456789', /** 2=24 **/
'abcdefghjklmnpqrstuvwxyz', /** 3=24 **/
'!?$#!?$#!?$#!?$#!?$#!?$#' /** 4=24 **/
];
for ( $i = 0; $i < $entropy_level; $i++ ) {
$chars[ $i ] = str_shuffle( $chars[ $i ] );
$chars[ $i ] = substr( $chars[ $i ], 0,
ceil( $length / $entropy_level ) );
}
$chars = array_slice( $chars, 0, $entropy_level );
$mix = str_shuffle( implode( $chars ) );
if ( $length > strlen( $mix ) ) {
$mix = str_pad( str_shuffle( $mix ), $length,
str_shuffle( $mix ), STR_PAD_RIGHT );
}
return substr( str_shuffle( $mix ), 0, $length );
}
/** Example #1 kiss_password() example **/
echo kiss_password() . "\n";
echo kiss_password( 3 ) . "\n";
echo kiss_password( 4, 4 ) . "\n";
echo "\n";
echo kiss_password( 1 ) . "\n";
echo kiss_password( 2 ) . "\n";
echo kiss_password( 3 ) . "\n";
echo kiss_password( 4 ) . "\n";
echo "\n";
echo kiss_password( $entropy_level = 1, $length = 16 ) . "\n";
echo kiss_password( $entropy_level = 2, $length = 16 ) . "\n";
echo kiss_password( $entropy_level = 3, $length = 16 ) . "\n";
echo kiss_password( $entropy_level = 4, $length = 16 ) . "\n";
/** The above example will output something similar to:
xC$vg5Zd!$6U42#D
45cehV5MA67GN9Ty
p8?A
DCYAVWTNBLUQRFXM
CV325EG3XDA8767P
h6pwM5HR4j4W7XuV
5PdQ!E$#6sB7r5?t
AJUVLGSPQMZDYWRK
3Z4PBLT6Q22W85N7
qVdMxn7397RYJvA7
kq!M9Vv?c#E?257P
**/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment