Last active
December 5, 2021 14:38
-
-
Save tychobrailleur/e784941a7636dce015728cdcc8e4670c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns chan_hash) | |
(import '[org.apache.commons.codec.binary Hex]) | |
(import '[java.nio ByteBuffer]) | |
(require '[clojure.core.async :as async :refer [chan go >! <! pipeline go-loop <!!]]) | |
(def sha256-digest (java.security.MessageDigest/getInstance "SHA-256")) | |
(defn hexify [bb] | |
(Hex/encodeHexString bb)) | |
(defn sha256 [message] | |
(.digest sha256-digest message)) | |
(defn int->bytes [i] | |
(let [buffer (ByteBuffer/allocate 4)] ;; ints are 4-byte long | |
(.putInt buffer i) | |
(.array buffer))) | |
(def in-chan (chan)) | |
(def out-chan (chan)) | |
(def transform (comp hexify sha256)) | |
(pipeline 4 out-chan (map transform) in-chan) | |
(doseq [i (range 1000)] | |
(go (>! in-chan (int->bytes i)))) | |
(go-loop [] | |
(println (<! out-chan)) | |
(recur)) | |
(Thread/sleep 5000) |
This file contains hidden or 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
{:deps | |
{org.clojure/core.async {:mvn/version "1.3.610"} | |
commons-codec/commons-codec {:mvn/version "1.15"}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment