Skip to content

Instantly share code, notes, and snippets.

@roberto-o-r
Created July 31, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberto-o-r/605bafb56c561e37f49a2b5fca8cda84 to your computer and use it in GitHub Desktop.
Save roberto-o-r/605bafb56c561e37f49a2b5fca8cda84 to your computer and use it in GitHub Desktop.
HashUtils.kt
package com.isscroberto.onebreath.utils
import java.security.MessageDigest
enum class Algorithm(val code: String) {
SHA1("SHA-1"), SHA256("SHA-256"), SHA512("SHA-512")
}
fun String.hash (algorithm: Algorithm): String {
val hexArray = "0123456789ABCDEF"
val bytes = MessageDigest.getInstance(algorithm.code).digest(this.toByteArray())
val hash = StringBuilder(bytes.size * 2)
bytes.forEach {
val i = it.toInt()
hash.append(hexArray[i shr 4 and 0x0f])
hash.append(hexArray[i and 0x0f])
}
return hash.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment