xin
2025-07-02 84277cefdc270f88cd9f2ece3419a428495c7cf2
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
47
48
49
50
51
52
53
54
55
56
package com.oying.modules.hwc.utils;
 
import org.apache.commons.codec.digest.DigestUtils;
 
import java.io.UnsupportedEncodingException;
 
/**
* 功能:MD5签名
* 版本:3.3
* 修改日期:2012-08-17
* */
public class MD5 {
 
    /**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset)).toUpperCase();
    }
 
    /**
     * 签名字符串
     * @param text 需要签名的字符串
     * @param sign 签名结果
     * @param key 密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static boolean verify(String text, String sign, String key, String input_charset) {
        text = text + key;
        String md5Hex = DigestUtils.md5Hex(getContentBytes(text, input_charset));
        return md5Hex.equals(sign);
    }
 
    /**
     * @param content 需要签名的字符串
     * @param charset 编码格式
     * @return 签名结果
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || charset.isEmpty()) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }
 
}