summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-04-24 02:17:17 -0700
committerDavid Barksdale <amatus.amongus@gmail.com>2010-04-24 02:17:17 -0700
commit0cbcdb61463e16fc1a97c1dcf08badea96858c55 (patch)
tree92a35544da87520291e4328e59819d005a7dcd44
parent139eca816fcb2f012e7f6d43afb80a431b7283f9 (diff)
More work on encoding and decoding various gnunet messages.
-rw-r--r--src/org/gnu/clojure/gnunet/hello.clj22
-rw-r--r--src/org/gnu/clojure/gnunet/identity.clj19
-rw-r--r--src/org/gnu/clojure/gnunet/message.clj48
3 files changed, 66 insertions, 23 deletions
diff --git a/src/org/gnu/clojure/gnunet/hello.clj b/src/org/gnu/clojure/gnunet/hello.clj
index b024f63..d63edef 100644
--- a/src/org/gnu/clojure/gnunet/hello.clj
+++ b/src/org/gnu/clojure/gnunet/hello.clj
@@ -12,12 +12,16 @@
(encode-int64 (.getTime (:expiration transport)))
(:encoded-address transport)))
+(defn decode-transports-and-split
+ [a]
+ )
+
(defn encode-hello
"Encode a hello message."
- [public-key transports]
+ [hello]
(let [padding (encode-int32 0)
- encoded-key (encode-rsa-public-key public-key)
- encoded-transports (mapcat encode-transport transports)
+ encoded-key (encode-rsa-public-key (:public-key hello))
+ encoded-transports (mapcat encode-transport (:transports hello))
size (+ header-size
(count padding)
(count encoded-key)
@@ -27,3 +31,15 @@
padding
encoded-key
encoded-transports)))
+
+(defn decode-hello-and-split
+ [a]
+ (let [[header after-header] (decode-header-and-split a)
+ [padding after-padding] (split-at 4 after-header)
+ [public-key after-public-key] (decode-rsa-public-key-and-split
+ after-padding)
+ [transports after-transports] (decode-transports-and-split
+ after-public-key)]
+ [{:public-key public-key
+ :transports transports}
+ after-transports]))
diff --git a/src/org/gnu/clojure/gnunet/identity.clj b/src/org/gnu/clojure/gnunet/identity.clj
index f6cecfd..ec729ee 100644
--- a/src/org/gnu/clojure/gnunet/identity.clj
+++ b/src/org/gnu/clojure/gnunet/identity.clj
@@ -1,21 +1,12 @@
(ns org.gnu.clojure.gnunet.identity
(:use (org.gnu.clojure.gnunet crypto message)))
-(defn bit-count-to-bytes [x] (quot (+ 7 x) 8))
-
-(defn encode-bigint
- "Convert a bigInteger to a sequence of bytes in network order."
- [x]
- (let [len (bit-count-to-bytes (.bitLength x))
- a (.toByteArray x)]
- (drop (- (alength a) len) a)))
-
(defn encode-rsa-public-key
"Convert an RSA public key to a sequence of bytes in gnunet format."
- [key]
- (let [modulus (encode-bigint (.getModulus key))
+ [public-key]
+ (let [modulus (encode-int (.getModulus public-key))
modulus-len (count modulus)
- exponent (encode-bigint (.getPublicExponent key))
+ exponent (encode-int (.getPublicExponent public-key))
exponent-len (count exponent)]
(concat
(encode-int16 (+ modulus-len exponent-len 4))
@@ -24,6 +15,10 @@
exponent
(encode-int16 0))))
+(defn decode-rsa-public-key-and-split
+ [a]
+ )
+
(defn generate-id
"Generate the SHA-512 digest of the encoded public key."
[keypair]
diff --git a/src/org/gnu/clojure/gnunet/message.clj b/src/org/gnu/clojure/gnunet/message.clj
index 9d0b74d..3701260 100644
--- a/src/org/gnu/clojure/gnunet/message.clj
+++ b/src/org/gnu/clojure/gnunet/message.clj
@@ -1,25 +1,57 @@
-(ns org.gnu.clojure.gnunet.message)
+(ns org.gnu.clojure.gnunet.message
+ (:import java.math.BigInteger))
+
+(defn bit-count-to-bytes [x] (quot (+ 7 x) 8))
+
+(defn encode-int
+ "Convert an integer to a sequence of bytes in network order."
+ [x]
+ (let [big (bigint x)
+ len (max 1 (bit-count-to-bytes (.bitLength big)))
+ a (.toByteArray big)]
+ (drop (- (alength a) len) a)))
(defn encode-int16
"Convert a 16-bit integer to a sequence of bytes in network order."
[x]
- (list (byte (quot x 256)) (byte (rem x 256))))
+ (list (byte (bit-shift-right x 8)) (byte (bit-and x 0xFF))))
(defn encode-int32
"Convert a 32-bit integer to a sequence of bytes in network order."
[x]
- (concat (encode-int16 (quot x 65536)) (encode-int16 (rem x 65536))))
+ (concat (encode-int16 (bit-shift-right x 16))
+ (encode-int16 (bit-and x 0xFFFF))))
(defn encode-int64
"Convert a 64-bit integer to a sequence of bytes in network order."
[x]
- (concat (encode-int32 (quot x 4294967269)) (encode-int32 (rem x 4294967269))))
+ (concat (encode-int32 (bit-shift-right (bigint x) 32))
+ (encode-int32 (bit-and x 0xFFFFFFFF))))
+
+(defn decode-int
+ "Convert a sequence of bytes in network order to a 2's complement integer"
+ [a]
+ (BigInteger. (byte-array a)))
+
+(defn decode-uint
+ "Convert a sequence of bytes in network order to an unsigned integer"
+ [a]
+ (BigInteger. 1 (byte-array a)))
(defn encode-header
"Encode a gnunet message header."
- [size message-type]
+ [hdr]
(concat
- (encode-int16 size)
- (encode-int16 message-type)))
+ (encode-int16 (:size hdr))
+ (encode-int16 (:message-type hdr))))
+
+(def header-size (count (encode-header {:size 0 :message-type 0})))
-(def header-size (count (encode-header 0 0))) \ No newline at end of file
+(defn decode-header-and-split
+ "Split a seq into a gnunet message header and the rest"
+ [a]
+ (let [[encoded-size after-encoded-size] (split-at 2 a)
+ [encoded-type after-encoded-type] (split-at 2 after-encoded-size)]
+ [{:size (decode-uint encoded-size)
+ :message-type (decode-uint encoded-type)}
+ after-encoded-type]))