Skip to content

Instantly share code, notes, and snippets.

@jinyangustc
Forked from jizhang/md5.clj
Created May 10, 2018 12:02
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 jinyangustc/05e97ae35f20459bd99f15ea0fc345ae to your computer and use it in GitHub Desktop.
Save jinyangustc/05e97ae35f20459bd99f15ea0fc345ae to your computer and use it in GitHub Desktop.
Clojure - Calculate MD5 hash of a given string.
(import 'java.security.MessageDigest
'java.math.BigInteger)
(defn md5 [s]
(let [algorithm (MessageDigest/getInstance "MD5")
size (* 2 (.getDigestLength algorithm))
raw (.digest algorithm (.getBytes s))
sig (.toString (BigInteger. 1 raw) 16)
padding (apply str (repeat (- size (count sig)) "0"))]
(str padding sig)))
; for full digest support, use clj-digest: https://github.com/tebeka/clj-digest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment