Skip to content

Instantly share code, notes, and snippets.

@jasonneylon
Created November 30, 2016 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonneylon/12fa59b83d2cebdca0bf4cd74352d4f1 to your computer and use it in GitHub Desktop.
Save jasonneylon/12fa59b83d2cebdca0bf4cd74352d4f1 to your computer and use it in GitHub Desktop.
Generate passphrases (and DNA!) using clojure.spec
(require '[clojure.spec :as s])
(require '[clojure.spec.gen :as gen])
;; PASSPHRASES - generate a random 5 word passphrase
;; naive approach was to generate a random string and contrain to match a five word pattern
;; however this hit 100 generation limit imposed by clojure.spec
(defn words [str] (clojure.string/split str #" "))
(def five-words? (comp (partial = 5) count words))
(gen/generate (s/gen (s/and string? five-words?)))
;; Our second attempt was to read the Linux word dictionary into a set
;; and generate a five word list from it
(def words
(-> (slurp "/usr/share/dict/words")
(clojure.string/split #"\n")
(->> (filter #(= 4 (count %))))))
(s/def ::word (s/and (set words)))
(s/def ::pass-phrase (s/every ::word :count 5))
(gen/generate (s/gen ::pass-phrase))
;; DNA
(s/def ::dna (s/every #{\G \T \C \A} :min-count 1))
(gen/generate (s/gen ::dna))
(s/exercise ::dna)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment