Skip to content

Instantly share code, notes, and snippets.

@jasonneylon
Created November 30, 2016 10:10

Revisions

  1. jasonneylon created this gist Nov 30, 2016.
    28 changes: 28 additions & 0 deletions passphrases.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    (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)