-
-
Save xtang/3445441 to your computer and use it in GitHub Desktop.
Clojure md5 digest
This file contains 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
(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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我抄过原版代码,有leading-zero 的 bug。。
早知道抄你这份了。。