[JAVA] HMAC in Kotlin

kotlin_800x320.png

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

HMAC in Kotlin
Big Decimal in Kotlin
[Java] Get KClass in Java [Kotlin]
Apache POI Excel in Kotlin
Implementation of HashMap in kotlin
Screen transition using Intent in Kotlin
Implement Material Design View in Kotlin
Error handling in gRPC (Kotlin version)
Imitate Java's functional interface in Kotlin
About Kotlin
Why Synchronized suspend doesn't work in Kotlin
Kotlin Bytecode
Kotlin infix
"Hello, World!" With Kotlin + CLI in 5 minutes
What Java programmers find useful in Kotlin
Introduction to Ratpack (Extra Edition) --Ratpack written in Kotlin
I want to be eventually even in kotlin
How to write Java String # getBytes in Kotlin?
[Java] Get KFunction from Method / Constructor in Java [Kotlin]