rentaiming
2024-05-21 dd7ad1ab0ead2aa3c9f7fc35cc9b0635309c642a
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.order.util.alipay.sign;
 
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
 
public class SignUtils {
 
    private static final String ALGORITHM = "RSA";
 
    private static final String SIGN_ALGORITHMS = "SHA1WithRSA";
 
    private static final String DEFAULT_CHARSET = "UTF-8";
 
    public static String sign(String content, String privateKey) {
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey));
            KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);
 
            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
 
            signature.initSign(priKey);
            signature.update(content.getBytes(DEFAULT_CHARSET));
 
            byte[] signed = signature.sign();
 
            return Base64.encode(signed);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
}