Skip to content

Instantly share code, notes, and snippets.

@robinraju
Last active February 22, 2022 07:59
Show Gist options
  • Save robinraju/100854f6957ca208eee9c886dfd5d1c8 to your computer and use it in GitHub Desktop.
Save robinraju/100854f6957ca208eee9c886dfd5d1c8 to your computer and use it in GitHub Desktop.
MD5 hash using Scala
import java.nio.charset.Charset
import java.security.MessageDigest
def md5(value: String): String = {
val Utf8 = Charset.forName("UTF-8")
MessageDigest
.getInstance("MD5")
.digest(value.getBytes(Utf8))
.foldLeft(new StringBuilder) { case (sb, byte) =>
sb.append(byte.formatted("%02x"))
}
.toString()
}
// SclaJS implementation
def md5(value: String): String =
scala.scalajs.js.Dynamic.global.CryptoJS.MD5(value).toString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment