HMAC-SHA1 based on the payload with Signature calculator

Hi,

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” → Instant.now().toEpochMilli().toString,
“firstname” → “John”,
“lastname” → ("Gatling " + Instant.now().toEpochMilli().toString),
“userRole” → “student”,
“timestamp” → Instant.now().toEpochMilli()
))

val scn = scenario(“Login test”)
.repeat(1) {
feed(loginFeeder)
.exec(http(“Login init”)
.post("/o/access")
.header(“Content-type”, “application/json”)
.body(ElFileBody(“login.json”)).asJson
.sign(new OwnSignatureCalculator)
.check(status.is(200)))
}

setUp(
scn.inject(
atOnceUsers(1)
).protocols(httpConf))

}

`

`

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”)
mac.init(secret)
val hashString: Array[Byte] = mac.doFinal(combined.getBytes)
val authToken = new String(hashString.map(_.toChar))

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.