Commits

Miki Tebeka committed 3a1f949

Simpler byte-seq

Comments (0)

Files changed (3)

 * byte array
 * java.io.File
 * java.io.InputStream
-* Sequence of [byte-array size]
+* Sequence of byte array
 
 Usage
 =====
 
 Installation
 ============
-Add `[digest "1.1.0"]` to your `project.clj`
+Add `[digest "1.2.0"]` to your `project.clj`
 
 License
 =======
-(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"
   #^{ :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 
   [reader]
   (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 []
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.