-
-
Save swannodette/311803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import [java.security MessageDigest]) | |
(def *sha1* "SHA-1") | |
(def *abc* (.getBytes "ABC")) | |
(def *abc-sha1* "3c1bdbb26f358bab27f267924aa2c9a3fcfdb8") | |
(defn start-message-digest [type] | |
(MessageDigest/getInstance type)) | |
(defn update-message-digest [#^MessageDigest md bytes] | |
(.update md (as-bytes bytes)) | |
md) | |
(defn as-hex [bytes] | |
(apply str (for [byte bytes] (format "%x" byte)))) | |
(defn hash-simple [type bytes-seq] | |
(as-hex (.digest (reduce update-message-digest (start-message-digest type) bytes-seq)))) | |
(assert (= *abc-sha1* (hash-simple *sha1* [*abc*]))) | |
(extend-class java.security.MessageDigest$Delegate | |
Editable | |
(transient-of [message-digest _] | |
message-digest) | |
Transient | |
(value-of [message-digest] | |
(.digest message-digest))) | |
(defn hash-cell [type bytes-seq] | |
(let [mdc (cell (start-sha1))] | |
(doseq [bytes bytes-seq] | |
(>> update-message-digest mdc bytes)) | |
(as-hex @mdc))) | |
(assert (= *abc-sha1* (hash-cell *sha1* [*abc*]))) | |
(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-cell-updateable [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))))) | |
(assert (= *abc-sha1* (hash-cell-updateable *sha1* [*abc*]))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment