* Add javax.crypto classes necessary for calculating hmac shas. * Add hmac-256-sha test for javax.crypto.
31 lines
1.3 KiB
Clojure
31 lines
1.3 KiB
Clojure
(ns babashka.crypto-test
|
|
(:require [babashka.test-utils :as test-utils]
|
|
[clojure.edn :as edn]
|
|
[clojure.test :refer [deftest is]])
|
|
(:import (javax.crypto Mac)
|
|
(javax.crypto.spec SecretKeySpec)))
|
|
|
|
(defn bb [& exprs]
|
|
(edn/read-string (apply test-utils/bb nil (map str exprs))))
|
|
|
|
(defn hmac-sha-256 [key data]
|
|
(let [algo "HmacSHA256"
|
|
mac (Mac/getInstance algo)]
|
|
(.init mac (SecretKeySpec. key algo))
|
|
(.doFinal mac (.getBytes data "UTF-8"))))
|
|
|
|
(deftest hmac-sha-256-test
|
|
(let [key-s "some-key"
|
|
data "some-data"
|
|
expected-sha (String. (hmac-sha-256 (.getBytes key-s) data))]
|
|
(is (= expected-sha (bb '(do (ns net
|
|
(:import (javax.crypto Mac)
|
|
(javax.crypto.spec SecretKeySpec)))
|
|
(defn hmac-sha-256 [key data]
|
|
(let [algo "HmacSHA256"
|
|
mac (Mac/getInstance algo)]
|
|
(.init mac (SecretKeySpec. key algo))
|
|
(.doFinal mac (.getBytes data "UTF-8"))))
|
|
(let [key-s "some-key"
|
|
data "some-data"]
|
|
(String. (hmac-sha-256 (.getBytes key-s) data)))))))))
|