Skip to content

Instantly share code, notes, and snippets.

@kobeumut
Created October 25, 2017 14:22
Show Gist options
  • Save kobeumut/8ef93ea6cea98038db7cb606ea0f37d6 to your computer and use it in GitHub Desktop.
Save kobeumut/8ef93ea6cea98038db7cb606ea0f37d6 to your computer and use it in GitHub Desktop.
I found Sam Clarke's kit. SHA 512 etc.
package com.samclarke.android.util
import java.security.MessageDigest
/**
* Hashing Utils
* @author Sam Clarke <www.samclarke.com>
* @license MIT
*/
object HashUtils {
fun sha512(input: String) = hashString("SHA-512", input)
fun sha256(input: String) = hashString("SHA-256", input)
fun sha1(input: String) = hashString("SHA-1", input)
/**
* Supported algorithms on Android:
*
* Algorithm Supported API Levels
* MD5 1+
* SHA-1 1+
* SHA-224 1-8,22+
* SHA-256 1+
* SHA-384 1+
* SHA-512 1+
*/
private fun hashString(type: String, input: String): String {
val HEX_CHARS = "0123456789ABCDEF"
val bytes = MessageDigest
.getInstance(type)
.digest(input.toByteArray())
val result = StringBuilder(bytes.size * 2)
bytes.forEach {
val i = it.toInt()
result.append(HEX_CHARS[i shr 4 and 0x0f])
result.append(HEX_CHARS[i and 0x0f])
}
return result.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment