Skip to content

Instantly share code, notes, and snippets.

@i-blis
Created August 10, 2021 01:16
Show Gist options
  • Save i-blis/b2f09e35bf7332ecf97e0e66b755ca9e to your computer and use it in GitHub Desktop.
Save i-blis/b2f09e35bf7332ecf97e0e66b755ca9e to your computer and use it in GitHub Desktop.
Simple Password Generator
#!/usr/bin/env bb
(require '[clojure.tools.cli :refer [parse-opts]])
(def generator (java.security.SecureRandom.))
(defn gen-pass [alphabet length]
(let [bound (count alphabet)
idxs (repeatedly length #(.nextInt generator bound))]
(apply str (map alphabet idxs))))
(def symbols {:uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
:lowercase "abcedfghijklmnopqrstuvwxyz"
:numerical "0123456789"
:special "!@#$%^&*"})
(def default-length 16)
(let [parsed (-> *command-line-args*
(parse-opts [["-u" "--uppercase"]
["-l" "--lowercase"]
["-n" "--numerical"]
["-s" "--special"]
["-x" "--extras EXTRAS"]]))
options (parsed :options)
flags (or (keys options) (keys symbols))
length (edn/read-string (first (:arguments parsed)))
length (if (integer? length) length default-length)
alphabet (->> flags (select-keys symbols) vals
(apply str (options :extras)) vec)]
(print (gen-pass alphabet length)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment