From d19d58d9228295c6f1db091931057d61ace6e507 Mon Sep 17 00:00:00 2001 From: Gustavo Valente Date: Thu, 2 Mar 2023 10:49:17 -0300 Subject: [PATCH 1/3] Add argon2 hash functions Co-authored-by: Soulflyer --- README.md | 3 ++- project.clj | 8 +++++--- src/crypto/password/argon2.clj | 15 +++++++++++++++ test/crypto/password/argon2_test.clj | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 src/crypto/password/argon2.clj create mode 100644 test/crypto/password/argon2_test.clj diff --git a/README.md b/README.md index 64825bf..edf4202 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A Clojure library for securing user passwords using a * [PBKDF2](http://en.wikipedia.org/wiki/PBKDF2) * [Bcrypt](http://bcrypt.sourceforge.net/) * [scrypt](http://www.tarsnap.com/scrypt.html) +* [argon2](http://https://github.com/phxql/argon2-jvm) [1]: http://en.wikipedia.org/wiki/Key_derivation_function @@ -19,7 +20,7 @@ Add the following dependency to your `project.clj` file: ## Usage -Pick an encryption algorithm, either `pbkdf2`, `bcrypt` or `scrypt`: +Pick an encryption algorithm, either `pbkdf2`, `bcrypt`, `scrypt` or `argon2`: ```clojure (require '[crypto.password. :as password]) diff --git a/project.clj b/project.clj index d54f032..f710b5d 100644 --- a/project.clj +++ b/project.clj @@ -8,17 +8,19 @@ [crypto-equality "1.0.0"] [commons-codec "1.15"] [at.favre.lib/bcrypt "0.9.0"] - [com.lambdaworks/scrypt "1.4.0"]] + [com.lambdaworks/scrypt "1.4.0"] + [de.mkammerer/argon2-jvm "2.11"]] :plugins [[lein-codox "0.9.4"]] :codox {:output-path "codox" :project {:name "Crypto-Password"} :metadata {:doc/format :markdown} :source-uri "http://github.com/weavejester/crypto-password/blob/{version}/{filepath}#L{line}"} - :aliases {"test-all" ["with-profile" "+1.6:+1.7:+1.8:+1.9:+1.10" "test"]} + :aliases {"test-all" ["with-profile" "+1.6:+1.7:+1.8:+1.9:+1.10:+1.11" "test"]} :profiles {:1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]} :1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]} :1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]} :1.9 {:dependencies [[org.clojure/clojure "1.9.0"]]} - :1.10 {:dependencies [[org.clojure/clojure "1.10.0"]]}}) + :1.10 {:dependencies [[org.clojure/clojure "1.10.0"]]} + :1.11 {:dependencies [[org.clojure/clojure "1.11.0"]]}}) diff --git a/src/crypto/password/argon2.clj b/src/crypto/password/argon2.clj new file mode 100644 index 0000000..9cc43fb --- /dev/null +++ b/src/crypto/password/argon2.clj @@ -0,0 +1,15 @@ +(ns crypto.password.argon2 + (:import (de.mkammerer.argon2 Argon2 Argon2Factory Argon2Advanced))) + +(def argon2 (Argon2Factory/create)) + +(defn encrypt + ([raw] (encrypt raw 10 65536 1)) + ([raw iter mem parallel] + (.hash argon2 iter mem parallel raw))) + +(defn check [raw hash] + (.verify argon2 hash raw)) + +(defn main [_ password] + (str password)) diff --git a/test/crypto/password/argon2_test.clj b/test/crypto/password/argon2_test.clj new file mode 100644 index 0000000..899fc4e --- /dev/null +++ b/test/crypto/password/argon2_test.clj @@ -0,0 +1,20 @@ +(ns crypto.password.argon2-test + (:require [clojure.test :refer [deftest are]] + [crypto.password.argon2 :as password])) + +(deftest test-passwords + (are [s] (password/check s (password/encrypt s)) + "a" + "foo" + "password" + "Testing" + "Test123" + "ÁäñßOÔ" + "großpösna" + "Some rather long pass phrase perhaps out of a book or poem") + + (are [s r] (not (password/check r (password/encrypt s))) + "a" "b" + "a" "a " + "aaaaa" "aaaaa\n" + "großpösna" "grossposna")) From 31a43d50fa5eb6ba2cc913ca052c17a881089606 Mon Sep 17 00:00:00 2001 From: Gustavo Valente Date: Thu, 2 Mar 2023 11:16:03 -0300 Subject: [PATCH 2/3] Add deps.edn --- deps.edn | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 deps.edn diff --git a/deps.edn b/deps.edn new file mode 100644 index 0000000..2b26319 --- /dev/null +++ b/deps.edn @@ -0,0 +1,8 @@ +{:paths ["src"] + :deps {org.clojure/clojure {:mvn/version "1.11.1"} + crypto-random {:mvn/version "1.2.1"} + crypto-equality {:mvn/version "1.0.1"} + commons-codec {:mvn/version "1.15"} + at.favre.lib/bcrypt {:mvn/version "0.9.0"} + com.lambdaworks/scrypt {:mvn/version "1.4.0"} + de.mkammerer/argon2-jvm {:mvn/version "2.11"}}} From 53644ab14b923061c477cf44a03e211b82be7fe2 Mon Sep 17 00:00:00 2001 From: Marius Rabenarivo Date: Fri, 3 Mar 2023 11:25:38 +0400 Subject: [PATCH 3/3] Correct URL to argon2 in the README and add docstrings --- README.md | 2 +- src/crypto/password/argon2.clj | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index edf4202..10f13ba 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A Clojure library for securing user passwords using a * [PBKDF2](http://en.wikipedia.org/wiki/PBKDF2) * [Bcrypt](http://bcrypt.sourceforge.net/) * [scrypt](http://www.tarsnap.com/scrypt.html) -* [argon2](http://https://github.com/phxql/argon2-jvm) +* [argon2](https://github.com/phxql/argon2-jvm) [1]: http://en.wikipedia.org/wiki/Key_derivation_function diff --git a/src/crypto/password/argon2.clj b/src/crypto/password/argon2.clj index 9cc43fb..f222b56 100644 --- a/src/crypto/password/argon2.clj +++ b/src/crypto/password/argon2.clj @@ -4,11 +4,33 @@ (def argon2 (Argon2Factory/create)) (defn encrypt + "Usage: + (encrypt raw) + (encrypt raw iter mem parallel) + + Parameters: + - raw (str): The raw string to be encrypted. + - iter (int): The number of iterations to perform. Defaults to 10 if not specified. + - mem (int): The amount of memory to use in kilobytes. Defaults to 65536 if not specified. + - parallel (int): The degree of parallelism to use. Defaults to 1 if not specified. + + Returns: + A byte array containing the encrypted string." ([raw] (encrypt raw 10 65536 1)) ([raw iter mem parallel] (.hash argon2 iter mem parallel raw))) -(defn check [raw hash] +(defn check + "Usage: + (check raw hash) + + Parameters: + - raw (str): The raw string to check against the hash. + - hash (byte-array): The Argon2 password hash to check against. + + Returns: + true if the raw string matches the hash, false otherwise." + [raw hash] (.verify argon2 hash raw)) (defn main [_ password]