57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package sm2
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
SELF_PRI = "47388B05A25F7EF05C6B663583AEB2F8C0CB87A4E77F3AB55B58AC51E60CDB31"
|
|
|
|
SELF_PUB = "0475904CB1A2F66077E964174995B319B0F1FA1DC593D57D5E7351B5E56F4E4E2E114D9A4234F66D799055566231170467EDA908E124D91FF826589A7CCE7450DA"
|
|
)
|
|
|
|
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 := "MDRmNGM0YTEyMGZlNjZhZDAyNTZhYjQ1MDQyOGM0MmIzMDA1MmIwN2NkNzExYjI2YWM1Mjg1MTMzOWU1NmNjNWIzYTRkMThhZWU4N2VjNmRiMTc5OTExOGVlYzg0YzQ5ZDY0N2EyZDVmMTY3ZTBiZmU3MTAxMDMzNjFlYjgwNGQ1NWU3Mjg3YjlmZDk2OTIwMzRhMWQ4NmNkMGRlNTI0ODE0NDAxNjJjMGMzNmExYmY4YjdlZDJlM2E5OWY3ZjQ3MjI4NWMxZTZkNTc4NWI0ZTgyNWNiNGU2"
|
|
decrypt, err := SM2Decrypt(en, SELF_PUB, SELF_PRI)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
t.Log(decrypt)
|
|
}
|
|
|
|
func encrypt() string {
|
|
data := "{\n \"pcode\": \"110000000000\",\n \"page\": 1,\n \"limit\": 10\n}"
|
|
en, err := SM2Encrypt(data, SELF_PUB)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return en
|
|
}
|