diff options
author | David Barksdale <amatus.amongus@gmail.com> | 2011-10-04 22:13:31 -0500 |
---|---|---|
committer | David Barksdale <amatus.amongus@gmail.com> | 2011-10-04 22:13:31 -0500 |
commit | 92c382cc7dd0ba664b6c1b4baa004f2564921b6f (patch) | |
tree | 8ffc1d63f02e7931ecbaac6c9d01d33478b1a77a | |
parent | d65e2514827a289efd90c1b49c2a330f63439ac8 (diff) |
Fixed crypto tests.
Turns out there was more java.lang.BigInteger/clojure.lang.BigInt confusion
left over from the port to clojure 1.3. I'm not sure how this was missed.
I also found that my current java VM doesn't like
(SecretKeyFactory/getInstance "AES"), however this was completely unnecessary
because SecretKeySpec impliments Key and works everywhere I have an aes-key.
I also found that Cipher.init will fail on a 256-bit AES key if it doesn't
have "unlimited strength".
-rw-r--r-- | src/main/clojure/org/gnu/clojure/gnunet/crypto.clj | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/main/clojure/org/gnu/clojure/gnunet/crypto.clj b/src/main/clojure/org/gnu/clojure/gnunet/crypto.clj index c5a9108..85b077d 100644 --- a/src/main/clojure/org/gnu/clojure/gnunet/crypto.clj +++ b/src/main/clojure/org/gnu/clojure/gnunet/crypto.clj @@ -129,8 +129,7 @@ (defn make-aes-key [byte-seq] - (.generateSecret (SecretKeyFactory/getInstance "AES") - (SecretKeySpec. (byte-array byte-seq) "AES"))) + (SecretKeySpec. (byte-array byte-seq) "AES")) (defn crc32 [byte-seq] @@ -326,7 +325,10 @@ hashes (iterate sha-512 seed) number (BigInteger. 1 (byte-array (apply concat (take cnt hashes)))) len (.bitLength number) - number (reduce bit-clear number (range (dec len) (dec bit-length) -1))] + number (reduce + #(.clearBit %1 %2) + number + (range (dec len) (dec bit-length) -1))] [number (nth hashes cnt)])) (is (= (let [[n seed] (random-int 1024 (sha-512 []))] [n (vec seed)]) @@ -339,7 +341,8 @@ (defn fermat-compositeness-test "Perform Fermat's Compositeness Test on the given BigInteger." [number] - (not (== 1 (.modPow (biginteger 2) (dec number) number)))) + (not + (== 1 (.modPow (biginteger 2) (.subtract number BigInteger/ONE) number)))) (defn miller-rabin-compositeness-test "Perform the Miller-Rabin Compositeness Test on the given BigInteger with the @@ -347,9 +350,9 @@ round." [n steps seed] (let [bit-length (.bitLength n) - nminus1 (dec n) + nminus1 (.subtract n BigInteger/ONE) k (.getLowestSetBit nminus1) - q (bit-shift-right nminus1 k)] + q (.shiftRight nminus1 k)] (loop [step 0 seed seed] (if (>= step steps) [false seed] @@ -376,9 +379,9 @@ {:pre [(>= bit-length 32)]} (loop [seed seed] (let [[prime seed] (random-int bit-length seed) - prime (bit-set prime (dec bit-length)) - prime (bit-set prime (- bit-length 2)) - prime (bit-set prime 0) + prime (.setBit prime (dec bit-length)) + prime (.setBit prime (- bit-length 2)) + prime (.setBit prime 0) mods (map (partial rem prime) small-primes) [prime seed] (loop [step 0 seed seed] (if (> step 20000) @@ -386,7 +389,7 @@ (if (not-any? zero? (map #(rem (+ step %1) %2) mods small-primes)) - (let [prime (+ prime step)] + (let [prime (.add prime (biginteger step))] (if (fermat-compositeness-test prime) (recur (inc step) seed) (let [[result seed] @@ -421,14 +424,14 @@ [q seed] (generate-prime (quot bit-length 2) seed) [p q] (sort [p q]) - n (* p q)] + n (.multiply p q)] [n p q seed]) [(biginteger 0) 0 0 seed]))) - t1 (dec p) - t2 (dec q) - phi (* t1 t2) + t1 (.subtract p BigInteger/ONE) + t2 (.subtract q BigInteger/ONE) + phi (.multiply t1 t2) g (.gcd t1 t2) - f (quot phi g) + f (.divide phi g) e (biginteger (first (filter #(== 1 (.gcd phi (biginteger %))) (iterate (partial + 2) 257))))] (let [private-key (try |