Skip to content

Instantly share code, notes, and snippets.

@jramb
Created March 5, 2012 13:50
Show Gist options
  • Save jramb/1978394 to your computer and use it in GitHub Desktop.
Save jramb/1978394 to your computer and use it in GitHub Desktop.
Password hashing in Clojure
(defn hash-password [password salt]
(assert (> (count salt) 10)) ;would like to have >64 bit of salt
(assert (> (count password) 6)) ;come on, how low can we go?
(let [md (java.security.MessageDigest/getInstance "SHA-512")
encoder (sun.misc.BASE64Encoder.)]
(.update md (.getBytes salt "UTF-8")) ;assume text salt
(.encode encoder
(loop [mangle (.getBytes password "UTF-8")
passes 1e5] ; paranoid, but are we paranoid enough?
(if (= 0 passes)
mangle
(recur (.digest md mangle) (dec passes)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment