Skip to content

Instantly share code, notes, and snippets.

@samth
Created October 15, 2012 15:10
Show Gist options
  • Save samth/3893002 to your computer and use it in GitHub Desktop.
Save samth/3893002 to your computer and use it in GitHub Desktop.
Generating XKCD-style passwords in Racket
#lang racket
(define dict "/etc/dictionaries-common/words")
(define (choose l) (list-ref l (random (length l))))
(define words (for/list ([i (file->lines dict)]
#:when (regexp-match? "^[a-z]*$" i)
#:when (3 . < . (string-length i))
#:when (10 . > . (string-length i)))
i))
(define (password)
(apply string-append
(add-between (for/list ([j 4])
(choose words))
" ")))
(module+ test
(require rackunit)
(for ([i 50])
(define e (password))
(check member (choose words) words)
(check regexp-match? "^[a-z]* [a-z]* [a-z]* [a-z]*$" e)
(check-true (< 19 (string-length e) 39))))
@samth
Copy link
Author

samth commented Oct 16, 2012

You would need to provide a different implementation of the random function. However, since you're only generating one result, I doubt there's much point (esp. if you go through a few passwords until you find one you like).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment