Thursday, June 6, 2019

Password generator with bash

Looks like the best way to handle your passwords strongly depends on which password breaking approach is the most effective at the moment, and apparently those ways are changing as quickly as technology develops. Which is why it's unlikely that there will ever be a perfect way to avoid all the risks, but we can design an optimal one.

It occurred to me that first we need to select between two strategies: storing generated password or memorizing them with mnemonics. I don't think it would be a good idea to store memorizable data cause it's too easy to break the pattern, or to try to remember a generated one. As I already can do mnemonics, so I decided to write my own password generator (I like having my own collection of tools).

Solution is based on LINK. Stuff in use: cat, tr, fold, head, /dev/urandom, echo, printf, for each and parameters testing

#!/bin/bash

# settings the defaults

type=[:graph:]
length=32
qty=10

# checking and reassigning input wherever applicable   

echo "Quantity of parameters passed: " $#

if [ -z $1 ] && [ -z $2 ] && [ -z $3 ]; then
  printf "Using default values type: ${type}, length: ${length}, qty: ${qty} \n"

    else

      if [ "$#" -eq 3 ]; then
 
        if [ "${1}" != "a" ] && [ "${2}" != "s" ] && [ "${3}" != "h" ]; then
        printf "Using default values type: ${type}, length: ${length}, qty: ${qty} \n"
        fi

        if [ "${1}" == "a" ]; then
       type=[:digit:][:alnum:]
        echo "Selecting charset alphanumeric aka ${type}"
        fi

        if [ -n $1 ] && [ "${1}" == "s" ]; then
          type=[:punct:][:digit:][:alnum:]
          echo "Selecting charset alphanumeric + special chars aka ${type}"
        fi

        if [ -n $1 ] && [ "${1}" == "h" ]; then
          type=[:xdigit:]
          echo "Selecting charset hexadecimals aka ${type}"
        fi

        if [ -n $2 ] && [ "${2}" -gt 7 ] && [ "${2}" -lt 51 ]; then
          length=$2
          echo "Actual length is ${length}"
          else echo "Value is not between 8 and 50, using default value for length: ${length}"
        fi

        if [ -n $3 ] && [ "${2}" -gt 0 ] && [ "${2}" -lt 51 ]; then
          qty=$3
          echo "Actual qty is ${qty}"
          else echo "Value is not between 1 and 50, using default value for qty: ${qty}"
        fi

      else
        printf "Using default values type: ${type}, length: ${length}, qty: ${qty} \n"

      fi
fi

# generating strings

result=$(cat /dev/urandom | tr -dc ${type} | fold -w ${length} | head -n ${qty})
printf "\n== START ==\n"
for i in ${result}; do echo ${i}; done
printf "== END ==\n\n"

No comments:

Post a Comment