Note how to encrypt with HMAC in Kotlin. Other various encryptions in Kotlin are [phxql / kotin-crypto-example](https://github.com/phxql/kotlin-crypto-example/blob/ There is a sample in master / src / main / kotlin / de / mkammerer / Crypto.kt).
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import kotlin.experimental.and
/**
* Hmac by Kotlin
*/
fun main(args: Array<String>) {
// Change algorithm as you like. ex. "HmacSHA1", "HmacMD5", etc
val algorithm = "HmacSHA256"
val key = "Secret Key"
val text = "Encryption Target"
// Encryption
val keySpec = SecretKeySpec(key.toByteArray(), algorithm)
val mac = Mac.getInstance(algorithm)
mac.init(keySpec)
val sign = mac.doFinal(text.toByteArray())
.joinToString("") { String.format("%02x", it and 255.toByte()) }
// 6bad2e332a94882be27d946b5fab39acd6be6fd64c6a52a27d77422daf36a6fd
println(sign)
}
Recommended Posts