Skip to content

Instantly share code, notes, and snippets.

@tychobrailleur
Last active December 5, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tychobrailleur/e784941a7636dce015728cdcc8e4670c to your computer and use it in GitHub Desktop.
Save tychobrailleur/e784941a7636dce015728cdcc8e4670c to your computer and use it in GitHub Desktop.
(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)
{: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