Skip to content

Commit

Permalink
Add argon2 hash function
Browse files Browse the repository at this point in the history
Co-authored-by: Soulflyer <[email protected]>

Add deps.edn

Fix bad url in README

Remove unrelated code

Co-authored-by: Iain Wood <[email protected]>

Remove more unrelated code

Co-authored-by: Iain Wood <[email protected]>

Fix README and docstrings length

Fix format
  • Loading branch information
gmsvalente committed Jun 1, 2023
1 parent 2873bfa commit ed1fbeb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](https://github.com/phxql/argon2-jvm)

[1]: http://en.wikipedia.org/wiki/Key_derivation_function

Expand All @@ -19,7 +20,8 @@ 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.<algorithm> :as password])
Expand Down
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[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"
Expand Down
38 changes: 38 additions & 0 deletions src/crypto/password/argon2.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns crypto.password.argon2
"Functions for encrypting passwords using the recommended argon2 algorithm.
See: https://infosecscout.com/best-algorithm-password-storage
https://github.com/phxql/argon2-jvm"
(:import [de.mkammerer.argon2 Argon2 Argon2Factory Argon2Advanced]))

(def argon2 (Argon2Factory/create))

(def ^:private default-iterations
(Long/parseLong (System/getProperty "crypto.password.argon2.default-iterations" "22")))

(def ^:private default-memory-cost
(Long/parseLong (System/getProperty "crypto.password.argon2.default-memory-cost" "65536")))

(def ^:private default-parallelization-parameter
(Long/parseLong (System/getProperty "crypto.password.argon2.default-parallelization-parameter" "1")))

(defn encrypt
"Encrypt a password string using the argon2 algorithm. This function takes
three optional parameters:
* `iter` - the number of iterations, defaults to 22
* `mem` - the memory cost, defaults to 65536
* `parallel` - the parallelization parameter, defaults to 1"
([raw]
(encrypt raw
default-iterations
default-memory-cost
default-parallelization-parameter))
([raw iter mem parallel]
(.hash argon2 iter mem parallel raw)))

(defn check
"Compare a raw string with a string encrypted with the [[encrypt]]
function. Returns true if the string matches, false otherwise."
[raw hash]
(.verify argon2 hash raw))
20 changes: 20 additions & 0 deletions test/crypto/password/argon2_test.clj
Original file line number Diff line number Diff line change
@@ -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"))

0 comments on commit ed1fbeb

Please sign in to comment.