All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.security.HmacSha256.kt Maven / Gradle / Ivy

package org.http4k.security

import org.http4k.util.Hex
import java.security.MessageDigest
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

object HmacSha256 {

    fun hash(payload: String): String = hash(payload.toByteArray())

    fun hash(payload: ByteArray): String = Hex.hex(MessageDigest.getInstance("SHA-256").digest(payload))

    fun hmacSHA256(key: ByteArray, data: String): ByteArray = Mac.getInstance("HmacSHA256").run {
        init(SecretKeySpec(key, "HmacSHA256"))
        doFinal(data.toByteArray(charset("UTF8")))
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy