A simple passphrase generator in bash

A simple passphrase generator in bash

Published by Arun Isaac on

In other languages: தமிழ்

Tags: project, software, bash

Yet another passphrase generator, this time one in bash…

The Electronic Frontier Foundation has released a new set of wordlists with increased usability in mind. And, I can't be bothered to go get some real dice to generate a passphrase. So, I wrote myself a little bash script to randomly pick a passphrase.

The script obtains random numbers by reading /dev/random. The random-line function reads 2 random bytes (random numbers from 0 to 65535) and uses rejection sampling to produce random numbers uniformly distributed between 1 and the number of words in the word list. I am of course assuming that the word list does not have more than 65536 words.

Passphrases are much easier to remember than passwords. The only difficulty is that they can be very long to type out. I am still not sure how inconvenient this is going to be. But, let's see.

WORDLIST=~/eff_large_wordlist.txt
NO_OF_WORDS=6

function random-line {
    NO_OF_LINES=$1
    RANDOM_NO=$(od -An -N 2 -t u2 < /dev/random)
    if [[ $RANDOM_NO -lt $NO_OF_LINES ]]
    then
        echo $(expr $RANDOM_NO + 1)
    else
        random-line $NO_OF_LINES
    fi
}

NO_OF_LINES=$(wc -l < $WORDLIST)
for i in $(seq 1 $NO_OF_WORDS)
do
    RANDOM_LINE=$(random-line $NO_OF_LINES)
    RANDOM_WORD=$(sed -n ${RANDOM_LINE}p $WORDLIST | awk '{print $2}')
    printf "%s " $RANDOM_WORD
done
printf "\n"

Downloads

ppgen.sh – passphrase generator script