0
votes

I'm calling a api serice that requires a http header with the hash value of the request body.

I'm trying to use a beanshell post processor in JMeter to automatically create the sha-256 hash of the request body. The hash value is right up until I introduce a line break in the request body (which is a pain as the JSON message spans several lines!)

I assume it's something to do with hidden characters however I can't work out what is going wrong! :(

When I compare the hash generated by JMeter to seperate Hash generator tools it is an exact match until there are line breaks, then JMeter is wrong.

Why is JMeter generating an incorrect hash when there are line breaks?

My code is:

[import org.apache.commons.httpclient.auth.DigestScheme; // necessary imports
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.services.FileServer;
import javax.xml.bind.DatatypeConverter;
import java.security.*;

String body = sampler.getArguments().getArgument(0).getValue();
String hash = DigestUtils.sha256Hex(body);
log.info(hash);
1
Dont' really get what you wanna know... - ivoruJavaBoy
Why do line breaks in my request body generate an incorrect sha 256 hash? - David
Line breaks in windows and linux are different. Can you provide more details ? - user7294900
run Jmeter 3.1 on windows - do you need any other info? - David
If you introduce line breaks you are altering the string and thus the hash. - Ray Oei

1 Answers

0
votes

What is your input data and what output do you expect?

Since JMeter 3.1 it is recommended to switch to JSR223 Test Elements and Groovy language so:

  1. Given the following request body:

    {
        "foo": "bar"
    }
    
  2. And the next Groovy code to generate a SHA-256 hex string:

    def sha256Hex = { input ->  
      java.security.MessageDigest.getInstance("SHA-256")   
        .digest(input.getBytes("UTF-8")).encodeHex().toString()  
    }
    log.info(sha256Hex(sampler.getArguments().getArgument(0).getValue()))
    

I'm getting dbc67f71c921b5b7649481a5123d94dfa919748d2962889681d96438033c548f value which is basically the same I can see using https://hash.online-convert.com/sha256-generator generator.