Guest User

Untitled

a guest
May 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. import java.security.MessageDigest
  2.  
  3. fun String.toMD5() = hashWithAlgorithm("MD5")
  4. fun String.toSHA() = hashWithAlgorithm("SHA")
  5. fun String.toSHA256() = hashWithAlgorithm("SHA256")
  6.  
  7. fun String.hashWithAlgorithm(algorithm: String): String {
  8. val digest = MessageDigest.getInstance(algorithm)
  9. val bytes = digest.digest(this.toByteArray(Charsets.UTF_8))
  10. return bytes.fold("", { str, it -> str + "%02x".format(it) })
  11. }
Add Comment
Please, Sign In to add comment