Created
October 9, 2014 11:47
-
-
Save ah45/59051b1a1fa5d1de74f1 to your computer and use it in GitHub Desktop.
ActiveDirectory objectGUID decoding
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
(ns active-directory.guid.util | |
(:require [clojure.string :as str] | |
[clojure.data.codec.base64 :as b64])) | |
(defn decode | |
"Decodes a Base64 encoded string into a sequence of bytes" | |
[b64s] | |
(b64/decode (.getBytes b64s))) | |
(defn byte->hex | |
"Convert a byte to a string of its hexadecimal value" | |
[b] | |
(let [i (.intValue (bit-and b 0xFF)) | |
p (if (< i 0xf) "0" "") | |
hs (java.lang.Integer/toHexString i)] | |
(str p hs))) | |
(defn bytes->hexes | |
"Convert a sequence of bytes to a sequence of hexadecimal strings" | |
[ba] | |
(map byte->hex ba)) | |
(defn base64string->guidstring | |
"Converts a Base64 encoded string into a GUID string | |
Example: | |
(base64string->guidstring \"fTchtIkBQUKLZWVxnq6VVw==\") | |
;=> \"b421377d-0189-4241-8b65-65719eae9557\"" | |
[b64s] | |
(let [hs (bytes->hexes (decode b64s)) | |
g1 (->> (take 4 hs) reverse (apply str)) | |
[g2 g3] (->> (drop 4 hs) | |
(take 4) | |
(partition 2) | |
(map #(->> % reverse (apply str)))) | |
[g4 g5] (->> (drop 8 hs) (split-at 2) (map #(apply str %))) | |
guid [g1 g2 g3 g4 g5]] | |
(str/join "-" guid))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment