Skip to content

Commit

Permalink
Add core file
Browse files Browse the repository at this point in the history
  • Loading branch information
rackdon committed Aug 23, 2017
1 parent 573df35 commit baa2549
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.idea
*.iml
.DS_STORE
.lein-repl-history
.nrepl-port
pom.xml
/target
2 changes: 1 addition & 1 deletion README.md
@@ -1,2 +1,2 @@
# adyen-signature
Easy way to sign adyen request
A clojure library designed to sign adyen requests
8 changes: 8 additions & 0 deletions project.clj
@@ -0,0 +1,8 @@
(defproject adyen-signature "0.1.0"
:description "A clojure library designed to sign adyen requests"
:url "https://github.com/rackdon/adyen-signature"
:license {:name "MIT License"}

:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/data.codec "0.1.0"]
[com.google.guava/guava "23.0"]])
43 changes: 43 additions & 0 deletions src/adyen_signature/core.clj
@@ -0,0 +1,43 @@
(ns adyen-signature.core
(:require [clojure.data.codec.base64 :as b64]
[clojure.string :as string])
(:import [javax.crypto.spec SecretKeySpec]
[javax.crypto Mac]
[com.google.common.io BaseEncoding]))

(defn to-camel-case [s]
(as-> (name s) $
(string/lower-case $)
(string/split $ #"-")
(map-indexed (fn [idx itm] (if (= idx 0) itm (string/capitalize itm))) $)
(string/join $)))

(defn replace-values [s]
(if (nil? s)
""
(string/replace s #"\\|:" {"\\" "\\\\" ":" "\\:"})))

(defn to-byte-array [s]
"Convert an string to byte array"
(into-array Byte/TYPE (.getBytes s "UTF-8")))

(defn decode-hex [s]
"Convert hex string into Byte Array"
(.decode (BaseEncoding/base16) s))

(defn encode-b64-string [ba]
"Convert a byte array to a base64 String"
(String. (b64/encode ba) "UTF-8"))

(defn sign [secret data]
(let [sha256-mac (Mac/getInstance "HmacSHA256")
signing-key (SecretKeySpec. (decode-hex secret) "HmacSHA256")
byte-data (->> (map (fn [[k v]] [(to-camel-case k) (replace-values v)]) (dissoc data :merchant-sig))
sort
(into {})
((fn [coll] (concat (keys coll) (vals coll))))
(string/join ":")
to-byte-array)]

(.init sha256-mac signing-key)
(encode-b64-string (.doFinal sha256-mac byte-data))))

0 comments on commit baa2549

Please sign in to comment.