Search code examples
clojure

Different values for MD 5 hash depending on technique


I'm trying to find a good way to hash a string. This method is working fine, but the results are not consistent with this website:

(defn hash-string
  "Use java interop to flexibly hash strings"
  [string algo base]
  (let [hashed
        (doto (java.security.MessageDigest/getInstance algo)
          (.reset)
          (.update (.getBytes string)))]
    (.toString (new java.math.BigInteger 1 (.digest hashed)) base))
  )

(defn hash-md5
  "Generate a md5 checksum for the given string"
  [string]
  (hash-string string "MD5" 16)
) 

When I use this, I do indeed get hashes. The problem is I'm trying a programming exercise at advent of code and it has its own examples of string hashes which offer a 3rd result different from the above 2!

How can one do an md5 in the "standard" way that is always expected?


Solution

  • Your MD5 operations are correct; you're just not displaying them properly.

    Since an MD5 is 32 hexadecimal characters long, you need to format the string to pad it out correctly.

    In other words, simply change this expression:

    (.toString (new java.math.BigInteger 1 (.digest hashed)) base))
    

    to one that uses format:

    (format "%032x" (new java.math.BigInteger 1 (.digest hashed)))))