using System;
using System.Text;
using GMCrypto.Lib;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using PTMedicalInsurance.Variables;

namespace AnHuiMI.Common
{
    class SignUtils
    {
		public static string signSm3WithSm2(string sm4key, string prvkey, string data)
		{
			ECPrivateKeyParameters privatekeyFromD = GmUtil.GetPrivatekeyFromD(new BigInteger(Convert.FromBase64String(prvkey)));
			return Convert.ToBase64String(GmUtil.SignSm3WithSm2(Encoding.UTF8.GetBytes(data), Encoding.UTF8.GetBytes(sm4key), privatekeyFromD));
		}

		public static bool verifySm3WithSm2(string msg, string sm4key, string signData, string pubKey)
		{
			byte[] array = Convert.FromBase64String(pubKey);
			BitConverter.ToString(array);
			AsymmetricKeyParameter publickeyFromXY = GmUtil.GetPublickeyFromXY(array);
			byte[] rs = Convert.FromBase64String(signData);
			return GmUtil.VerifySm3WithSm2(Encoding.UTF8.GetBytes(msg), Encoding.UTF8.GetBytes(sm4key), rs, publickeyFromXY);
		}


		public static long CurrentTimeStamp(bool isMinseconds = false)
		{
			TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
			return Convert.ToInt64(isMinseconds ? timeSpan.TotalMilliseconds : timeSpan.TotalSeconds);
		}

		public static string encryptMsg(string appId, string appSecret, string prvkey, string data, ref string rtSignPlain)
		{
			string ts = Convert.ToString(CurrentTimeStamp());

			JObject jobject = new JObject();
			jobject.Add("appId", appId);
			jobject.Add("data", JObject.Parse(data));
			jobject.Add("encType", "SM4");
			jobject.Add("signType", "SM2");
			jobject.Add("timestamp", ts); ;
			jobject.Add("version", "2.0.1");

			string signData = SMUtil.sign(jobject, appSecret, prvkey);
			string encData = SMUtil.encrypt(data, appId, appSecret);

			jobject.Add("encData", encData);
			jobject.Add("signData", signData);
			// 删除明文
			jobject.Remove("data");

			rtSignPlain = signData;

			return jobject.ToString(Formatting.None, null);
		}


		public static string sm4Encrypt(string chnlId, string sm4key, string message)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(chnlId.Substring(0, 16));
			byte[] bytes2 = Encoding.UTF8.GetBytes(sm4key);
			byte[] bytes3 = Encoding.UTF8.GetBytes(message);
			string text = Hex.ToHexString(GmUtil.Sm4EncryptECB(bytes, bytes2, "SM4/ECB/PKCS7Padding")).ToUpper();
			return Hex.ToHexString(GmUtil.Sm4EncryptECB(Encoding.UTF8.GetBytes(text.Substring(0, 16)), bytes3, "SM4/ECB/PKCS7Padding"));
		}

		public static string sm4Decrypt(string chnlId, string sm4key, string message)
		{
			byte[] bytes = Encoding.UTF8.GetBytes(chnlId.Substring(0, 16));
			byte[] bytes2 = Encoding.UTF8.GetBytes(sm4key);
			byte[] cipher = Hex.Decode(message);
			string text = BitConverter.ToString(GmUtil.Sm4EncryptECB(bytes, bytes2, "SM4/ECB/PKCS7Padding"), 0).Replace("-", string.Empty).ToUpper();
			byte[] bytes3 = GmUtil.Sm4DecryptECB(Encoding.UTF8.GetBytes(text.Substring(0, 16)), cipher, "SM4/ECB/PKCS7Padding");
			return Encoding.UTF8.GetString(bytes3);
		}
	}
}
