diff options
| author | Scott Ostler <scottbot9000@gmail.com> | 2010-10-24 20:46:30 -0400 |
|---|---|---|
| committer | Scott Ostler <scottbot9000@gmail.com> | 2010-10-24 20:46:30 -0400 |
| commit | 33955c93ae8050778c75c18756585a59103ea86f (patch) | |
| tree | 3d59aded387fa0782e9eb8e59ea31bc699671148 /src/utils.clj | |
| parent | 02a0597e39ed337ba4dc0cccddebb4c98c63b5ee (diff) | |
Add initial event logic and merge
Diffstat (limited to 'src/utils.clj')
| -rwxr-xr-x | src/utils.clj | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/utils.clj b/src/utils.clj index 9460bbc..08b06ab 100755 --- a/src/utils.clj +++ b/src/utils.clj @@ -6,7 +6,12 @@ java.io.File java.net.URLDecoder javax.sql.DataSource + javax.crypto.Cipher + javax.crypto.spec.SecretKeySpec + javax.crypto.KeyGenerator + javax.crypto.Mac org.postgresql.ds.PGPoolingDataSource + org.apache.commons.codec.binary.Base64 org.apache.commons.codec.digest.DigestUtils org.antlr.stringtemplate.StringTemplateGroup) (:use clojure.contrib.json.write @@ -297,7 +302,44 @@ (doseq [[i o] (map vector (iterate inc 1) objects)] (.setObject stmt i o)) (.executeQuery stmt)))) - + +;; Crypto + +(def b64codec (Base64.)) + +(defn b64enc [bytes] + (.encodeToString b64codec bytes)) + +(defn b64dec [s] + (.decode b64codec s)) + +(defn generate-aes-key [bits] + (let [kgen (doto (KeyGenerator/getInstance "AES") + (.init bits)) + skey (.generateKey kgen)] + (.getEncoded skey))) + +(defn aes-encoder [secret] + (let [spec (SecretKeySpec. secret "AES") + cipher (Cipher/getInstance "AES")] + (.init cipher Cipher/ENCRYPT_MODE spec) + (fn [input] + (.doFinal cipher input)))) + +(defn aes-decoder [secret] + (let [spec (SecretKeySpec. secret "AES") + cipher (Cipher/getInstance "AES")] + (.init cipher Cipher/DECRYPT_MODE spec) + (fn [input] + (.doFinal cipher input)))) + + +(defn make-signer [secret] + (let [algo "HmacSHA1" + spec (SecretKeySpec. (.getBytes secret) algo) + mac (Mac/getInstance algo)] + (.init mac spec) + (fn [input] (.doFinal mac (.getBytes input))))) ;; Parsing |
