1. Miki Tebeka
  2. clj-digest


Miki Tebeka  committed 3a1f949

Simpler byte-seq

  • Participants
  • Parent commits add1b1a
  • Branches default
  • Tags 1.2.0

Comments (0)

Files changed (3)

File README.rst

View file
  • Ignore whitespace
 * byte array
 * java.io.File
 * java.io.InputStream
-* Sequence of [byte-array size]
+* Sequence of byte array
-Add `[digest "1.1.0"]` to your `project.clj`
+Add `[digest "1.2.0"]` to your `project.clj`

File project.clj

View file
  • Ignore whitespace
-(defproject digest "1.1.1"
+(defproject digest "1.2.0"
   :description "Digest algorithms (MD5, SHA ...) for Clojure"
   :author "Miki Tebeka <miki.tebeka@gmail.com>"
   :url "https://bitbucket.org/tebeka/clj-digest/src"

File src/digest.clj

View file
  • Ignore whitespace
   #^{ :author "Miki Tebeka <miki.tebeka@gmail.com>"
       :doc "Message digest algorithms for Clojure"}
   (:use [clojure.string :only (split lower-case)])
-  (:import (java.security MessageDigest Security)
+  (:import java.util.Arrays
+           (java.security MessageDigest Security)
            (java.io FileInputStream File InputStream)))
 ; Default buffer size for reading
 (def *buffer-size* 1024)
+; Why on earth is java.io.byte-array-type private?
 (def ByteArray (type (make-array Byte/TYPE 0)))
 (defn- read-some 
   (let [buffer (make-array Byte/TYPE *buffer-size*)
         size (.read reader buffer)]
-    (when (> size 0) [buffer size])))
+    (when (> size 0)
+      (if (= size *buffer-size*) buffer (Arrays/copyOf buffer size)))))
 (defn- byte-seq
   "Return a sequence of [data size] from reader."
   (digest algorithm (.getBytes message)))
 (defmethod digest ByteArray [algorithm message]
-  (digest algorithm [[message (count message)]]))
+  (digest algorithm [message]))
 (defmethod digest File [algorithm file]
   (digest algorithm (FileInputStream. file)))
 (defmethod digest :default [algorithm chunks]
   (let [algo (MessageDigest/getInstance algorithm)]
     (.reset algo)
-    (dorun (map (fn [[message size]] (.update algo message 0 size)) chunks))
+    (dorun (map #(.update algo %) chunks))
     (.toString (BigInteger. 1 (.digest algo)) 16)))
 (defn algorithms []