aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2012-06-25 20:43:32 -0500
committerDavid Barksdale <amatus.amongus@gmail.com>2012-06-25 20:43:32 -0500
commit8d35fb5b10ea2697975e4dc23d5c0c206ff4a6bd (patch)
tree25309411670c13f46b619cfa509d020d87aa85b5 /src
parentbfd2c6357d450f4bd3023a3e83f54a64991ce203 (diff)
Implemented base32-encode.
Diffstat (limited to 'src')
-rw-r--r--src/clojure/foofs/util.clj23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/clojure/foofs/util.clj b/src/clojure/foofs/util.clj
index 04f2d90..4f050b0 100644
--- a/src/clojure/foofs/util.clj
+++ b/src/clojure/foofs/util.clj
@@ -31,3 +31,26 @@
(defmacro agent-do
[_agent f]
`(send ~_agent #(do ~f %)))
+
+(def base32-alphabet [\A \B \C \D \E \F \G \H \I
+ \J \K \L \M \N \O \P \Q \R
+ \S \T \U \V \W \X \Y \Z \2
+ \3 \4 \5 \6 \7])
+
+(defn to-32
+ [x]
+ (nth base32-alphabet (bit-and 0x1f x)))
+
+(defn base32-encode
+ [byte-seq]
+ (mapcat (fn
+ [[a b c d e]]
+ [(to-32 (bit-shift-right a 3))
+ (to-32 (bit-or (bit-shift-left a 2) (bit-shift-right b 6)))
+ (to-32 (bit-shift-right b 1))
+ (to-32 (bit-or (bit-shift-left b 4) (bit-shift-right c 4)))
+ (to-32 (bit-or (bit-shift-left c 1) (bit-shift-right d 7)))
+ (to-32 (bit-shift-right d 2))
+ (to-32 (bit-or (bit-shift-left d 3) (bit-shift-left e 5)))
+ (to-32 e)])
+ (partition 5 5 (repeat (byte 0)) byte-seq)))