wpc
2018-11-27 680fbc9e73da3e11988557cf88fd935efd3e0b1e
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
97
98
99
100
101
/*
Copyright (C) Huawei Technologies Co., Ltd. 2015. All rights reserved.
See LICENSE.txt for this sample's licensing information.
*/
 
package com.huawei.android.hms.agent.game.checksignutil;
 
import android.text.TextUtils;
 
import com.huawei.android.hms.agent.common.HMSAgentLog;
 
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
 
public class RSAUtil {
 
    public static final String KEY_ALGORITHM = "RSA";
 
    public static final String SIGNATURE_ALGORITHM_SHA256 = "SHA256WithRSA";
 
    public static boolean doCheck(String content, String sign, String publicKey) {
        if (TextUtils.isEmpty(publicKey)) {
            return false;
        }
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
            byte[] encodedKey = Base64Util.decode(publicKey);
            PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
 
            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM_SHA256);
            
            signature.initVerify(pubKey);
            signature.update(content.getBytes("utf-8"));
            
            return signature.verify(Base64Util.decode(sign));
        }
        catch (Exception localException) {
            HMSAgentLog.e("check error");
        }
        return false;
    }
 
    public static String sign(String content, String privateKey) {
        if ((content == null) || (privateKey == null)) {
            return null;
        }
        String charset = "utf-8";
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Util.decode(privateKey));
            KeyFactory keyf = KeyFactory.getInstance(KEY_ALGORITHM);
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);
 
            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM_SHA256);
            
            signature.initSign(priKey);
            signature.update(content.getBytes(charset));
            
            byte[] signed = signature.sign();
            
            return Base64Util.encode(signed);
        }
        catch (Exception localException) {
            HMSAgentLog.e("sign error");
        }
        return null;
    }
 
 
 
    public static String urlEncode(String src) {
        if(src == null) {
            return null;
        }else {
            try {
                return URLEncoder.encode(src, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("~", "%7E");
            } catch (UnsupportedEncodingException var2) {
                return null;
            }
        }
    }
 
    public static String urlDecode(String src)
    {
        if(src == null) {
            return null;
        }else {
            try {
                return URLDecoder.decode(src, "UTF-8");
            } catch (UnsupportedEncodingException var2) {
                return null;
            }
        }
    }
}