UNIX Bash – Random Secure Password

| | | | | | |

Tested on Raspberry Pi but should work on any Debian based OS

# Install Dictionaries
if [ $(dpkg-query -W -f='${Status}' wamerican 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  sudo apt install wamerican -y;
  else
  echo 'Already Installed';
fi

if [ $(dpkg-query -W -f='${Status}' wbritish 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  sudo apt install wbritish -y;
  else
  echo 'Already Installed';
fi

# This will combine all words into a new file that we can filter for bad words.
sort /usr/share/dict/american-english /usr/share/dict/british-english /usr/share/dict/words | uniq | sudo dd of=words-password

# This will be your badwordlist, fill it with the words you would not like to see in a password
sudo touch /usr/share/dict/badwordlist

NOTE: With the three languages included you may get some words for your password that may be offensive if you choose not to put words in the badwordlist file. If so just run the script again.

#!/bin/bash

CleanUpPassword () {
  DirtyPassword="$1"
  #echo "Dirty Password: $DirtyPassword"

  # Replace i, I, l with a number one
  CleanPassword=$(echo "$DirtyPassword" | sed "s|i|1|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|I|1|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|l|1|g" )

  # Replace a, A with @
  CleanPassword=$(echo "$CleanPassword" | sed "s|a|@|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|A|@|g" )

  # Replace e, E with 3
  CleanPassword=$(echo "$CleanPassword" | sed "s|e|3|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|E|3|g" )

  # Replace s, S with $
  CleanPassword=$(echo "$CleanPassword" | sed "s|s|$|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|S|$|g" )

  # Replace o, O with a number zero
  CleanPassword=$(echo "$CleanPassword" | sed "s|o|0|g" )
  CleanPassword=$(echo "$CleanPassword" | sed "s|O|0|g" )

  #echo "Clean Password: $CleanPassword"
  echo "$CleanPassword"
}

# Get Random Word - Start
GetRandomWord () {
passwordwordlist=/usr/share/dict/words-password

sWord1=$(shuf -n1 $passwordwordlist);
chrlen=${#sWord1}
#echo $chrlen;
if [ $chrlen -lt 4 ];
then
GetRandomWord;
elif [ $chrlen -gt 8 ];
then
GetRandomWord;
fi
#echo $sWord1;
}

GetRandomWordMod () {
  GetRandomWord
  #echo $sWord1
  
first=`echo $sWord1|cut -c1|tr [a-z] [A-Z]`;
second=`echo $sWord1|cut -c2-`;
sWord2=$(echo $first$second);

    sWord3=$(echo "$sWord2" | sed 's/a/@/g' | sed 's/e/3/g' | sed 's/i/1/g' | sed 's/o/0/g' | sed 's/u/_/g' | sed 's/y/-/g')
 
  #echo $sWord3
}

CreatePassword () {
  # Change Disk Password - Start
  # 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

  GetRandomWordMod
  sPWDPart2=$sWord3

  GetRandomWordMod
  sPWDPart3=$sWord3

  sFullPWD=$(echo "$sPWDPart1.$sPWDPart2.$sPWDPart3")
  #sFullPWD=$(cat ~/FileVault.txt)
  # Change Disk Password - Stop

  printf "\nProposed Password:\n$sFullPWD\n\nAcceptable?, if yes type y or if no type n followed by [ENTER]:\n"
  read sPWDCreated
  case $sPWDCreated in
    [yY])
      
      clear

      echo "\nWrite down this ${#sFullPWD} character password:\n$sFullPWD"

     
      ;;
    *)
      CreatePassword
      ;;
  esac

}

CreatePassword
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.