package cmb

import (
	"testing"
)

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 := "9450c673cf801164435b9c164ac1404e87997245bc6f323fde22015875a03f6e"
	content := "BHeko/ZYFzQOJn6Q3y46X1AjNz8Nh5fq1FfMuWebh+TangLnlK5iFqePCst4rjG/FKJInijiKO2Qq18sJULlMEEgri05s+bHHDKM+Y+73crAbCnIhHbZxUjt8A0cq2rKjzkl8bxW33dU18uuiTEAmsAvKvmZgE6zJ1eDyjFWefHEIFJaKCNY2cTQOInt|8UYFZFTkx0DovPhaWCbdBkAqbgGmegT14F5gwXLJ6G1uWdYNvX+i5QWAYUGtd8u9"
	xx, err := Decrypt(priKey, content)
	t.Log(xx, err)
}

func TestEncryptDecrypt(t *testing.T) {
	priKey := "44c33b565308062183ec53e7e9171eeab1a3f5df1f116bceb420f039eab469d4"
	pukKey := "0479c4d66ffc74d5e0ff667d53da72f63f2c091cf4ee5fc3cc53142bc104dd68ffe332cf1151ce07e0cc62d64c9c7c3b5c94b161668d281691cab2424013eadfb2"

	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"

	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"

	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="

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