forked from weavejester/crypto-password
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
2873bfa
commit ed1fbeb
Showing
4 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |