无关风月
2025-08-10 f0e738ecd05cba3d71b144433789bc08cb101911
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.ruoyi.other.util.pay;
 
import org.apache.commons.codec.digest.DigestUtils;
 
import java.security.MessageDigest;
 
/**
 * 类MD5_Sign:MD5签名和验签
 * 
 * @author Lori 2018年6月04日 下午16:10:04
 */
public class Md5_Sign {
 
    /**
     * MD5签名
     * 
     * @param requestSign 请求签名串
     * @param merchantKey 商户秘钥
     */
    public static String SignByMD5(String requestSign, String merchantKey) {
        
        String reqHmac = "";
        try {
            reqHmac = DigestUtils.md5Hex(requestSign + merchantKey).toUpperCase();
        } catch (Exception e) {}
        
        return reqHmac;
    }
 
    public static String SignByMD5A(String content, String key) {
        try {
            // 汇付天下可能需要在内容后加上key
            String toSign = content + key;
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(toSign.getBytes("UTF-8"));
 
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            throw new RuntimeException("MD5签名失败", e);
        }
    }
}