1

I have the following Scala function:

import java.security.MessageDigest
def md5(s: String) = MessageDigest.getInstance("MD5").digest(s.getBytes).toString()

When I encrypt the same string, I always get different results, what could be the problem? There are no new lines or spaces at the end of the input strings and all of them have the same length.

sstan
  • 35,425
  • 6
  • 48
  • 66
ps0604
  • 1,227
  • 23
  • 133
  • 330
  • 2
    have a look at the return value type of `digest` and read http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 and then the comments on http://stackoverflow.com/questions/5992778/computing-the-md5-hash-of-a-string-in-scala – zapl Jun 03 '16 at 12:16
  • 1
    While it seems "leet" to put everything in one line debugging really suffers, you can't examine the intermediate values, if you did you would probably find the error. – zaph Jun 03 '16 at 12:37

1 Answers1

0

You want:

def md5(s: String) = {
    MessageDigest.getInstance("MD5").digest(s.getBytes).map("%02x".format(_)).mkString
}
airudah
  • 1,169
  • 12
  • 19