Skip to content

Instantly share code, notes, and snippets.

@raecoo
Forked from navicore/sha256.scala.md
Created June 13, 2019 05:46
Show Gist options
  • Save raecoo/a88168ef859283751a20e7ae092248cb to your computer and use it in GitHub Desktop.
Save raecoo/a88168ef859283751a20e7ae092248cb to your computer and use it in GitHub Desktop.
sha256 single line function in scala

def sha256Hash(text: String) : String = java.security.MessageDigest.getInstance("SHA-256").digest(text.getBytes()).map(0xFF & ).map { "%02x".format() }.foldLeft(""){_ + _}

def sha256Hash(text: String) : String = String.format("%064x", new java.math.BigInteger(1, java.security.MessageDigest.getInstance("SHA-256").digest(text.getBytes("UTF-8"))))

verify via:

scala> sha256Hash("Rusty is a cowboy!")
res3: String = e0baa113fe0dd65bc88b4144c6b12c89f362a3617644315b48b36f668f7fa1d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment