Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash and encode variable before sending along with request

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.

like image 877
Mike LP Avatar asked Nov 21 '25 19:11

Mike LP


2 Answers

  1. 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.

    • Use JSR223 Sampler / PostProcessor / PreProcessor with the following [groovy] code:
    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.

  2. Similar groovy code used with __groovy function.

  3. jmeter-plugins set contains ${__MD5(...)}, ${__base64Encode(...)}, ${__uppercase(...)} functions but that's not enough for your case (no SHA256 digest).

  4. You can also look onto OS Process Sampler to implement the same using your OS (nice if linux) capabilities.

like image 85
Aliaksandr Belik Avatar answered Nov 24 '25 11:11

Aliaksandr Belik


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

like image 39
user7294900 Avatar answered Nov 24 '25 11:11

user7294900



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!