Last active
March 10, 2017 17:11
-
-
Save sw-samuraj/bdca98cb10b975fadf44df1fe46d67ab to your computer and use it in GitHub Desktop.
Map-reduce snippet for a blog post.
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 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