-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.clj
63 lines (54 loc) · 1.78 KB
/
util.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/
(ns dev.gethop.object-storage.azure-blob-storage.util
(:import [java.net URLEncoder]
[java.time ZonedDateTime ZoneOffset]
[java.time.format DateTimeFormatter]
[java.util Base64]
[javax.crypto Mac]
[javax.crypto.spec SecretKeySpec]))
(defn encode-base64
"Encodes a byte[] as String using Base64"
[^bytes src]
(.encodeToString (Base64/getEncoder) src))
(defn decode-base64
"Returns a byte[] from a Base64 encoded String"
[^String src]
(.decode (Base64/getDecoder) src))
(defn hmac-sha256
"Returns the calculated HMAC SHA256 of 'data' using 'key'."
[^bytes key ^bytes data]
(let [algo "HmacSHA256"
mac (Mac/getInstance algo)]
(.init mac (SecretKeySpec. key algo))
(.doFinal mac data)))
(defn get-current-date-time-in-rfc-1123
[]
(.format
(ZonedDateTime/now ZoneOffset/UTC)
DateTimeFormatter/RFC_1123_DATE_TIME))
(defn parse-rfc-1123-date
[s]
(ZonedDateTime/parse s DateTimeFormatter/RFC_1123_DATE_TIME))
(defn update-map-keys
[m update-fn & args]
(reduce-kv
(fn [m k v]
(assoc m (apply update-fn k args) v))
{}
m))
(defn url-encode-str
[^String s]
(URLEncoder/encode s "UTF-8"))
(defn build-resource-url
([account container]
(format "%s://%s.blob.%s/%s"
(get account :default-endpoint-protocol)
(get account :name)
(get account :default-endpoint-suffix)
(url-encode-str (get container :name))))
([account container object-id]
(format "%s/%s"
(build-resource-url account container)
(url-encode-str object-id))))