I was reading rhickey's ideas about cells, and it seemed like it
matched very well with using java's MessageDigest to do sha1 or md5
hashing.  I coded it up, and I liked the result and thought others
might be interested.

http://gist.github.com/311790


The key parts ended up looking like this:

(extend-class java.security.MessageDigest$Delegate
  Editable
  (transient-of [message-digest _]
    message-digest)

  Transient
  (value-of [message-digest]
    (.digest message-digest)))

(defprotocol Updateable
  (update [updatable val]))

(extend-class java.security.MessageDigest$Delegate
  Updateable
  (update [md bytes]
    (.update md bytes)
    md))

(defn update-with-seq [updateable vals]
  (doseq [val vals]
    (update updateable val))
  updateable)

(defn update-cell [c s]
  (>> update-with-seq c s)
  c)

(defn hash [type bytes-seq]
  (let [mdc (cell (start-message-digest type))]
    ; do stuff in other thread at the same time and always see safe
snapshots!
    (as-hex (deref (update-cell mdc bytes-seq)))))

(hash "SHA-1" [(.getBytes "ABC")]) ;should be
"3c1bdbb26f358bab27f267924aa2c9a3fcfdb8"

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to