-
-
Save BurakDizlek/87eddbfc21568060b6643e580116f0cd to your computer and use it in GitHub Desktop.
I found Sam Clarke's kit. SHA 512 etc.
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.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