XKPassword for Elixir: Memorable Passwords, Ten Years On
I've just published XKPassword 0.1.0 on Hex. It's a small Elixir library, with a command-line tool, that makes passwords out of a few random words: kerry-lewis-jumping-AERIAL rather than q7#Lp!2v. The source is on GitHub under the MIT licence.
It's also a port of a Ruby gem I wrote ten years ago, so this post is partly about where it came from.
Where it started: Orpiva and Ruby
In 2013 I joined Orpiva, then called ClothesNetwork, as a remote software engineer. Back then Orpiva was a curated fashion marketplace across Europe, where customers could try products on virtually, shop together with friends and buy; today, as Orpiva AI, it's a product focused on influencers. It was also where I learned Ruby. Before Orpiva I was writing PHP; there I spent three years on Ruby on Rails, Sinatra, PostgreSQL and Redis, and Ruby has been part of how I think about code ever since.
Bjoern Rennhak, Orpiva's CTO, once sent me a cartoon over Skype: xkcd's Password Strength. Its argument is that Tr0ub4dor&3, the kind of password we're told is strong, is hard for people to remember and easy for computers to guess, while four random common words like correct horse battery staple are the opposite. I didn't believe it at first. Then I did the arithmetic, and it holds up.
The Ruby gem, 2016
In August 2016 I was building a new blog and wanted a password generator for it, so I wrote one over a couple of days and published it as the xkpassword gem: a library for Ruby apps and an xkpassword command for the terminal. It picks a word length, picks a word of that length from a dictionary of common English words, repeats, and joins the words with a separator. The README promised more updates "over the next few months", and then I went back to building the blog.
The gem stayed quiet for a long time. In 2021 I took the swear words out of the dictionary, which matters more than it sounds when a password is shown to a customer. This April I came back to it properly: case transforms, presets for common uses, a ~/.xkpassword file for CLI defaults, and support for the Ruby versions still maintained today (3.3, 3.4 and 4.0).
Why Elixir now
These days most of what I build is in Elixir and Phoenix, including this site and Chimes, my church management software. I'm building a demo mode for the Chimes demo server: you leave your details, and you get your own admin account with a freshly generated password. Someone trying Chimes for the first time has to read that password and type it in, and maybe come back to it a day later, so it should be easy to read and remember. That's exactly what XKPassword was for, but I didn't want to shell out to Ruby or copy a word list into the project. So I ported the gem.
The Elixir version uses the same dictionary and the same presets as the Ruby gem, and the CLI reads the same ~/.xkpassword file, so if you use both, they behave the same.
Using it
Add it to mix.exs (Elixir 1.17 or later):
{:xkpassword, "~> 0.1.0"}
Then generate passwords:
XKPassword.generate()
#=> "kerry-lewis-jumping-AERIAL"
XKPassword.generate(preset: :security, separator: ".")
#=> "showers.systems.fact.dvds.sending.dollar"
XKPassword.generate(words: 5, min_length: 5, max_length: 8, case_transform: :capitalize)
generate/1 takes a keyword list or a map. A preset sets the defaults and anything you pass explicitly overrides it. The case transforms are :upcase, :downcase and :capitalize; leave it out to upper-case each word at random. Bad options raise an ArgumentError instead of quietly producing a weaker password.
| Preset | Words | Word length | Separator | Case |
|---|---|---|---|---|
:xkcd (default) |
4 | 4–8 | Hyphen | Random per word |
:web32 |
4 | 4–5 | Hyphen | Random per word |
:wifi |
6 | 4–8 | Hyphen | Random per word |
:security |
6 | 4–8 | Space | Lowercase |
:apple_id |
3 | 4–7 | Hyphen | Random per word |
For the command line, install it as an escript:
mix escript.install hex xkpassword
xkpassword
xkpassword --preset apple-id --separator .
xkpassword --init # writes a commented ~/.xkpassword
Make sure ~/.mix/escripts is on your PATH. CLI flags override the file, and both override the preset.
How strong are these?
The useful way to measure a generated password is to assume the attacker knows everything except the random choices: the dictionary, the preset, even that you used this library. What's left is the number of passwords the generator could have produced.
The dictionary has 9,893 words. With the default preset, each word comes from one of five lengths (4 to 8 letters, with between 1,100 and 1,491 words at each), and each word is upper-cased or not at random. That's about 13.7 bits of entropy per word, or about 55 bits for four words. The comic's example was 44 bits. Every extra bit doubles the guessing work, so the default is well over a thousand times harder to guess, and still easy to type and remember.
By the same measure, :apple_id is about 40 bits, :web32 about 49, :security about 76 and :wifi about 82. For anything that matters, use more words: it's the cheapest way to add strength.
One thing the port fixed
Porting a library makes you read every line again. Until version 0.6.0, the Ruby gem picked words with Array#sample, which uses Ruby's default random number generator. That's fine for shuffling a playlist, but not what you want for passwords. The Elixir version takes its randomness from Erlang's :crypto.strong_rand_bytes/1, and throws away the values that would make some words slightly more likely than others, so every choice is uniform.
The Ruby gem now does the same: version 0.7.0 chooses word lengths, words and random upper-casing with SecureRandom. If you use the gem, update to 0.7.0.
The dictionary is embedded at compile time, grouped by word length, so generating a password doesn't read any files.
What's next
XKPassword is at 0.1.0 and does what I need. If you use it and want something it doesn't do, open an issue on GitHub. And thanks to Bjoern for the comic, ten years ago, and to Orpiva for Ruby.