어제 밤에 개삽질 했던거, 10분 만에 끝냄.


오늘의 교훈: 5시 이후엔 일하지 말자



(defonce hmac-sha256 (Mac/getInstance "HmacSHA256"))


(defn- key+mac->secret-key-spec [^bytes key mac]

  (SecretKeySpec. key (.getAlgorithm mac)))


(defn- secret-key-spec+message->bytes [secret-key-spec message]

  (.init hmac-sha256 secret-key-spec)

  (->> "UTF-8"

       (.getBytes message)

       (.doFinal hmac-sha256)))


(defn message->signature-bytes [^bytes key message]

  (-> key

      (key+mac->secret-key-spec hmac-sha256)

      (secret-key-spec+message->bytes message)))


(defn- byte-array->hex-string ^String [^bytes byte-array]

  (->> byte-array

       (map #(bit-and % 0xff)) ;; signed -> unsigned

       (clojure.pprint/cl-format false "~{~(~2'0X~)~}")))


(defn compute-aws4-signing-key [key now & next-components]

  (loop [[msg & more-msg] `[~(time->basic-date-str now) ~@next-components]

         k (.getBytes (str "AWS4" key) "UTF8")]

    (if msg

      (recur more-msg (message->signature-bytes k msg))

      (byte-array->hex-string k))))