Created
July 31, 2019 18:54
-
-
Save roberto-o-r/605bafb56c561e37f49a2b5fca8cda84 to your computer and use it in GitHub Desktop.
HashUtils.kt
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
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