package cmb

import (
	"encoding/json"
	"fmt"
	_ "gitea.cdlsxd.cn/self-tools/l_crypt"
	"testing"
	v1 "voucher/api/v1"
)

func TestGenerateSm2Key(t *testing.T) {
	got, got1 := GenerateSm2Key()
	t.Log("SM2私钥:", got, len(got))
	t.Log("SM2公钥:", got1, len(got1))

	smbPrk := "9450c673cf801164435b9c164ac1404e87997245bc6f323fde22015875a03f6e"
	cmbPuk := "04a702106cf530dc981e44cd515b394747cfd6bb059247696b188b25281ea4278fe7c6e34a83680110eec71becd31f5db14abc671e5d8e67ce7ca3c6b3adc86674"
	t.Log("掌上生活SM2私钥:", smbPrk, len(smbPrk))
	t.Log("掌上生活SM2公钥:", cmbPuk, len(cmbPuk))
}

func TestEncrypt(t *testing.T) {
	pukKey := "04a702106cf530dc981e44cd515b394747cfd6bb059247696b188b25281ea4278fe7c6e34a83680110eec71becd31f5db14abc671e5d8e67ce7ca3c6b3adc86674"
	content := `{"name":"zhangxx","phoneNo":"137xxxxxxxx"}`
	xx, err := Encrypt(pukKey, content)
	t.Log(xx, err)
}

func TestDecrypt(t *testing.T) {
	priKey := "8d39ff3d2559258c163f4510f082727f51531e1953ab203d5ab1ea4a6d94fd73"

	//content := "BAdcIauIjNx3LsrplpJiZoljE4hCiGHra6ulhgG1qL0tKcAeenX+Z9VaHfXLSdkji1fYBpdZiiI35R0vFtnXPXJCJdHsGbfbae+PzNznYQS3KM8/90Y/FIWzSoszfUiF6fAuv8I6v9kQuqHUTidHeHyICDoyvJ0nhbNyUyg85bAKd6TmkVX1MgXLQ81m|5KfR/5UkpVBEQv1dx+iJbojOykNRuDV8Gsy3QOIlRI+cZvafRRPUUG6eeixnPMumhOvyZwsSG/OBeg0U/lSlAepg12tXWcQ601wjgyLaKN1iMvb1DCtfnJFAm8EWAc2SLH3NQuyhxGe/jgCXvj0wGphh4vBUzm8la8i8Aij0BI5lfgU5OzglkKDln6zHN3vBHDqOurEh18eU6z1bfvNnDpzdwEcygcEIH/6lGiqVnGH+C2+QpcKeCnj5qKGFiuSC"
	//content := "BDUuuPClIlUKDJdpHtNBIU6u5JTetrVG53AfKDSrhah9Q0QZWAj3K5pZF8G/HFtzj/KvbrHfP/gnHri0L7L91HaKYYc1vqy/q8Z69v7MEiIWL2LeLOsc2b0cHmnt7Qey5aZzYVbZJJNhus2gvhahGwOPpPL50JFC8IqAlU4+E/kUBgx+RgzAIkLLs8Se|k1WOt2Eb+xkxSobw/1DaLblguznhnsk1ga87sfqrrPATRuTyszzdVrRtCUuUHRKJ+vQwlZAR1ypkBC1vPMdt8zccI/CeNof+4Ap23enbQwTJQ4KRvij4kbJd6ycY97B+vHnI+oSnwfmjK0EWNUeRrCr2Uau7yxmzlFwJHprbgPZpHVuUjAzMIXNixOfxSTdc9dL+j3/tR7+yookvd9W3hNm96XZFPTheI0FqUKkbKWUUcVs4lC1/yEgjwXFcD4NM"
	content := "BB6AXQSC+ZGUudJrAxUDSzdDRDooDXZkTMZkUQyHjDFI6s/9dwtNS2ZgM68Yj7x2e5d4mjIrnKh58m3ut12glqF34tFM+L3OfaWPUHstybr0MoEqerRVKcQCKFUTmVCaM8z4aVEGCqOWJ2i0BbPSGN/kL0BmRHF6npQwEE2kGDLPAkE6n3h9soCJLzr2|w078/Op8yMsZ3hN9C0s9oWWh8HPrkCte52UTvEBF1KT/wUwDZH8OHtoePQ2SkiikdSnUNz5Sc6VijIjpkaJjEtOCVLx7KZ+lQPu/6GFdyQLdHNpUdRZsA2NDw/6+hzGQOCZ1sAi3CQdIHwRxZkfiXfWMMrtygUD+evUUDoSr1U/YkK4HxC7wRa9Kmxmk/AK8aMcaTi78M5vmUp+CE50TDl40G+//+75cw6IZp1L/nCXbEml+9flMfxoc1RuYSXlj"

	decryptStr, err := Decrypt(priKey, content)
	if err != nil {
		t.Log(err)
		return
	}

	t.Logf(decryptStr)
}

func TestEncryptDecrypt(t *testing.T) {
	priKey := "8d39ff3d2559258c163f4510f082727f51531e1953ab203d5ab1ea4a6d94fd73"
	pukKey := "04d827a7dbaaa358ce45b8c7794a7f54819f5c175005a702370e47f135ef6f5f9732758b1474f218419fe9e87f90c28c3b05f08254c651db27df35fae67b77b2e4"

	content := `{"name":"zhangxx","phoneNo":"137xxxxxxxx"}`

	xx, err := Encrypt(pukKey, content)
	if err != nil {
		t.Log(err)
		return
	}
	t.Log(xx)

	aa, err := Decrypt(priKey, xx)
	if err != nil {
		t.Log(err)
		return
	}

	t.Log(aa)
}

func TestSign(t *testing.T) {
	//priKey := "9450c673cf801164435b9c164ac1404e87997245bc6f323fde22015875a03f6e"
	priKey := "8d39ff3d2559258c163f4510f082727f51531e1953ab203d5ab1ea4a6d94fd73"

	content := "accessToken.json?aid=9dad6d3900ec3ffabd80e46522a10ead&cmbKeyAlias=SM2_CMBLIFE&date=20240314091628&encryptBody=BNhquiza494xicGIOvE7G5jLr7nhQI6Tp4V5j3a+3P98oD+uUKVXAPXt+ae3GsDvs+FyOaqoNSzdYsSqNDW3rA1AFUbsljTI9EaWMB+FAHpZ3Wjn6Qyl8EzJpUIbCnqAIOWjr2hDsexJd+NPVxQwNqq9W66lC/PBC4/1/QCz+87Yq8b4hHx7bP2u5h95|YwTJn3uKMQfhDSuJKIx3hyFZcoLm9M2xKnVDmNLPq5PbGedoZY/4g1Z/sKk8cWzH&keyAlias=CO_PUB_KEY_SM2&mid=f806c259d86e3b9aa956c98d475b6af7&random=320bcb8c8cf7419e98d562439bdb3baa"

	xx, err := Sign(priKey, content)
	t.Log(xx, err)
}

func TestVerify(t *testing.T) {
	//pukKey := "04838f74275e6f4f2373d4e6e974ac790c10ab6f9c17e273cf0c84848c6838979c158315932e36f0b9444442f145e4671b1ee5d43d5d63913a70d4d0d52cc3c0d6"
	pukKey := "04d827a7dbaaa358ce45b8c7794a7f54819f5c175005a702370e47f135ef6f5f9732758b1474f218419fe9e87f90c28c3b05f08254c651db27df35fae67b77b2e4"

	content := "accessToken.json?aid=9dad6d3900ec3ffabd80e46522a10ead&cmbKeyAlias=SM2_CMBLIFE&date=20240314091628&encryptBody=BNhquiza494xicGIOvE7G5jLr7nhQI6Tp4V5j3a+3P98oD+uUKVXAPXt+ae3GsDvs+FyOaqoNSzdYsSqNDW3rA1AFUbsljTI9EaWMB+FAHpZ3Wjn6Qyl8EzJpUIbCnqAIOWjr2hDsexJd+NPVxQwNqq9W66lC/PBC4/1/QCz+87Yq8b4hHx7bP2u5h95|YwTJn3uKMQfhDSuJKIx3hyFZcoLm9M2xKnVDmNLPq5PbGedoZY/4g1Z/sKk8cWzH&keyAlias=CO_PUB_KEY_SM2&mid=f806c259d86e3b9aa956c98d475b6af7&random=320bcb8c8cf7419e98d562439bdb3baa"
	//signDataBase64 := "MEUCIAg1zxnKG+X8t/hlwEoyL/T33iKgle09S6bfb3eZh/FqAiEAxtK5TdGAo+JpK7JmL15tT4nlQyrUzC14flu0Tq+9Svo="
	signDataBase64 := "MEUCICL72ttxrYiuooO25vLsnp42YMKAoaN2y79/LED+M5WcAiEAgneRDvfZ+LhN3XvgxL3B4L6TYrzBnzeqjIb9B7KPxVc="

	xx, err := Verify(pukKey, content, signDataBase64)
	t.Log(xx, err)

	ok, err := VerifyBody(content, signDataBase64, pukKey)
	fmt.Println(ok, err)
}

func TestVerifyCmbReply(t *testing.T) {
	zsStr := "cmbKeyAlias=SM2_CMBLIFE&date=20250312094153&encryptBody=BPaYG3nLlLJvnu6tHBBNKCwXigqN/n8yhUbVRtaKWCIb06GkYo/ykWSNXFRKEPzaYNEeh49frKlVckqvq0heDIYxSUs9uozl5dDnkcFwBv2UK9jxlTRHLkO7MznBD3p/et/m01FsTlMXQcMjL46HsvRZUsXVwpeb56cS4/kQ+zum5mLhgXVv4Mkpheig|JCmQuJMe/WkF9JjLoPspNRK6QxC+ViGT9TccJKUal1dgcawKGLbcoAIwd0zO/6kkFwzkjp83+iwRu5WOsO0ver27v1671RmRJQAObxtmk0LRf7JeHbq+KTd/cGbGu+ZZZcQzPIgfrUTt3TTx8eT0aQ==&keyAlias=CO_PUB_KEY_SM2&respCode=1000&respMsg=成功"
	prk := "8d39ff3d2559258c163f4510f082727f51531e1953ab203d5ab1ea4a6d94fd73"

	reply := &v1.CmbReply{
		RespCode:    "1000",
		RespMsg:     "成功",
		Date:        "20250312094153",
		KeyAlias:    "CO_PUB_KEY_SM2",
		CmbKeyAlias: "SM2_CMBLIFE",
		EncryptBody: "BPaYG3nLlLJvnu6tHBBNKCwXigqN/n8yhUbVRtaKWCIb06GkYo/ykWSNXFRKEPzaYNEeh49frKlVckqvq0heDIYxSUs9uozl5dDnkcFwBv2UK9jxlTRHLkO7MznBD3p/et/m01FsTlMXQcMjL46HsvRZUsXVwpeb56cS4/kQ+zum5mLhgXVv4Mkpheig|JCmQuJMe/WkF9JjLoPspNRK6QxC+ViGT9TccJKUal1dgcawKGLbcoAIwd0zO/6kkFwzkjp83+iwRu5WOsO0ver27v1671RmRJQAObxtmk0LRf7JeHbq+KTd/cGbGu+ZZZcQzPIgfrUTt3TTx8eT0aQ==",
		Sign:        "",
	}

	str := SortStructStr(reply)
	t.Log(str)

	t.Log(zsStr == str)

	xx, err := Sign(prk, str)
	if err != nil {
		t.Log(err)
		return
	}
	t.Log(xx)
}

func TestCmbReply(t *testing.T) {

	req := []byte(`{"date":"20250313173740","keyAlias":"CO_PUB_KEY_SM2","sign":"MEUCIEMi826+YIMNrCb0iexVGgkbV2nbjcGIWqZcPu+XaPU7AiEA6INeLjYYpgSX2eePSLNAinoQsTKCkjwQwN1vT8yEggY=","encryptBody":"BHhdJbBP8hybQ57niOICuPiaNTGYKgLF3Ntf1ShdpHDjS6s3UJOCpSxEj0nWCVGlJMcTBJIBZ0pvPD4/oWKxgmp69MhpR3VWJeDcHhJHALdvzl2CR2GTXB7lgy1eUEBH/HlYxCK2P3kcV2ENKkazQRpT2Gk5WJ0rCV8iSaEqovmjRsTV3V4eLLrcuzK+|NZQ2Ae4McLlpSjBhYWQFmc9g0h/DQHcykGwIproe0AA0HayMun04aalcZmzh4Qp5p1SBZNBfwqtETyEjHYlWTj7/ht+kWsWRkuVx1uipZVIYPkOw5thsjoCdK+q1S76EssDCXOTVf/XAoSgqV+6/a1dGwkQgzXVGkGdAUoOdN0Rry8g8JnTBQZyiQY/fjDTiieJiS9MUmm6JiE/wZGG+LliUKHVsF8ZdrIEyrkSgAWw=","cmbKeyAlias":"SM2_CMBLIFE"}`)

	var reply *v1.CmbReply

	err := json.Unmarshal(req, &reply)
	if err != nil {
		t.Log(err)
		return
	}

	t.Logf("reply:%+v", reply)

	prk := "8d39ff3d2559258c163f4510f082727f51531e1953ab203d5ab1ea4a6d94fd73"

	cmbPuk := "0416445bc16cbf42e47002ad9fe7c7af67d902b48be1eb69b98f6a006b0918630e1127f5f2fff83b2ecb30fc7fd72c34c33f37c7c355dffde3589f66800f0036ca"

	str := SortStructStr(reply)
	t.Log(str)

	xx, err := Verify(cmbPuk, str, reply.Sign)
	if err != nil {
		t.Log(err)
		return
	}
	t.Log(xx)

	aa, err := Decrypt(prk, reply.EncryptBody)
	if err != nil {
		t.Log(err)
		return
	}
	t.Log(aa)
}

func TestGenerateSm2KeyAndEncryptDecrypt(t *testing.T) {
	priKey, pukKey := GenerateSm2Key()

	t.Log("SM2私钥:", priKey, len(priKey))
	t.Log("SM2公钥:", pukKey, len(pukKey))

	content := `{"name":"zhangxx","phoneNo":"137xxxxxxxx"}`

	xx, err := Encrypt(pukKey, content)
	if err != nil {
		t.Log(err)
		return
	}
	t.Log(xx)

	aa, err := Decrypt(priKey, xx)
	if err != nil {
		t.Log(err)
		return
	}
	t.Log(aa)
}

func TestVerifyBody(t *testing.T) {
	pukKey := "04838f74275e6f4f2373d4e6e974ac790c10ab6f9c17e273cf0c84848c6838979c158315932e36f0b9444442f145e4671b1ee5d43d5d63913a70d4d0d52cc3c0d6"

	content := "accessToken.json?aid=9dad6d3900ec3ffabd80e46522a10ead&cmbKeyAlias=SM2_CMBLIFE&date=20250305115032&encryptBody=BHeko/ZYFzQOJn6Q3y46X1AjNz8Nh5fq1FfMuWebh+TangLnlK5iFqePCst4rjG/FKJInijiKO2Qq18sJULlMEEgri05s+bHHDKM+Y+73crAbCnIhHbZxUjt8A0cq2rKjzkl8bxW33dU18uuiTEAmsAvKvmZgE6zJ1eDyjFWefHEIFJaKCNY2cTQOInt|8UYFZFTkx0DovPhaWCbdBkAqbgGmegT14F5gwXLJ6G1uWdYNvX+i5QWAYUGtd8u9&keyAlias=CO_PUB_KEY_SM2&mid=f806c259d86e3b9aa956c98d475b6af7&random=286d97b1d8ed4bbf822b004470c92ae8"
	signDataBase64 := "MEUCIAg1zxnKG+X8t/hlwEoyL/T33iKgle09S6bfb3eZh/FqAiEAxtK5TdGAo+JpK7JmL15tT4nlQyrUzC14flu0Tq+9Svo="

	ok, err := VerifyBody(content, signDataBase64, pukKey)
	fmt.Println(ok, err)
}

func TestDecryptBody(t *testing.T) {
	priKey := "f6a8d2f412e289686aba6a0f33cad1a64367d0ba012046ee0fbbefd3ffd675bd" // 测试
	//priKey := "8d39ff3d2559258c163f4510f082727f51531e1953ab203d5ab1ea4a6d94fd73"

	content := "BH666zulhEdX6Axd3+LUaPo0a6WU1ze2qKFrtu+pxq4EZAy1TPY1NQ0+53+WpscLsoksfjlpCOPVpSLQDKe1IHARFTgJSNgsOfvdBGIEoyDEDdMOc6JzONUuijKsfUTnQI+UsecoJOmRo4xtSloR0sql9FPqhxTn9quMrsJQVL/DNnol+vs8r6RsyLPL|ArPZsEB4BoGL5UX0BTb9vdyOuSt+cMf7msRcEIUUX6O/oOoCdQmxYIVsaLCgq9IkK6mP4O1oAiu6R8gfdsyHgIQSDtKiv7DRAwrI/b4UD4ka+BVVhJXkX0uYlqmA8yCrcmdM2zwp7c/v0qeEr0LnJhrm2tPwAEhLdWEDJskNsH/dQsVkWxf77Vj1OtLQjl+6Ir0RYzdEXlBxNGBv6Y0x2khQcbcTbVfcMFJ3zoGcCEU75UrDLS/ph73t/BM2dICakqL+ymlKiqMm/+K77B4pdYbJ2SpiUdydv4wUzNLPsx2mhuh8KhuyEo1DI8Fzjqd3TiuXiIZISyxaXJ3aV9NdxdYVkZ4j5iLpYdL7GqAxBtdLLK/J6KtqzW+3rwM3L7alxbXHVdcgoxVZanunisaNWPsn4V5BUoL8halT3/smwZM="
	//content := "BDUuuPClIlUKDJdpHtNBIU6u5JTetrVG53AfKDSrhah9Q0QZWAj3K5pZF8G/HFtzj/KvbrHfP/gnHri0L7L91HaKYYc1vqy/q8Z69v7MEiIWL2LeLOsc2b0cHmnt7Qey5aZzYVbZJJNhus2gvhahGwOPpPL50JFC8IqAlU4+E/kUBgx+RgzAIkLLs8Se|k1WOt2Eb+xkxSobw/1DaLblguznhnsk1ga87sfqrrPATRuTyszzdVrRtCUuUHRKJ+vQwlZAR1ypkBC1vPMdt8zccI/CeNof+4Ap23enbQwTJQ4KRvij4kbJd6ycY97B+vHnI+oSnwfmjK0EWNUeRrCr2Uau7yxmzlFwJHprbgPZpHVuUjAzMIXNixOfxSTdc9dL+j3/tR7+yookvd9W3hNm96XZFPTheI0FqUKkbKWUUcVs4lC1/yEgjwXFcD4NM"

	rs, err := DecryptBody(&Decrypts{content, priKey})
	if err != nil {
		t.Log(err)
		return
	}

	t.Log(string(rs))
}

func TestEncryptBody(t *testing.T) {
	pukKey := "04a702106cf530dc981e44cd515b394747cfd6bb059247696b188b25281ea4278fe7c6e34a83680110eec71becd31f5db14abc671e5d8e67ce7ca3c6b3adc86674"
	content := `{"name":"zhangxx","phoneNo":"137xxxxxxxx"}`
	xx, err := Encrypt(pukKey, content)
	t.Log(xx, err)
	//签名
	rs, err := EncryptBody(&Encrypts{content, pukKey})

	fmt.Println(rs, err)
}
