Skip to content

Instantly share code, notes, and snippets.

@jhickner
Created April 14, 2012 07:00
Show Gist options
  • Select an option

  • Save jhickner/2382543 to your computer and use it in GitHub Desktop.

Select an option

Save jhickner/2382543 to your computer and use it in GitHub Desktop.

Revisions

  1. jhickner revised this gist Apr 14, 2012. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions 3.clj
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,17 @@
    (:import javax.crypto.spec.IvParameterSpec)
    (:import java.util.UUID))

    (defvar hmac-defaults
    {:algorithm "HmacSHA256"}
    "Default options for HMACs.")

    (defvar encrypt-defaults
    {:algorithm "AES"
    :key-size 128
    :mode "CBC"
    :padding "PKCS5Padding"}
    "Default options for symmetric encryption.")

    (defn- make-algorithm
    "Return an algorithm string suitable for JCE from a map of options."
    [options]
  2. jhickner revised this gist Apr 14, 2012. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions 3.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    (ns compojure.crypto
    "Functions for cryptographically signing, verifying and encrypting data."
    (:use compojure.encodings)
    (:use clojure.contrib.def)
    (:use clojure.contrib.java-utils)
    (:import java.security.SecureRandom)
    (:import javax.crypto.Cipher)
    (:import javax.crypto.KeyGenerator)
    (:import javax.crypto.Mac)
    (:import javax.crypto.spec.SecretKeySpec)
    (:import javax.crypto.spec.IvParameterSpec)
    (:import java.util.UUID))

    (defn- make-algorithm
    "Return an algorithm string suitable for JCE from a map of options."
    [options]
    (str "AES/" (options :mode) "/" (options :padding)))

    (defn- make-cipher
    "Create an AES Cipher instance."
    [options]
    (Cipher/getInstance (make-algorithm options)))

    (defn decrypt-bytes
    "Decrypts a byte array with the given key and encryption options."
    [options key data]
    (let [options (merge encrypt-defaults options)
    cipher (make-cipher options)
    [iv data] (split-at (.getBlockSize cipher) data)
    iv-spec (IvParameterSpec. (to-bytes iv))
    secret-key (SecretKeySpec. key (options :algorithm))]
    (.init cipher Cipher/DECRYPT_MODE secret-key iv-spec)
    (.doFinal cipher (to-bytes data))))

    (defn decrypt
    "Base64 encodes and encrypts a string with the given key and algorithm."
    [options key data]
    (String. (decrypt-bytes options key (base64-decode-bytes data))))
  3. jhickner revised this gist Apr 14, 2012. 2 changed files with 5 additions and 0 deletions.
    File renamed without changes.
    5 changes: 5 additions & 0 deletions 2.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    (defn hmac-sha-256
    [key-seq byte-seq]
    (let [hmac-key (SecretKeySpec. (byte-array key-seq) "HmacSHA256")
    hmac (doto (Mac/getInstance "HmacSHA256") (.init hmac-key))]
    (.doFinal hmac (byte-array byte-seq))))
  4. jhickner created this gist Apr 14, 2012.
    13 changes: 13 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    (ns oauth.digest
    (:import (javax.crypto Mac)
    (javax.crypto.spec SecretKeySpec)))

    (defn hmac
    "Calculate HMAC signature for given data."
    [^String key ^String data]
    (let [hmac-sha1 "HmacSHA1"
    signing-key (SecretKeySpec. (.getBytes key) hmac-sha1)
    mac (doto (Mac/getInstance hmac-sha1) (.init signing-key))]
    (String. (org.apache.commons.codec.binary.Base64/encodeBase64
    (.doFinal mac (.getBytes data)))
    "UTF-8")))