简体   繁体   中英

Jmeter how to generate hash sha512

I need to create test in jmeter for many requests and I need to create post request which will contain pass in header and it should be hash sha512 from user + someid + someid . I need to change user in each request so I just add in Header Manager user${__threadNum} , it looks fine but I need to generate hash from this in next post request. I was searching that it should be possible via beanshell script or jsr223 preprocessor, but I am not very familiar with scripting or java.

Using JMeter >= 4.0, just use function __digest :

${__digest(SHA-512,string to hash,,,)}

Since JMeter 4.0, the easier answer is:

For previous versions :

Add JSR223 Sampler with Java language, pass variables password and salt using vars.get("password") and using this code variable generatedPassword will hold the new hash generated

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

String passwordToHash = vars.get("password");
String   salt= vars.get("salt");
String generatedPassword = null;
    try {
     MessageDigest md = MessageDigest.getInstance("SHA-512");
     md.update(salt.getBytes("UTF-8"));
     byte[] bytes = md.digest(passwordToHash.getBytes("UTF-8"));
     StringBuilder sb = new StringBuilder();
     for(int i=0; i< bytes.length ;i++){
        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
     }
     generatedPassword = sb.toString();
     log.info(generatedPassword);
     vars.put("generatedPassword", generatedPassword);
    } 
   catch (NoSuchAlgorithmException e){
    e.printStackTrace();
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM