Mac Terminal – Random Readable Password

| | | |

I recently was challenged to create a random readable password for Mac OS. While I designed this for Mac OS it’s likely it will work with little to no changes on any UNIX system.

Password format was two digit number then a word followed by another word with special characters with a period as the delimiter.

Example: 99.G0ats.L1ve

# Password Format:
# ##.Word$.Oth3r
# Numbers. Word with Caps and Special Character.Word again
# 12-20 Characters in length

# Get two digit random number - Start
sPWDPart1=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 0-9 | head -c 2; echo)
#echo $sPWDPart1
# Get two digit random number - Stop

# Get Random Word - Start
function GetRandomWord()
{
sWord1=$(cat -n /usr/share/dict/words | grep -w $(jot -r 1 1 $n) | cut -f2)
chrlen=${#sWord1}
#echo $chrlen
if [ $chrlen -lt 4 ];
then
GetRandomWord
elif [ $chrlen -gt 8 ];
then
GetRandomWord
fi
#echo $sWord1
}

function GetRandomWordMod()
{
GetRandomWord
#echo $sWord1
sWord2=$(echo "$(tr '[:lower:]' '[:upper:]' <<< ${sWord1:0:1})${sWord1:1}")
if [[ $sWord2 == *"a"* ]]; then
sWord3=$(echo "$sWord2" | sed 's/a/@/g')
elif [[ $sWord2 == *"e"* ]]; then
sWord3=$(echo "$sWord2" | sed 's/e/3/g')
elif [[ $sWord2 == *"i"* ]]; then
sWord3=$(echo "$sWord2" | sed 's/i/1/g')
elif [[ $sWord2 == *"o"* ]]; then
sWord3=$(echo "$sWord2" | sed 's/o/0/g')
elif [[ $sWord2 == *"u"* ]]; then
sWord3=$(echo "$sWord2" | sed 's/u/_/g')
elif [[ $sWord2 == *"y"* ]]; then
sWord3=$(echo "$sWord2" | sed 's/y/-/g')
fi
#echo $sWord3
}
# Get Random Word - Stop

GetRandomWordMod
sPWDPart2=$sWord3

GetRandomWordMod
sPWDPart3=$sWord3

sFullPWD=$(echo "$sPWDPart1.$sPWDPart2.$sPWDPart3")

echo "Write down this ${#sFullPWD} character password:\n$sFullPWD"
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.