Skip to content

Instantly share code, notes, and snippets.

@xtang
Forked from eliasson/digest.clj
Created August 24, 2012 04:32
Show Gist options
  • Save xtang/3445441 to your computer and use it in GitHub Desktop.
Save xtang/3445441 to your computer and use it in GitHub Desktop.
Clojure md5 digest
(defn md5-sum
"Compute the hex MD5 sum of a string."
[#^String input]
(let [alg (doto (MessageDigest/getInstance "MD5")
(.update (.getBytes input)))
hash (.toString (new BigInteger 1 (.digest alg)) 16)
length (.length hash)]
(if (> 32 length)
;; 0x065 => 65, leading zero is dropped by BigInteger
(apply str (concat (repeat (- 32 length) \0) hash))
hash)))
@siteshen
Copy link

我抄过原版代码,有leading-zero 的 bug。。
早知道抄你这份了。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment