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.


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

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


ppgen.sh – passphrase generator script