Skip to content

Instantly share code, notes, and snippets.

@Carlos-Augusto
Created February 17, 2021 06:22
Show Gist options
  • Save Carlos-Augusto/22c21fedfe1389197d55288d2e2be93d to your computer and use it in GitHub Desktop.
Save Carlos-Augusto/22c21fedfe1389197d55288d2e2be93d to your computer and use it in GitHub Desktop.
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.util.Strings
import java.security.Security
import java.util.Base64
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
Security.addProvider(new BouncyCastleProvider())
val macKey = new SecretKeySpec(
Base64.getDecoder.decode("QwKFZpTiEcXnLdbbGEdzcO57/yoIVGOpixXfeKGLDdg="), // Secret
"HmacSHA256"
)
val data = Strings.toByteArray("Hello World")
val mac = Mac.getInstance("HmacSHA256", "BC")
mac.init(macKey)
mac.update(data)
Base64.getEncoder.encodeToString(mac.doFinal())
//IFVv0O2lWoHNjEEsGcL9YOiaPNWkHIVkpCiiTKsp1ac=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment