/
hash.clj
27 lines (25 loc) · 877 Bytes
/
hash.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(ns csele.hash
(:require [byte-streams :as bs])
(:import [java.io InputStream]
[java.util Base64]
[java.security MessageDigest]
[org.apache.commons.codec.binary Hex]
[org.bouncycastle.jcajce.provider.digest SHA3$DigestSHA3]))
(defn hash-hex
"SHA3 hash of string"
([input] (hash-hex input 512))
([^String input ^Integer strength]
(let [bytes (.getBytes input)
sha3 (SHA3$DigestSHA3. strength)]
(.update sha3 bytes)
(Hex/encodeHexString (.digest sha3)))))
(defn hash-base64
"Base64 encoded SHA-256 hash of input."
[input]
(if (instance? InputStream input) (.reset ^InputStream input))
(let [bytes (bs/to-byte-array input)
bowel (MessageDigest/getInstance "SHA-256")
encoder (Base64/getEncoder)]
(->> bytes
(.digest bowel)
(.encodeToString encoder))))