package com.yeejoin.amos.boot.module.hygf.api.util;



        import java.io.FileInputStream;
        import java.io.InputStreamReader;
        import java.nio.charset.StandardCharsets;
        import java.security.KeyFactory;
        import java.security.PrivateKey;
        import java.security.PublicKey;
        import java.security.Signature;
        import java.security.cert.CertificateFactory;
        import java.security.cert.X509Certificate;
        import java.security.interfaces.RSAPublicKey;
        import java.security.spec.PKCS8EncodedKeySpec;
        import java.security.spec.X509EncodedKeySpec;

        import org.apache.commons.codec.binary.Base64;
        import org.bouncycastle.util.io.pem.PemReader;
/**
 * SHA256WithRSA签名、验签工具
 *
 * @FileName: RSASignUtils.java
 */
public class RSASignUtils {

    /**
     * 日志
     */


    private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
    private static final String SIGN_SHA1RSA_ALGORITHMS = "SHA1WithRSA";

    /**
     * 签名
     *
     * @param content
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String sign(String content, PrivateKey privateKey) throws Exception {
        Signature signature = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS);
        signature.initSign(privateKey);
        signature.update(content.getBytes(StandardCharsets.UTF_8));
        // 签名使用Base64编码后得到的值即为请求数据中signature字段的值
        return Base64.encodeBase64String(signature.sign());
    }

    public static String signWithByte(byte[] content, PrivateKey privateKey) throws Exception {
        Signature signature = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS);
        signature.initSign(privateKey);
        signature.update(content);
        // 签名使用Base64编码后得到的值即为请求数据中signature字段的值
        return Base64.encodeBase64String(signature.sign());
    }

    public static byte[] signWithByte_New(byte[] content, PrivateKey privateKey) throws Exception {
        Signature signature = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS);
        signature.initSign(privateKey);
        signature.update(content);
        // 签名使用Base64编码后得到的值即为请求数据中signature字段的值
        return signature.sign();
    }

    /**
     * 验签
     *
     * @param content
     * @param signature
     * @param publicKey
     * @return
     */
    public static Boolean checkSign(String content, String signature, PublicKey publicKey) {
        try {

            Signature signatureTool = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS);
            signatureTool.initVerify(publicKey);
            signatureTool.update(content.getBytes(StandardCharsets.UTF_8));
            byte[] signbyte = Base64.decodeBase64(signature.getBytes());
            return signatureTool.verify(signbyte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static Boolean checkSignWithBytes(byte[] src_Bytes, byte[] signBytes, PublicKey publicKey) {
        try {

            Signature signatureTool = Signature.getInstance(SIGN_SHA1RSA_ALGORITHMS);
            signatureTool.initVerify(publicKey);
            signatureTool.update(src_Bytes);
            byte[] signbyte = (signBytes);
            return signatureTool.verify(signbyte);
        } catch (Exception e) {
          e.printStackTrace();
        }
        return false;
    }

    public static Boolean checkSignWithBytes256(byte[] src_Bytes, byte[] signBytes, PublicKey publicKey) {
        try {

            Signature signatureTool = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS);
            signatureTool.initVerify(publicKey);
            signatureTool.update(src_Bytes);
            byte[] signbyte = (signBytes);
            return signatureTool.verify(signbyte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 加载 pkcs8 格式私钥
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static PrivateKey loadPrivateKey(String path) throws Exception {
        PemReader pemReader = new PemReader(new InputStreamReader(new FileInputStream(path)));
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(pemReader.readPemObject().getContent());
        pemReader.close();
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(pkcs8EncodedKeySpec);
    }

    /**
     * 从证书加载公钥
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static PublicKey loadPublicKey(String path) throws Exception {
        CertificateFactory fact = CertificateFactory.getInstance("X.509");
        FileInputStream is = new FileInputStream(path);
        X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
        return cer.getPublicKey();
    }

    /**
     * 从文件加载 PKCS8 格式的 RSA 公钥
     */
    public static RSAPublicKey readPublicKeyFromFile(String path) throws Exception {
        String publicKeyPEM ="-----BEGIN PUBLIC KEY-----\n" +
                "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwFgHD4kzEVPdOj03ctKM7KV+1\n" +
                "6bWZ5BMNgvEeuEQwfQYkRVwI9HFOGkwNTMn5hiJXHnlXYCX+zp5r6R52MY0O7BsT\n" +
                "CLT7aHaxsANsvI9ABGx3OaTVlPB59M6GPbJh0uXvio0m1r/lTW3Z60RU6Q3oid/r\n" +
                "NhP3CiNgg0W6O3AGqwIDAQAB\n" +
                "-----END PUBLIC KEY-----";
        publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----", "")
                .replace("-----END PUBLIC KEY-----", "")
                .replaceAll("\\r", "").replaceAll("\\n", "");

        byte[] publicKeyDER = java.util.Base64.getDecoder().decode(publicKeyPEM);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyDER));
    }


    public static void main(String[] args) throws Exception {

//        FileUtils.save2File(
//                "/Users/cyzx-mengxr/Downloads/00008_0202202280000000579_0200003309088104357_001_20220930_acc_1_1_New.sign",
//                RSASignUtils.signWithByte_New( FileUtils.toByteArray("/Users/cyzx-mengxr/Downloads/00008_0202202280000000579_0200003309088104357_001_20220930_acc_1_1.zip"),
//                RSASignUtils .loadPrivateKey("/Users/cyzx-mengxr/Downloads/private.pem")));



       //String signSrc = RSAUtil.byte2Hex(FileUtils.toByteArray("/Users/cyzx-mengxr/Downloads/00008_0202202280000000579_0200003309088104357_001_20220930_acc_1_1.sign"));
       //String signSrc = RSAUtil(FileUtils.toByteArray("/Users/cyzx-mengxr/Downloads/00008_0202202280000000579_0200003309088104357_001_20220930_acc_1_1.sign"));
        /*验证网关文件*/

//        System.out.println( RSASignUtils.checkSignWithBytes(
//                FileUtils.toByteArray("/Users/cyzx-mengxr/Downloads/00008_0202202280000000579_0200003309088104357_001_20220930_acc_1_1.zip"),
//                FileUtils.toByteArray("/Users/cyzx-mengxr/Downloads/00008_0202202280000000579_0200003309088104357_001_20220930_acc_1_1.sign"),
//                RSASignUtils.readPublicKeyFromFile("/Users/cyzx-mengxr/Downloads/public.pem")));


        System.out.println(RSASignUtils.checkSignWithBytes256(
                FileUtils.toByteArray("/Users/cyzx-mengxr/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/dc1430eae477f5ad06811d277af61ff6/Message/MessageTemp/bb418fb928671a5accdc29aba40c4b42/File/ENTRUSTRES-20240331JB00400003.bin"),
                FileUtils.toByteArray("/Users/cyzx-mengxr/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/dc1430eae477f5ad06811d277af61ff6/Message/MessageTemp/bb418fb928671a5accdc29aba40c4b42/File/ENTRUSTRES-20240331JB00400003.sign"),
                RSASignUtils.readPublicKeyFromFile("/Users/cyzx-mengxr/Downloads/public.pem")));

    }
}