Skip to content

Instantly share code, notes, and snippets.

@sw-samuraj
Last active March 10, 2017 17:11
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 sw-samuraj/bdca98cb10b975fadf44df1fe46d67ab to your computer and use it in GitHub Desktop.
Save sw-samuraj/bdca98cb10b975fadf44df1fe46d67ab to your computer and use it in GitHub Desktop.
Map-reduce snippet for a blog post.
(ns blog-map-reduce.core
(:import java.security.MessageDigest))
(defn digest [string]
"Returns a SHA-1 digest of the given string."
(.digest (MessageDigest/getInstance "SHA-1")
(.getBytes string)))
(defn hex [bt]
"Returns a hexadecimal value of the given byte."
(if (< (bit-and 0xff bt) 0x10)
(str 0 (Integer/toHexString (bit-and 0xff bt)))
(Integer/toHexString (bit-and 0xff bt))))
(defn sha1 [string]
"Returns a SHA-1 hash of the given string."
(reduce str (map hex (digest string))))
(defn sha1-map [strings]
"Returns a map of SHA-1 hashes as a keyword keys
and given string as a value."
(zipmap (map keyword (map sha1 strings)) strings))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment