using System.IO;
using Com.Bocom.OpenApi.Security.Crypt;
using Com.Bocom.OpenApi.Utils;

namespace Com.Bocom.OpenApi
{
    public class BocomEncrypt
    {
        private static RSACryptor.RSAFromPkcs8 rsaCryptor =
            new RSACryptor.RSAFromPkcs8();

        /// <summary>
        /// 对称加密，仅支持AES256和SM4。
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="encryptType"></param>
        /// <param name="secretKey"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        public static string SymmetricalEncrypt(string plainText, string encryptType, string secretKey, string charset)
        {
            if (BocomConstants.ENCRYPT_TYPE_RSA_AND_AES == encryptType)
            {
                return AESCryptor.Encrypt(plainText, secretKey, charset);
            }

            if (BocomConstants.ENCRYPT_TYPE_SM2_AND_SM4 == encryptType)
            {
                return SM4Cryptor.Encrypt(plainText, secretKey, charset);
            }

            throw new BocomApiException("对称加密仅支持AES256和SM4！");
        }

        /// <summary>
        /// 对称解密，仅支持AES256和SM4。
        /// </summary>
        /// <param name="cipherText"></param>
        /// <param name="encryptType"></param>
        /// <param name="secretKey"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        /// <exception cref="BocomApiException"></exception>
        public static string SymmetricalDecrypt(string cipherText, string encryptType, string secretKey, string charset)
        {
            if (BocomConstants.ENCRYPT_TYPE_RSA_AND_AES == encryptType)
            {
                return AESCryptor.Decrypt(cipherText, secretKey, charset);
            }

            if (BocomConstants.ENCRYPT_TYPE_SM2_AND_SM4 == encryptType)
            {
                return SM4Cryptor.Decrypt(cipherText, secretKey, charset);
            }

            throw new BocomApiException("对称解密仅支持AES256和SM4！");
        }

        /// <summary>
        /// 非对称加密，仅支持RSA和SM2。
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="encryptType"></param>
        /// <param name="publicKey"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        /// <exception cref="BocomApiException"></exception>
        public static string AsymmetricalEncrypt(string plainText, string encryptType, string publicKey, string charset)
        {
            if (BocomConstants.ENCRYPT_TYPE_RSA_AND_AES == encryptType)
            {
                return rsaCryptor.EncryptData(plainText, publicKey, charset);
            }

            if (BocomConstants.ENCRYPT_TYPE_SM2_AND_SM4 == encryptType)
            {
                return SM2Cryptor.Encrypt(plainText, publicKey, charset);
            }

            throw new BocomApiException("非对称加密仅支持RSA和SM2！");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cipherText"></param>
        /// <param name="encryptType"></param>
        /// <param name="privateKey"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        public static string AsymmetricalDecrypt(string cipherText, string encryptType, string privateKey,
            string charset)
        {
            if (BocomConstants.ENCRYPT_TYPE_RSA_AND_AES == encryptType)
            {
                return rsaCryptor.DecryptData(cipherText, privateKey, charset);
            }

            if (BocomConstants.ENCRYPT_TYPE_SM2_AND_SM4 == encryptType)
            {
                return SM2Cryptor.Decrypt(cipherText, privateKey, charset);
            }

            throw new BocomApiException("非对称解密仅支持RSA和SM2！");
        }

        public static byte[] EncryptFile(FileStream fi, string cryptKey, string encryptType)
        {
            if (BocomConstants.ENCRYPT_TYPE_RSA_AND_AES == encryptType)
            {
                return AESCryptor.EncryptFile(fi, cryptKey);
            }

            if (BocomConstants.ENCRYPT_TYPE_SM2_AND_SM4 == encryptType)
            {
                // SM2加密（缺省）
                return null;
            }

            return null;
        }

        public static byte[] DecryptFile(FileStream fi, string cryptKey, string encryptType)
        {
            if (BocomConstants.ENCRYPT_TYPE_RSA_AND_AES == encryptType)
            {
                return AESCryptor.DecryptFile(fi, cryptKey);
            }

            if (BocomConstants.ENCRYPT_TYPE_SM2_AND_SM4 == encryptType)
            {
                // SM2加密（缺省）
                return null;
            }

            return null;
        }
    }
}