方法调用错误:在Jmeter中计算Hmac时方法encodeBase64()

Error in method invocation: Method encodeBase64() while calculating Hmac in Jmeter

我正在尝试在 Jmeter 中使用以下脚本生成 hmac 作为 API 请求的输入:

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import org.apache.commons.codec.binary.Base64;
import java.util.Base64;

String mobile="5000066639";
String name="Random ";
String email="random@gmail.com";
String dob="18041908";
String gender="M";
String pin="123456";
 
String CombinedKey = mobile+name+email+dob+gender+pin;

Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(CombinedKey.getBytes(), "HmacSHA256");
mac.init(secretKeySpec);
byte[] digest = mac.doFinal(CombinedKey.getBytes());
String sigB64 = digest.encodeBase64().toString();
//Tried with below these 2 values as well but similar result
//String sigB64 = Base64.encodeBase64String(digest);   
//String sigB64 = Base64.encodeToString(digest);

vars.put("hmac",sigB64);

但是出现以下错误

Response message:javax.script.ScriptException: Sourced file: inline evaluation of: ``import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.sec . . . '' : Typed variable declaration : Error in method invocation: Method encodeBase64() not found in class'[B' : at Line: 20 : in file: inline evaluation of: ``import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.sec . . . '' : digest .encodeBase64 ( ) 
 in inline evaluation of: ``import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.sec . . . '' at line number 20

只需切换到groovy语言

不要与下拉列表中的 java 选项混淆,尽管实际上它的名称是 Beanshell,但它具有不同的语法、某些限制和较差的性能,尤其是在涉及加密算法时.更多信息: