Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala one-liner to generate MD5 Hash from string

Tags:

hash

md5

scala

I'm new to Scala and I found this interesting one-liner to generate a hex-encoded MD5 hash from a string. I was hoping someone could help me understand this better.

private def getMd5(inputStr: String): String = {
  val md: MessageDigest = MessageDigest.getInstance("MD5")
  md.digest(inputStr.getBytes()).map(0xFF & _).map { "%02x".format(_) }.foldLeft("") {_ + _}
}

Thanks.

like image 993
AngryPanda Avatar asked Aug 09 '16 16:08

AngryPanda


People also ask

How do you generate MD5 hash of a string?

Call MessageDigest. getInstance("MD5") to get a MD5 instance of MessageDigest you can use. The compute the hash by doing one of: Feed the entire input as a byte[] and calculate the hash in one operation with md.

What is the MD5 hash of a string?

What is an MD5 hash? An MD5 hash is created by taking a string of an any length and encoding it into a 128-bit fingerprint. Encoding the same string using the MD5 algorithm will always result in the same 128-bit hash output.

What is the length of MD5 hash?

The hash size for the MD5 algorithm is 128 bits. The ComputeHash methods of the MD5 class return the hash as an array of 16 bytes. Note that some MD5 implementations produce a 32-character, hexadecimal-formatted hash.

How does MD5 hash work?

How does MD5 work? MD5 runs entire files through a mathematical hashing algorithm to generate a signature that can be matched with an original file. That way, a received file can be authenticated as matching the original file that was sent, ensuring that the right files get where they need to go.


1 Answers

It's just a analogue to this java code but without StringBuilder (it's up to you)

    MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
    String password = "secret";
    messageDigest.update(password.getBytes());
    byte[] bytes = messageDigest.digest();
    StringBuilder stringBuilder = new StringBuilder();
    for (byte aByte : bytes) {
        stringBuilder.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
    }
    System.out.println(stringBuilder.toString());

Let's consider second line:

md.digest(inputStr.getBytes()).map(0xFF & _).map { "%02x".format(_) }.foldLeft("") {_ + _}
  1. md.digest(inputStr.getBytes()) ---- take bytes from String
  2. md.digest(inputStr.getBytes()).map(0xFF & _) --- bitwise & with every item of array (map return a new array)
  3. md.digest(inputStr.getBytes()).map(0xFF & ).map { "%02x".format() } map with formatting each item.
  4. md.digest(inputStr.getBytes()).map(0xFF & ).map { "%02x".format() }.foldLeft("") {_ + _} it's typical fold, in our case starting from left and init value "", (if it will be easier for you, it's "far" analogue of StringBuilder behaving in above example). I recommend you to read about fold, reduce and etc. actions in scala. e.g. https://coderwall.com/p/4l73-a/scala-fold-foldleft-and-foldright
like image 110
pacman Avatar answered Sep 17 '22 05:09

pacman