package sm2

import (
	"fmt"
	"testing"
)

const (
	SELF_PRI = "cde43bc462e2ffad2d6fda08e975bee59d673c33591fcbf1aa181d4cb9d89314"

	SELF_PUB = "04B5A21130C6BCE5B161C33B182D4E9E84F366C28660F515AC1DB3278E2B55C5CB62765A56144049997D6AE5F75D70750EA1C621D3D9599558E8847B55A90856B2"

	SOA_PUB = "0416445bc16cbf42e47002ad9fe7c7af67d902b48be1eb69b98f6a006b0918630e1127f5f2fff83b2ecb30fc7fd72c34c33f37c7c355dffde3589f66800f0036ca"
)

func TestGenerateSM2KeyPair(t *testing.T) {
	// Generate a new SM2 key pair
	privateKey, publicKey := GenerateKey()

	// Print the private and public keys
	fmt.Printf("Private Key: %s\n", privateKey)
	fmt.Printf("Public Key: %s\n", publicKey)

	data := "{\"name\":\"张三\",\"sex\":1,\"is_human\":true}"
	en, err := SM2Encrypt(data, publicKey)
	if err != nil {
		panic(err)
	}

	decrypt, err := SM2Decrypt(en, publicKey, privateKey)
	if err != nil {
		panic(err)
	}
	t.Log(decrypt)

}

func TestSM2Encrypt(t *testing.T) {
	t.Log(encrypt())
}

func TestSM2Decrypt(t *testing.T) {
	en := encrypt()
	decrypt, err := SM2Decrypt(en, SELF_PUB, SELF_PRI)
	if err != nil {
		panic(err)
	}
	t.Log(decrypt)
}

func TestSM2Decrypt2(t *testing.T) {
	en := "BOZcpvnFxHrqyzqpq6w3aiTv0KZ5VvLMQnT9sZDxBpMY2hjOrDmwiRau+5QB6iV8ghNZZQKivzUU3I6sfIBSqvRtmtK8nGepQsqi+OCq3uNyMnzWOXkW2Ft9kbt4n4AusCeLLJJGTwS0cmoURiWpdrpr+sNk5wW3+crw9Yo4Xv5UIhyFYtYS8vyEb57Z|YNtjSKBan8/cAxfTgirq7EqLxSdFkEpht/BDp2wW56BCg+4eTmUZo7/YDtUe2MIoAJpSRoI8RwqzrZ3B1V5rOxGopycEwZ/nrrcUkGN6pNE="
	decrypt, err := SM2Decrypt(en, SELF_PUB, SELF_PRI)
	if err != nil {
		panic(err)
	}
	t.Log(decrypt)
}

func encrypt() string {
	data := "funmallGetOrderList.json?aid=AppFunmallTestSM&cmbKeyAlias=CO_PUB_KEY_SM2&date=20241106105348&encryptBody=TURRNU5HWXpPV015WXpNMU1HRTVNMlV4TkRnNE1XTTNPVGhsWlRjMk1qUXhNamhoWkRVeU5UQmtPVFEyWVRjMk56VXpNemt3TkdVeE5XVTVObUUxTmpFNU1XWXdPVFJpT1RoaFlURmhNemMzTWpNMVlqTTVaRGd3WmpZNFl6ZzVZVE15TUdKaFpESXdaVEJsWlRnNE1qTTBNelZrWlRBNE5tVTVNR0poTWpZeE5qUmhOakprWm1ZeFpHRTBaV0UyTmpJNU1ESmlObUUwTWpGa056UTRZVGhtTkRkbE4yUTJNek15TVRVellUSXlOREV5WWpKaE9HRTBNVEUwWXpoaFltVTROalpoTVdZM00yRmlNall4WW1abVptRXpNakExTmpVMU0yWmpZamN4WVRBellqVXdZakl3TXpabU4ySTVNR1V3WlRCbFl6VmpNVE0wTURFelkyTXpaRGd4TVdZeU9HTXhOemszTVRrM1pqVmpZell6TjJVeFltUTFOakEzTnpsbE5tVTROemxoWlRsaE0yUTRNREUxWVdGbU5qWTJORFJrT0ROalptVTJaamsy|lGeghQQEGiVwd0oINhKJDvEIhI6Qbvh41G5x9CseXApghhHGJ98TOd2B8UUCo6uJ4bvmDhl6lMhq1Uy2FvZfkF1WQ8cG2H2EwDLUtsF7CEY=&keyAlias=SM2_CMBLIFE&mid=MerchFunmallTestSM&random=ziy1gctmame68wz4hdr3xc0vmvmhnf35"

	en, err := SM2Encrypt(data, SELF_PUB)
	if err != nil {
		panic(err)
	}
	return en
}
