Hash and encode the variable before sending along with the request

I am trying to configure JMeter to connect to a service that requires some hashing for some variables before and between requests.

I have created a user-defined variable that contains the authentication information in text format.
Before making the first HTTP request, I need to hash the password with SHA256, encode it with base64 and then convert it to uppercase.

I receive auth_token

JSON as part of the response body. Then I need to make the same chain SHA256 -> base64 -> uppercase

as auth_token and from now on it will be used in the request header.

+3


source to share


2 answers


  • If you are not implementing resource-critical scenarios (load testing), you can use, for example, the JSR223 Sampler / JSR223 PostProcessor / JSR223 PreProcessor with a little code.

    eg.

    • Use JSR223 Sampler / PostProcessor / PreProcessor with the following code [groovy]:
    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 reuse this sampler for both the hash and the auth_token password - through the Parameters field in the JSR223 Sampler configuration: use, for example, ${password}

      in the first case, and auth_token

      in the second.

    • The hash value can be called a variable ${encodedValue}

      .

  • Similar groovy code used with __groovy .

  • jmeter-plugins set contains ${__MD5(...)}

    , ${__base64Encode(...)}

    , ${__uppercase(...)}

    functions The , but this is not enough for your application (no SHA256 digest).

  • You can also look at the OS Process Sampler to implement the same using your OS capabilities (with a good one if linux).



+5


source


There is a new feature __digest

, currently in nightly build

In your case, use the following to store in encodedValue variable as a result of using password variable:

${__digest(SHA-256,${password},,,encodedValue)}

      

You can download Custom JMeter Functions to call base 64 encoding function:



${__base64Encode(encodedValue, base64Value)}

      

And then call the uppercase function:

${__uppercase(base64Value, finalValue)}

      

$ {finalValue} will contain the final value of these operations

+1


source







All Articles