package com.miaxis.common.utils.rsa;


import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class test {


    static String  private_key = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMLNHTgsyYOXDJVF8r4qCyb+8Uw4" +
            "XKq8aThAhUIaPYIop+HJxETGbXk3e0o46i4iOX8RTv0i3jmhL6W0mSxy9ismfXPP7VtKgEt6VnMm" +
            "ufsrQ2kcXyRsKmuwUd3rTH5gniXwLdIPF8V5ujiSq94nPHok1krDMWbss8Q7lcH2kQFHAgMBAAEC" +
            "gYBh6Eu2TzRdMg79+7EciO8gA2Tt5SoWGipcPnl7lsqxX5O5dPpzgrPFB/CshlyVIVh6mA12Rw8g" +
            "MfZLt7CA2asFdgGHfgCvMjsCnL2U1Nf1QFnsnGXmFXeVUvdy7XPILi75rVyOEAw+/f3KzjSA7B3w" +
            "URE8M2QlIUcjm8LLyCVC4QJBAO+taJH7pVZGVn2upsOk14X08QU0lsQ7ufe/8VXe+IujwGMh34U6" +
            "BHGV7grodtA7TvQ3mDggQLHB2+IxBWdDCrcCQQDQEVMI+CD7afEyN9rK/MAJwevtxNaTyEd1bhKw" +
            "t6WKr1YbAdUz2gvnpCQjD1Gqt5yXHKu279msX9W4euXePG3xAkBUCYZn4UdIp+L9sHZSlQLoukik" +
            "HZtbdOGw58Ez7blSJPl5CNB5wyz5sqtDcdzvCEyXvsKFs3FbonB5r9yIJ26jAkEAugGC1dXcxmWj" +
            "dl1wf18M3qw6GkmJ+ntlRpmso8bJev3cYDB3RO99DQw0MhOT0qZqmrzK1bP+SJc0HMPhk5c6QQJA" +
            "WRLIxmeM/P9X2mDjMrI46Wnn27QoKGgYSHdPxmLD0y7NUytf5HyCsY2vhA9FZcnikibax7SwzC/B" +
            "D7yOXFAy/Q==";
    static String public_key ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCzR04LMmDlwyVRfK+Kgsm/vFMOFyqvGk4QIVC" +
            "Gj2CKKfhycRExm15N3tKOOouIjl/EU79It45oS+ltJkscvYrJn1zz+1bSoBLelZzJrn7K0NpHF8k" +
            "bCprsFHd60x+YJ4l8C3SDxfFebo4kqveJzx6JNZKwzFm7LPEO5XB9pEBRwIDAQAB";

    public static void main(String[] args) throws Exception {
        String content  = "我很666";
        // 进行签名服务
        Signature signature = Signature.getInstance("SHA256withRSA");
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey privateKey =  kf.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(private_key)));

        signature.initSign(privateKey);
        signature.update(content.getBytes("UTF-8"));
        byte[] signedData = signature.sign();
        String base64Str =  Base64.getEncoder().encodeToString(signedData);
        System.out.println("签名值为:"+base64Str);

        //验签

        PublicKey publicKey =  kf.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(public_key)));
        signature.initVerify(publicKey);
        // 把我们构造的验签名串更新到签名器中
        signature.update(content.toString().getBytes(StandardCharsets.UTF_8));
        Boolean result = signature.verify(Base64Utils.decodeFromString(base64Str));
        System.out.println("签名结果:"+result);


    }

    public Boolean validateSign(String openId , String isVip ,String sign) throws Exception{
        //rsa 公钥
        String publicKeyBase64 ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCzR04LMmDlwyVRfK+Kgsm/vFMOFyqvGk4QIVC" +
                "Gj2CKKfhycRExm15N3tKOOouIjl/EU79It45oS+ltJkscvYrJn1zz+1bSoBLelZzJrn7K0NpHF8k" +
                "bCprsFHd60x+YJ4l8C3SDxfFebo4kqveJzx6JNZKwzFm7LPEO5XB9pEBRwIDAQAB";
        // 构造被签名串
        String verifyString = openId+ isVip;
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PublicKey publicKey =  kf.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyBase64)));
        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initVerify(publicKey);
        // 把我们构造的被签名串更新到签名器中
        signature.update(verifyString.getBytes("UTF-8"));
        return signature.verify(Base64Utils.decodeFromString(sign));

    }


}
