xuhy
2025-04-27 5fa6e6f8410ef9d057174bcff2a3c5038c54a551
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
package com.ruoyi.system.utils.wx.tools;
 
import java.security.MessageDigest;
 
public class SHA1 {
 
 
    /**
     * 用SHA1算法生成安全签名
     *
     * @param str
     * @return
     * @throws WxException
     */
    public static String getSHA1(String str) throws WxException {
        try {
            // SHA1签名生成
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes());
            byte[] digest = md.digest();
            StringBuffer hexstr = new StringBuffer();
            String shaHex;
            for (int i = 0; i < digest.length; i++) {
                shaHex = Integer.toHexString(digest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexstr.append(0);
                }
                hexstr.append(shaHex);
            }
            return hexstr.toString();
        } catch (Exception e) {
            throw new WxException(WxException.ComputeSignatureError);
        }
    }
 
}