3

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.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Mike LP
  • 671
  • 8
  • 13

2 Answers2

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

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
  • 1
    I was able to streamline it a bit by using vars.put("passwordHash", Base64.encodeBase64String(digest).toUpperCase() ); – Mike LP Feb 06 '13 at 19:20
  • Only cause I found this now and I needed similar functions. JMeter comes with commons-codec.jar in it's lib path. So you can use that also in the BeanShell :) – user432024 Nov 28 '13 at 14:42
1

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

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • When I run this in jmeter it throw an error trying to set a property on the script file named (the hash value): ${__digest(SHA-256,${password},,,encodedValue)} – ssmith Oct 23 '19 at 19:11
  • javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: baa5f05689d39412e59913c3b514c459d8fa211d696b19322f0ac57334eaf19 for class: Script64 – ssmith Oct 23 '19 at 19:11