diff options
author | David Barksdale <amatus.amongus@gmail.com> | 2010-04-07 10:18:38 -0700 |
---|---|---|
committer | David Barksdale <amatus.amongus@gmail.com> | 2010-04-07 10:18:38 -0700 |
commit | a2785c6f20c5f362f780467301e753b70de55f41 (patch) | |
tree | badbb028736be7d76592e32380f727f264af2f00 | |
parent | 0137267336e36d3492e266c187d4580fdb10deb3 (diff) |
Worked some more on the identity lib.
-rw-r--r-- | src/gnunet.clj | 2 | ||||
-rw-r--r-- | src/org/gnu/clojure/gnunet/identity.clj | 40 |
2 files changed, 30 insertions, 12 deletions
diff --git a/src/gnunet.clj b/src/gnunet.clj deleted file mode 100644 index a86019d..0000000 --- a/src/gnunet.clj +++ /dev/null @@ -1,2 +0,0 @@ -(ns gnunet) - diff --git a/src/org/gnu/clojure/gnunet/identity.clj b/src/org/gnu/clojure/gnunet/identity.clj index 0f36057..dc47164 100644 --- a/src/org/gnu/clojure/gnunet/identity.clj +++ b/src/org/gnu/clojure/gnunet/identity.clj @@ -2,21 +2,41 @@ (defn bit-count-to-bytes [x] (quot (+ 7 x) 8)) -(defn bigint-to-bytes [x] +(defn encode-bigint + "Convert a bigInteger to a sequence of bytes in bigendian." + [x] (let [len (bit-count-to-bytes (.bitLength x)) a (.toByteArray x)] (drop (- (alength a) len) a))) -(defn short-to-bytes [x] - (list (quot x 256) (rem x 256))) +(defn encode-short + "Convert a short to a sequence of bytes in bigendian." + [x] + (list (byte (quot x 256)) (byte (rem x 256)))) -(defn rsa-public-key-binary-encoded [key] - (let [modulus (bigint-to-bytes (.getModulus key)) - exponent (bigint-to-bytes (.getPublicExponent key)) - len (+ (count modulus) (count exponent) 4)] +(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)) + modulus-len (count modulus) + exponent (encode-bigint (.getPublicExponent key)) + exponent-len (count exponent)] (concat - (short-to-bytes len) - (short-to-bytes (count modulus)) + (encode-short (+ modulus-len exponent-len 4)) + (encode-short modulus-len) modulus exponent - (short-to-bytes 0))))
\ No newline at end of file + (encode-short 0)))) + +(defn generate-rsa-keypair + "Generate a 2048 bit RSA keypair" + [] + (let [rsa (java.security.KeyPairGenerator/getInstance "RSA") + spec (java.security.spec.RSAKeyGenParameterSpec. 2048 (bigint 257))] + (.initialize rsa spec) + (.generateKeyPair rsa))) + +(defn sha-512 + [x] + (let [sha (java.security.MessageDigest/getInstance "SHA-512")] + (.digest sha (byte-array x))))
\ No newline at end of file |