Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Last active September 30, 2023 03:17
Show Gist options
  • Save joshuakfarrar/91b3f5a4fd54987f365e0c234b64034f to your computer and use it in GitHub Desktop.
Save joshuakfarrar/91b3f5a4fd54987f365e0c234b64034f to your computer and use it in GitHub Desktop.
implementing Bouncy Castle's argon2 in clojure, from the Leiningen repl
(import org.bouncycastle.crypto.generators.Argon2BytesGenerator)
(import org.bouncycastle.crypto.params.Argon2Parameters)
(import org.bouncycastle.crypto.params.Argon2Parameters$Builder)
(import org.bouncycastle.util.encoders.Hex)
(defn argon2-hash
[version iterations memory parallelism password salt output-length]
(let [builder (doto (Argon2Parameters$Builder. Argon2Parameters/ARGON2_i)
(.withVersion version)
(.withIterations iterations)
(.withMemoryPowOfTwo memory)
(.withParallelism parallelism)
(.withSalt (.getBytes salt "UTF-8")))
params (.build builder)
gen (doto (Argon2BytesGenerator.)
(.init params))
result (byte-array output-length)]
(.generateBytes gen (.toCharArray password) result 0 (alength result))
result))
(-> (argon2-hash Argon2Parameters/ARGON2_VERSION_13 2 16 1 "password" "somesalt" 32) (Hex/toHexString))
; "c1628832147d9720c5bd1cfd61367078729f6dfb6f8fea9ff98158e0d7816ed0" 🎉
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment