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

Correct URL to argon2 in the README and add docstrings #1

Open
wants to merge 3 commits into
base: add-argon2
Choose a base branch
from
Open
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
3 changes: 2 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,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.<algorithm> :as password])
Expand Down
8 changes: 8 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -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"}}}
8 changes: 5 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]}})
37 changes: 37 additions & 0 deletions src/crypto/password/argon2.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns crypto.password.argon2
(:import (de.mkammerer.argon2 Argon2 Argon2Factory Argon2Advanced)))

(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
"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]
(str password))
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"))