【问题标题】:Error in method invocation: Method encodeBase64() while calculating Hmac in Jmeter方法调用错误:在 Jmeter 中计算 Hmac 时方法 encodeBase64()
【发布时间】:2021-03-29 01:49:03
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: jmeter base64 sha256 hmac


    【解决方案1】:

    只需切换到groovy 语言

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

    【讨论】:

    • 感谢 Dmitri,这真的救了我的命!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2021-10-09
    相关资源
    最近更新 更多