cl-diceware

https://github.com/billstclair/cl-diceware.git

git clone 'https://github.com/billstclair/cl-diceware.git'

(ql:quickload :cl-diceware)
2

cl-diceware

This is a Common Lisp implementation of the Diceware passphrase generation algorithm.

You can find a description of the algorithm at http://diceware.com

MIT license in LICENSE, and at the bottom of source files.

It's a lot more effort to get cryptographically secure random numbers in Windows, so I didn't bother.

Pretty standard lisp package. System definition in cl-diceware.asd.

Tested in Clozure Common Lisp (CCL) and Steel Bank Common Lisp (SBCL).

Command-line script

The “diceware” file is a bash script to print passphrases in a shell.

;; Run in CCL (assuming “ccl” will start the lisp).
./diceware [<count>]

;; Run in SBCL
LISP=sbcl ./diceware [<count>]

;; Run in other lisp
LISP=whatever LISP_OPTIONS="--noprint options" ./diceware [<count>]

<count> defaults to 5. Non-integer , e.g. “-h” or “–help”, prints help.

;; Use /dev/random instead of the default of /dev/urandom
;; (Or set CL_DICEWARE_REAL_RANDOM=t in your shell init file)
CL_DICEWARE_REAL_RANDOM=t ./diceware [<count>]

Function reference

All functions and variables below are exported from the cl-diceware package. Random bytes are fetched from /dev/random, except on Windows, where it uses cl:random.

with-/dev/random (&optional stream) &body body

A macro to bind an (unsigned-byte 8) stream to STREAM around the execution of BODY. Wrap calls to the RANDOM-xxx functions with this to prevent multiple opening and closing of /dev/random.

random-byte

Returns a random integer between 0 and 255 (inclusive).

random-integer limit

Returns a random integer >= 0 and < limit. Same as cl:random, but better randomness.

random-word

Returns a random word from the Diceware word list.

random-words count

Returns a list of COUNT random words from the Diceware word list.

random-words-string count

Returns a string containing COUNT random words, separated by spaces.

*real-random-p*

Variable controls whether to use /dev/random or /dev/urandom for random numbers. If true and not :USE-FEATURES, will use /dev/random, otherwise /dev/urandom. If EQ to :USE-FEATURES, will use /dev/random only if :CL-DICEWARE-REAL-RANDOM-P is in *FEATURES*. cl-diceware.asd pushes :CL-DICEWARE-REAL-RANDOM-P onto *FEATURES* if the CL_DICEWARE_REAL_RANDOM environment variable is non-blank.

Default: use /dev/urandom

Bill St. Clair <billstclair@gmail.com>
4 September 2015