Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different values for MD 5 hash depending on technique

Tags:

clojure

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?

like image 815
johnbakers Avatar asked Nov 24 '25 13:11

johnbakers


1 Answers

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)))))
like image 56
Makoto Avatar answered Nov 28 '25 14:11

Makoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!