PaymentCenter/app/utils/encrypt/sm2/sm2_test.go

57 lines
1.2 KiB
Go

package sm2
import (
"fmt"
"testing"
)
const (
SELF_PRI = "BD13D44C74422F80ADDF54AD2DE2888C4092AF6F61E41FABADA6C47F17574A41"
SELF_PUB = "04F26C6CDA5E58153999FDF3023259F16EDE0A556C39FCF4BC97C04A10A8A6CF1D52297B53DB4DD72FEEF8221394C8478E6424F4E84E4A6E2784C1A4A1C99F2DC2"
)
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 encrypt() string {
data := "{\"order_no\":5476377146882523228,\"order_type\":1,\"out_tread_no\":\"asdadasdas\",\"amount\":1,\"desc\":\"abc\",\"status\":2,\"create_time\":\"2024-08-08 14:47:35\"}"
en, err := SM2Encrypt(data, SELF_PUB)
if err != nil {
panic(err)
}
return en
}