# HMAC-SHA1 based on the payload with Signature calculator

**URL:** <https://community.gatling.io/t/hmac-sha1-based-on-the-payload-with-signature-calculator/4865>\
**Category:** Gatling (Open-Source)\
**Created:** [March 12, 2019, 1:47pm UTC](https://community.gatling.io/t/hmac-sha1-based-on-the-payload-with-signature-calculator/4865 "2019-03-12T13:47:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaakko\_Salminen](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jaakko\_Salminen](https://community.gatling.io/u/Jaakko_Salminen)\
**Post date:** [March 12, 2019, 1:47pm UTC](https://community.gatling.io/t/hmac-sha1-based-on-the-payload-with-signature-calculator/4865/1 "2019-03-12T13:47:17Z")

</div>

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.
