HMAC-SHA1 based on the payload with Signature calculator


I need to create HMAC-SHA1 (+Base64 encoding) on JSON payload + secret key.

What I have is this:


package simulations

import baseConfig.BaseSimulation
import utils.OwnSignatureCalculator
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import java.time.Instant

class LoginTest extends BaseSimulation {

val loginFeeder = Iterator.continually(Map(
“materialId” → 1,
“userId” →,
“firstname” → “John”,
“lastname” → ("Gatling " +,
“userRole” → “student”,
“timestamp” →

val scn = scenario(“Login test”)
.repeat(1) {
.exec(http(“Login init”)
.header(“Content-type”, “application/json”)
.sign(new OwnSignatureCalculator)





package utils

import io.gatling.http.Predef._
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.util.Base64
import java.nio.charset.StandardCharsets

class OwnSignatureCalculator extends SignatureCalculator{

override def sign(request: Request): Unit = {

val combined = request.getBody.getContent.toString + “secret-abc123”
println(“DEBUG: " + combined)
val secret = new SecretKeySpec(”“secret-abc123”.getBytes, “HmacSHA1”)
val mac = Mac.getInstance(“HmacSHA1”)
val hashString: Array[Byte] = mac.doFinal(combined.getBytes)
val authToken = new String(

request.getHeaders.add(“Authorization”, Base64.getEncoder.encodeToString(authToken.getBytes(StandardCharsets.UTF_8)))



Now the first issue is that the line "println(“DEBUG: " + combined)” does show that the body is no the JSON I expected it to be - it seems to be some sort of byte array. How can I get the body properly before sending the request?
And if you see some other issues, please free to comment.