Skip to content

Commit

Permalink
Added the gen-salt function and modified hash-password to handle its …
Browse files Browse the repository at this point in the history
…output.
  • Loading branch information
budu committed Jun 14, 2011
1 parent e5e0977 commit bc44deb
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/sandbar/auth.clj
Expand Up @@ -121,14 +121,25 @@
:else false)
(partition 2 config))))

(defn gen-salt
"Generate a secure salt."
([] (gen-salt 8))
([n]
(let [random (java.security.SecureRandom.)
salt (make-array Byte/TYPE n)]
(.nextBytes random salt)
salt)))

(defn hash-password
"Generate a hash from the password and salt. Default value of n is
*hash-delay*."
([password salt] (hash-password password salt *hash-delay*))
([password salt n]
(let [digest (java.security.MessageDigest/getInstance "SHA-256")]
(do (.reset digest)
(.update digest (.getBytes salt "UTF-8")))
(.update digest (if (string? salt)
(.getBytes salt "UTF-8")
salt)))
(loop [input (.digest digest (.getBytes password "UTF-8"))
count n]
(if (= count 0)
Expand Down

0 comments on commit bc44deb

Please sign in to comment.