Created
October 15, 2012 15:10
-
-
Save samth/3893002 to your computer and use it in GitHub Desktop.
Generating XKCD-style passwords in Racket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).