shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.walker.pay.allinpay.util;
 
import com.walker.infrastructure.utils.Base64;
import com.walker.infrastructure.utils.MD5;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.pay.allinpay.Constants;
 
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Map;
import java.util.TreeMap;
 
public class SignUtils {
 
    public static String unionSign(TreeMap<String, String> params, String appKey, String signType) throws Exception {
        params.remove("sign");
        if (signType.equals(Constants.SIGN_TYPE_MD5)) {// 如果是md5则需要把md5的key加入到排序
            params.put("key", appKey);
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (entry.getValue() != null && entry.getValue().length() > 0) {
                sb.append(entry.getKey()).append(StringUtils.CHAR_EQUALS).append(entry.getValue()).append(StringUtils.CHAR_AND);
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        String sign = StringUtils.EMPTY_STRING;
        if (signType.equals(Constants.SIGN_TYPE_MD5)) {
//            System.out.println(sb.toString());
//            sign = md5(sb.toString().getBytes("UTF-8"));// 记得是md5编码的加签
            sign = MD5.getMessageDigest(sb.toString().getBytes(StringUtils.DEFAULT_CHARSET_UTF8));
            params.remove("key");
        } else if(signType.equals(Constants.SIGN_TYPE_SM2)){
//            System.out.println(sb.toString());
            PrivateKey privateKey = SmUtil.privKeySM2FromBase64Str(appKey);
            sign = SmUtil.signSM3SM2RetBase64(privateKey, params.get(Constants.VALUE_APP_ID), sb.toString().getBytes(StringUtils.DEFAULT_CHARSET_UTF8));//签名
        } else {
//            System.out.println(sb.toString());
            sign = rsaSign(sb.toString(), appKey, StringUtils.DEFAULT_CHARSET_UTF8);
        }
        return sign;
    }
 
    public static String rsaSign(String content, String privateKey, String charset) throws Exception {
        PrivateKey priKey = getPrivateKeyFromPKCS8(Constants.SIGN_TYPE_RSA,
//                Base64.decodeBase64(privateKey.getBytes()));
                Base64.decode(privateKey.getBytes()));
        return rsaSign(content, priKey, charset);
    }
 
    public static String rsaSign(String content, byte[] privateKey, String charset) throws Exception {
        PrivateKey priKey = getPrivateKeyFromPKCS8(Constants.SIGN_TYPE_RSA, privateKey);
        return rsaSign(content, priKey, charset);
    }
 
    public static String rsaSign(String content, PrivateKey priKey, String charset) throws Exception {
        java.security.Signature signature = java.security.Signature.getInstance("SHA1WithRSA");
        signature.initSign(priKey);
        if (StringUtils.isEmpty(charset)) {
            signature.update(content.getBytes());
        } else {
            signature.update(content.getBytes(charset));
        }
        byte[] signed = signature.sign();
        return new String(Base64.encodeBase64(signed));
    }
 
    public static PrivateKey getPrivateKeyFromPKCS8(String algorithm, byte[] encodedKey) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
        return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
    }
 
//    public static String md5(byte[] b) {
//        try {
//            MessageDigest md = MessageDigest.getInstance("MD5");
//            md.reset();
//            md.update(b);
//            byte[] hash = md.digest();
//            StringBuffer outStrBuf = new StringBuffer(32);
//            for (int i = 0; i < hash.length; i++) {
//                int v = hash[i] & 0xFF;
//                if (v < 16) {
//                    outStrBuf.append('0');
//                }
//                outStrBuf.append(Integer.toString(v, 16).toLowerCase());
//            }
//            return outStrBuf.toString();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//            return new String(b);
//        }
//    }
}