HMAC-SHA256签名

文章介绍了如何在HTTP请求中添加Authorization头部,使用HMAC-SHA256算法生成并验证签名,包括获取时间戳、构造签名字符串、计算签名并进行比对的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.需求

请求加上Authorization头部,按指定格式添加校验内容。Authorization包含以下几个方面信息:
校验方式:固定为HMAC-SHA256
timestamp:当前时间的13位毫秒时间戳
signature:请求签名,按指定方式生成

二.实现

final String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
final String algorithm = "HmacSHA256";
private boolean authorizationValidation(HttpServletRequest request) {
    CachedBodyHttpServletRequest cachedRequest = (CachedBodyHttpServletRequest) request;
    String authorizationHeader = request.getHeader("Authorization");
    try {
      // 解析 Authorization 值
      String[] authValues = authorizationHeader.split(",");
      String timestamp = authValues[1];
      String body = cachedRequest.getBody();
      if (body == null) {
        body = "";
      }
      String receivedSignature = authValues[2];
      String path = request.getRequestURI();
      String query = request.getQueryString();
      if (StringUtils.isNotBlank(query)) {
        path += "?" + query;
      }
      // 构建参与签名的字符串
      StringBuilder stringToSign = new StringBuilder();
      stringToSign.append(path).append("\n");
      stringToSign.append(timestamp).append("\n");
      stringToSign.append(body).append("\n");
      // 使用 HMAC-SHA256 算法进行签名
      SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8),algorithm);
      Mac mac = Mac.getInstance(algorithm);
      mac.init(signingKey);
      byte[] signatureBytes = mac.doFinal(stringToSign.toString().getBytes(StandardCharsets.UTF_8));

      // 对签名进行 Base64 编码
      String calculatedSignature = Base64.getEncoder().encodeToString(signatureBytes);// 对比签名值
      if (calculatedSignature.equals(receivedSignature)) {
      	return true;
      }
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
      e.printStackTrace();
      log.info(e.getMessage());
    }
    return false;
  }
public class CachedBodyHttpServletRequest extends HttpServletRequestWrapper {

  private final byte[] cachedBody;

  public CachedBodyHttpServletRequest(HttpServletRequest request) throws IOException {
    super(request);
    InputStream requestInputStream = request.getInputStream();
    this.cachedBody = StreamUtils.copyToByteArray(requestInputStream);
  }

  @Override
  public ServletInputStream getInputStream() {
    return new CachedBodyServletInputStream(this.cachedBody);
  }

  @Override
  public BufferedReader getReader() throws IOException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedBody);
    return new BufferedReader(new InputStreamReader(byteArrayInputStream));
  }

  public String getBody() {
    return new String(cachedBody);
  }
}
public abstract class StreamUtils {
    public static final int BUFFER_SIZE = 4096;

    public StreamUtils() {
    }

    public static byte[] copyToByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
        copy(in, out);
        return out.toByteArray();
    }

    public static int copy(InputStream in, OutputStream out) throws IOException {
        int byteCount = 0;
        byte[] buffer = new byte[4096];

        int bytesRead;
        for(boolean var4 = true; (bytesRead = in.read(buffer)) != -1; byteCount += bytesRead) {
            out.write(buffer, 0, bytesRead);
        }

        out.flush();
        return byteCount;
    }
}
HMAC-SHA256签名是一种使用HMAC算法和SHA-256哈希算法对数据进行签名的方法。HMAC(Hash-based Message Authentication Code)是一种基于哈希函数和密钥的消息认证码算法,用于确保数据的完整性和认证性。SHA-256是一种常用的哈希算法,用于将任意长度的数据转换为固定长度的哈希值。 在JMeter中进行HMAC-SHA256签名逻辑的添加,可以通过在HTTP Request下方添加一个JSR223 PreProcessor来实现。然后,需要编写Python脚本来计算签名,并将签名结果存储到变量中。 以下是一个示例Python脚本,用于计算HMAC-SHA256签名并将结果存储到变量中: ``` import hmac import hashlib api_key = "your_api_key" secret_key = "your_secret_key" timestamp = str(int(time.time() * 1000)) nonce = str(random.randint(100000, 999999)) params = {"amount": 100, "currency": "USD", "merchant_id": 1234} message = f"{timestamp}\n{nonce}\nPOST\n/payments\n{api_key}\n" \ "&".join([f"{k}={v}" for k, v in params.items()]) signature = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256).hexdigest() vars.put("timestamp", timestamp) vars.put("nonce", nonce) vars.put("signature", signature) ``` 在这个示例中,我们使用了API Key和Secret Key来计算签名。首先,我们获取当前时间戳和随机数作为参数,并根据接口要求的格式构建消息。然后,我们使用HMAC函数和SHA-256哈希算法对消息进行签名,得到签名结果。最后,我们将时间戳、随机数和签名存储到JMeter的变量中,以供后续使用。 请根据实际情况修改示例中的API Key、Secret Key和参数,并将脚本添加到JMeter的JSR223 PreProcessor中,以实现HMAC-SHA256签名逻辑。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [揭秘支付接口测试:如何使用JMeter和Python打造绝密HMAC-SHA256签名逻辑?(Base64,Md5,Sha,Rsa等)实战...](https://blog.csdn.net/weixin_48500307/article/details/130274880)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值