package cmbv2

import (
	"testing"
)

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

	n, err := NewCmb(priKey, pukKey)
	if err != nil {
		t.Errorf("NewCmb() error = %v", err)
		return
	}

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

	got, err := n.Encrypt([]byte(content))
	if err != nil {
		t.Errorf("Encrypt() error = %v", err)
		return
	}

	t.Log(got)
}

func TestCmb_Decrypt(t *testing.T) {
	//priKey := "8d39ff3d2559258c163f4510f082727f51531e1953ab203d5ab1ea4a6d94fd73"
	priKey := "f6a8d2f412e289686aba6a0f33cad1a64367d0ba012046ee0fbbefd3ffd675bd"
	//pukKey := "04d827a7dbaaa358ce45b8c7794a7f54819f5c175005a702370e47f135ef6f5f9732758b1474f218419fe9e87f90c28c3b05f08254c651db27df35fae67b77b2e4"
	pukKey := "043b2fade30067b6bd8e61b42771b1e953116fc5a0f9ed6939fceb9254b8d7d6989902c913642c3c68c42a2b56364512675ea0b517dd4469e73b73c888a2f4e8e3"

	n, err := NewCmb(priKey, pukKey)
	if err != nil {
		t.Errorf("NewCmb() error = %v", err)
		return
	}

	//input := "BJAwi/eC8AHtVVbbCFN58lQhJOIhO4b8ua6CS6AKGma4dGGVgedIlX4B5QbZOb58mNLTXA6dcUIQwFQxrc8z05Ckf9UZ6LQKgLxMcnbvdqlXQCLYEqjxhomkn543Nexw9MINybonMseYh8oy9eSEu4WgvjjfkNrkAF52njJXMiDgqKjTz+xlkGNrCx/9|TZgKpHqElSZEuP05q0/UdP6Sju9e45nIWReMjSao9yDghYJXMwW73exZ3tKUQeLI"
	input := "BBwR1zhvi1cp5osiTFuTzE5ei74SYzHA1XLb7TCKh1XAquRbAoGcTYIzMgftTY5Jj6kziilMF5yg3FxxKCn4cnWa5JMYHJ4ifEdpEZP2+W9T773Di0XC8GpZ89k4dzDGlTcYoiIv3ebReyCPve2QF5hx6t2EH1YKbVpoU32MTDzL8Mz6XuhhnwLKLePu|u8s9XJxe8gWCyZlL1mkGdZq428qmEl5qjzzIO4Bn+MjBi+iEeULR4HlyrD9/fPPsdhwjUNdrBgRmDJidc0Eksfo4QCYhaytWrUBvJxAkmPCkGNyiHAFipqfmGM0A0HLoR81zWdEIjChlNYWu4/x3+FFWgMRAvgEk9ggqGWRoqHQTvSXu2FMZGJoTgfUDSAuyuwfpTLKULCFyZmymPubN0Jjg67ZrbGro+/43MnPeZNE1HpAhwGbQaUugJWtk9a/u"

	got2, err := n.Decrypt(input)
	if err != nil {
		t.Errorf("%v", err)
		return
	}

	t.Log(got2)
}

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

	n, err := NewCmb(priKey, pukKey)
	if err != nil {
		t.Errorf("NewCmb() error = %v", err)
		return
	}

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

	got, err := n.Encrypt([]byte(content))
	if err != nil {
		t.Errorf("Encrypt() error = %v", err)
		return
	}

	t.Log(got)

	got2, err := n.Decrypt(got)
	if err != nil {
		t.Errorf("Decrypt() error = %v", err)
		return
	}

	t.Log(got2)
}

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

	n, err := NewCmb(priKey, pukKey)
	if err != nil {
		t.Errorf("NewCmb() error = %v", err)
		return
	}

	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"

	got, err := n.Sign([]byte(content))
	if err != nil {
		t.Errorf("Sign() error = %v", err)
		return
	}

	t.Log(got)
}

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

	n, err := NewCmb(priKey, pukKey)
	if err != nil {
		t.Errorf("NewCmb() error = %v", err)
		return
	}

	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"

	sign := "MEYCIQDGqIJzzhAvXbOS1mnctYsKmCA6M+Dorm+YeYMUncYu6QIhALjazOfd2Doix00lrU2w95uq8SHbPFwxoXXBs56apKjG"

	got2, err := n.Verify(content, sign)
	if err != nil {
		t.Errorf("Verify() error = %v", err)
		return
	}

	t.Log(got2)
}

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

	n, err := NewCmb(priKey, pukKey)
	if err != nil {
		t.Errorf("NewCmb() error = %v", err)
		return
	}

	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"

	got, err := n.Sign([]byte(content))
	if err != nil {
		t.Errorf("Sign() error = %v", err)
		return
	}

	t.Log(got)

	got2, err := n.Verify(content, got)
	if err != nil {
		t.Errorf("Verify() error = %v", err)
		return
	}

	t.Log(got2)
}
