com.leanplum.utils.HashUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leanplum-core Show documentation
Show all versions of leanplum-core Show documentation
The Leanplum SDK messaging platform
The newest version!
package com.leanplum.utils
import java.security.MessageDigest
object HashUtil {
private fun ByteArray.toHex(limit: Int): String =
joinToString(separator = "", limit = limit, truncated = "") { eachByte ->
"%02x".format(eachByte)
}
/**
* Returns hexadecimal representation of sha256 on the input string.
*/
fun sha256(text: String, limit: Int = 32): String {
return MessageDigest
.getInstance("SHA-256")
.digest(text.toByteArray(Charsets.UTF_8))
.toHex(limit)
}
/**
* Get first 10 symbols from hexadecimal representation of sha256.
*/
fun sha256_40(text: String) = sha256(text, 5)
/**
* Get first 50 symbols from hexadecimal representation of sha256.
*/
fun sha256_200(text: String) = sha256(text, 25)
}