Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea | ||
*.iml | ||
.DS_STORE | ||
.lein-repl-history | ||
.nrepl-port | ||
pom.xml | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# adyen-signature | ||
Easy way to sign adyen request | ||
A clojure library designed to sign adyen requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)))) |