Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
the correct way to use SecureRandom in clojure. automatically reseeds every ttl ms
(defn- reseeding-prng [ttl]
(let [state (atom {})]
(fn []
(let [now (.getTime (java.util.Date.))]
(when (> (- now (:last-seeded @state 0)) ttl)
(let [new-generator (java.security.SecureRandom/getInstance "SHA1PRNG" "SUN")]
(.nextBytes new-generator (byte-array 0))
(swap! state assoc :last-seeded now :generator new-generator)))
(:generator @state)))))
(def ^:private prng (reseeding-prng (* 60 60 1000)))
(defn- random-id []
(.toString (BigInteger. 160 (prng)) 36))
@rufoa

This comment has been minimized.

Copy link
Owner Author

@rufoa rufoa commented Jun 4, 2015

don't use SHA1 these days though

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