Commits

Miki Tebeka  committed 54c19f1

Correct signature length

  • Participants
  • Parent commits b4e8ec7

Comments (0)

Files changed (3)

File src/digest.clj

   [reader]
   (take-while (complement nil?) (repeatedly (partial read-some reader))))
 
+(defn- signature
+  "Get signature (string) of digest."
+  [algorithm]
+  (let [size (* 2 (.getDigestLength algorithm))
+        sig (.toString (BigInteger. 1 (.digest algorithm)) 16)
+        padding (apply str (repeat (- size (count sig)) "0"))]
+    (str padding sig)))
+
 (defmulti digest
   "Returns digest for input with given algorithm."
   (fn [algorithm message] (class message)))
   (let [algo (MessageDigest/getInstance algorithm)]
     (.reset algo)
     (dorun (map #(.update algo %) chunks))
-    (.toString (BigInteger. 1 (.digest algo)) 16)))
+    (signature algo)))
 
 (defn algorithms []
   "List support digest algorithms."

File test/digest_test.clj

 ; Just making sure that we don't explode on nil
 (deftest nil-test
   (md5 nil))
+
+(deftest length-test
+  (is (= (sha (File. "test/length.txt")) 
+        "007b65165b253172d054189e8e3175f3bcb9e28e")))

File test/length.txt

+blah
+bar baz