Killring Rick Dillon's Weblog

Generating Random Passphrases

Good passwords are long, but easy to remember, so you can use one for each system you log in to. For years, I've simply memorized long strings of random characters for my important passwords, but in the past two years, I've integerated passphrases into the mix as well. Pass phrases are a series of words that you string together, often with numbers and/or special characters thrown in.

There are lots of tools to do this available, but I thought it would neat to base the word selection on my own corpus culled from literature available online. So, give a directory of titles downloaded from Project Gutenberg in ASCII, this Ruby script will generate a handful of four-word passphrases that you can spice up with numbers or special characters as you see fit.

It runs through all words in the files in the current directory, ranks them by frequency of occurance in the corpus, removes those of length less than four, grabs the top 5000 and samples four from that collection ten times, producing ten passphrases.

There are a ton of ways to make it faster, but I don't need to use it very often, and it is quite simple, so I thought I'd share.

#!/usr/bin/env ruby
words = {}.tap{|h| h.default = 0}
corpus = %x[ls].split.select{|n| n =~ /\.txt/}
  .map{|f| File.read(f)}.reduce(&:+).split
  .map{|w| w.downcase.gsub(/[^a-z]/,'')}
corpus.each{|w| words[w] += 1}
ranked_words = words.select{|k,_| k.length > 3}.sort_by{|_,v| -v}[0..5000]
10.times do
  puts ranked_words.sample(4).map(&:first).map(&:capitalize).join
end