Skip to content

Commit

Permalink
Add support of Argon2 key derivation function to xiana/hash (#280)
Browse files Browse the repository at this point in the history
* temporary solution

* import argon2 functionality

* add argon2 keyword to supported list for dispatch check

* make feature

* check feature

* remove redundant test

* argon2 functionality test

* deps.edn imports last change

* cljstyle fix
  • Loading branch information
theodor-meresescu authored Feb 9, 2024
1 parent 8145743 commit 57f4091
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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

0 comments on commit 57f4091

Please sign in to comment.