Created
November 24, 2017 15:58
-
-
Save lovubuntu/164b6b9021f5ba54cefc67f60f7a1a25 to your computer and use it in GitHub Desktop.
function to generate Sha-256 in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Hasher { | |
fun hash(): String { | |
val bytes = this.toString().toByteArray() | |
val md = MessageDigest.getInstance("SHA-256") | |
val digest = md.digest(bytes) | |
return digest.fold("", { str, it -> str + "%02x".format(it) }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can replace the
fold
withtoHexString()
(Mind you that this is, as of the time of writing this, marked asExperimentalStdlibApi
):