Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of Argon2 key derivation function to xiana/hash #280

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
com.draines/postal {:mvn/version "2.0.5"}
com.flexiana/tiny-rbac {:mvn/version "0.1.1"}
com.taoensso/timbre {:mvn/version "5.2.1"}
crypto-password/crypto-password {:mvn/version "0.3.0"}
;; crypto-password/crypto-password {:mvn/version "0.3.0"}
crypto-password/crypto-password {:git/url "https://github.com/Flexiana/crypto-password"
:sha "cfd90d519e09797a97ded565a1e27c0b938771f1"}
funcool/cuerdas {:mvn/version "2.2.1"}
info.sunng/ring-jetty9-adapter {:mvn/version "0.30.1"}
metosin/malli {:mvn/version "0.8.4"}
Expand Down
18 changes: 17 additions & 1 deletion src/xiana/hash.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
Supported algorithms are bcrypr, pbkdf2, and scrypt.
The required algorithm should be in (-> state :deps :auth :hash-algorithm)"
(:require
[crypto.password.argon2 :as argon2]
[crypto.password.bcrypt :as hash-b]
[crypto.password.pbkdf2 :as hash-p]
[crypto.password.scrypt :as hash-s]))

(def supported [:bcrypt :pbkdf2 :scrypt])
(def supported [:bcrypt :pbkdf2 :scrypt :argon2])

(defn- dispatch
([state password]
Expand Down Expand Up @@ -49,6 +50,18 @@
(if (= :sha1 (:type pbkdf2-settings))
"HMAC-SHA1" "HMAC-SHA256")))

(defmethod make :argon2
[{{:keys [argon2-settings]
:or {argon2-settings {:iterations 22
:memory-cost 65536
:parallelization 1}}} :deps/auth}
password]
(argon2/encrypt
password
(:iterations argon2-settings)
(:memory-cost argon2-settings)
(:parallelization argon2-settings)))

(defmulti check
"Validating password."
dispatch)
Expand All @@ -61,3 +74,6 @@

(defmethod check :pbkdf2 [_ password encrypted]
(hash-p/check password encrypted))

(defmethod check :argon2 [_ password encrypted]
(argon2/check password encrypted))
5 changes: 3 additions & 2 deletions test/xiana/hash_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
(testing-mistake fragment)
(testing-ok fragment)))

(deftest test-assert-functionality
(deftest test-full-functionality-argon2
(let [fragment {:deps {:auth {:hash-algorithm :argon2}}}]
(is (thrown? java.lang.AssertionError (hash/make fragment password)))))
(testing-mistake fragment)
(testing-ok fragment)))

(deftest hash-behavior
(let [pwd "not_nil"
Expand Down
Loading