I'm trying to setup JMeter for connecting to a service which requires some hashing be done on some variables before I start and in-between requests.
I've created a User Defined Variable that has the authentication info in plain text.
Before the first HTTP Request is called I need to hash the password using SHA256, encode it using base64 and then convert to uppercase.
I will receive an auth_token within a JSON formated response body. Then I need to do the same SHA256 -> base64 -> uppercase chain to that auth_token and from then on it will be used in the request header.
If you don't implement resource-critical scenarios (load-testing) you can possibly use e.g. JSR223 Sampler / JSR223 PostProcessor / JSR223 PreProcessor with a bit of code.
E.g.
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Base64;
import org.testng.annotations.Test;
String [] params = Parameters.split(",");
String text = params[0];
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(text.getBytes("UTF-8"));
byte[] digest = md.digest();
byte[] encoded = Base64.encodeBase64(digest);
String encText = (new String(encoded)).toUpperCase();
vars.put("encodedValue",encText);
You can re-use this sampler both to hash both password and auth_token - via "Parameters" field in JSR223 Sampler configuration: use e.g. ${password} variable in the first case, and auth_token - in the second.
Hashed value you can refer as ${encodedValue} variable.
Similar groovy code used with __groovy function.
jmeter-plugins set contains ${__MD5(...)}, ${__base64Encode(...)}, ${__uppercase(...)} functions but that's not enough for your case (no SHA256 digest).
You can also look onto OS Process Sampler to implement the same using your OS (nice if linux) capabilities.
There's a new function __digest, currently in nightly builds
In your case to save in encodedValue variable the result of password variable use the following:
${__digest(SHA-256,${password},,,encodedValue)}
You can download Custom JMeter Functions plugin to call base 64 encoding function:
${__base64Encode(encodedValue, base64Value)}
And then call uppercase function:
${__uppercase(base64Value, finalValue)}
${finalValue} will hold the final value of this operations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With