-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signatures.clj
31 lines (27 loc) · 1.05 KB
/
signatures.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
28
29
30
31
(ns csele.signatures
(:require [csele.keys :as keys]
[byte-streams :as bs])
(:import [java.security Security Signature]
[java.io ByteArrayInputStream]
[java.util Base64]
[org.bouncycastle.jce.provider BouncyCastleProvider
JCERSAPublicKey]))
(Security/addProvider (BouncyCastleProvider.))
(def algo "SHA256withRSA")
(defn verify
"Expects a base64 encoded signature"
[^String signature actual-data public-key]
(let [sig (doto (Signature/getInstance algo)
(.initVerify ^JCERSAPublicKey
(keys/string-to-key public-key))
(.update (bs/to-byte-array actual-data)))]
(.verify sig
(.decode (Base64/getDecoder) signature))))
(defn sign
"Produces a base64 encoded signature"
[data private-key]
(let [sig (doto (Signature/getInstance algo)
(.initSign (keys/string-to-key private-key))
(.update (bs/to-byte-array data)))]
(.encodeToString (Base64/getEncoder)
(.sign sig))))