Created
November 11, 2020 09:30
-
-
Save rotten77/57e0218b0f26ef9390a0b7f08f8480bd to your computer and use it in GitHub Desktop.
Password generators (Linux Bash, Windows PowerShell)
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
# utility for password generator | |
function Get-RandomChar { | |
$length = $args[0] | |
$characters = $args[1] | |
$counter = 0 | |
$random = "" | |
while($counter -ne $length) { | |
$random += $characters.Substring((Get-Random -Maximum $characters.length), 1) | |
$counter++ | |
} | |
return $random | |
} | |
# generate password | |
function Get-RandomPassword { | |
$length = $args[0] | |
# characters to work with | |
$capitals = 'ABCDEFGHKLMNOPRSTUVWXYZ' | |
$lowers = 'abcdefghiklmnoprstuvwxyz' | |
$nums = '1234567890' | |
$specials = '.!&=?@#+' | |
# minimal length is 4 | |
if($length -lt 4) {$length = 4} | |
# first 4 characters - required chars (capital, lower, number, special) | |
$password = Get-RandomChar 1 $lowers | |
$password += Get-RandomChar 1 $capitals | |
$password += Get-RandomChar 1 $nums | |
$password += Get-RandomChar 1 $specials | |
# rest - only alphanumeric | |
while($password.length -ne $length) { | |
$password += Get-RandomChar 1 ($lowers + $capitals + $nums) | |
} | |
return $password; | |
} | |
# example | |
$new_password = Get-RandomPassword 10 |
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
#!/bin/bash | |
# utility for password generator | |
get_random_char () { | |
if [ "$#" -ne 2 ] | |
then | |
echo "Usage: get_random_char [length] [characters]" | |
exit 1 | |
fi | |
length=$1 | |
characters=$2 | |
if [[ length -lt 1 ]]; then length=1; fi | |
charactersLength=${#characters} | |
if [[ charactersLength -lt 1 ]]; then characters="*"; charactersLength=1; fi | |
random_chars="" | |
for ((n=0;n<$length;n++)) | |
do | |
rand=$((0 + $RANDOM % charactersLength)) | |
random_chars+=${characters:rand:1} | |
done | |
echo $random_chars | |
} | |
# generate password | |
generate_password () { | |
if [ "$#" -ne 1 ] | |
then | |
echo "Usage: generate_password [length]" | |
exit 1 | |
fi | |
length=$1 | |
# minimal length is 4 | |
if [[ length -lt 1 ]]; then length=4; fi | |
# characters to work with | |
capitals='ABCDEFGHKLMNOPRSTUVWXYZ' | |
lowers='abcdefghiklmnoprstuvwxyz' | |
nums='1234567890' | |
specials='.!&=?@#+' | |
# first 4 characters - required chars (capital, lower, number, special) | |
password="$(get_random_char 1 $lowers)" | |
# echo $password | |
password+="$(get_random_char 1 $capitals)" | |
# echo $password | |
password+="$(get_random_char 1 $nums)" | |
# echo $password | |
password+="$(get_random_char 1 $specials)" | |
# echo $password | |
# rest - only alphanumeric | |
for ((n=4;n<$length;n++)) | |
do | |
password+="$(get_random_char 1 "${lowers}${capitals}${nums}")" | |
# echo $password | |
done | |
echo $password | |
} | |
# example | |
new_pass=$(generate_password 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment