Skip to content

Instantly share code, notes, and snippets.

@jsl
Created January 16, 2013 15:26
Show Gist options
  • Save jsl/4547933 to your computer and use it in GitHub Desktop.
Save jsl/4547933 to your computer and use it in GitHub Desktop.
How to calculate the default password hash used by Authlogic
(defn sha-512
"Return the SHA-512 of the given string"
[data]
(let [md (. java.security.MessageDigest getInstance "SHA-512")]
(. md update (.getBytes data))
(let [bytes (. md digest)]
(reduce #(str %1 (format "%02x" %2)) "" bytes))))
(defn hash-repeatedly
"Hash the given string n number of times"
([str times] (hash-repeatedly str times 0))
([str times cur]
(if (< (- times 1) cur)
str
(recur (sha-512 str) times (inc cur)))))
user> (hash-repeatedly (str password salt) 20)
"371df0048ddbc0da3f098437a65d100abcc5d709166840e017a92934f0cdd2c6bc5da156b782b4b45ddbab60838328cb6ab766f72eb6e38a844909392679c3d9"
@jsl
Copy link
Author

jsl commented Jan 16, 2013

Authlogic (a Rails plugin for authentication) defaults to applying the SHA-512 hashing algorithm 20 times to the concatenated users' password + salt. The above will give you the same result using Clojure and the java.security.MessageDigest library.

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