MCPcopy
hub / github.com/tjfoc/gmsm / TestSm2

Function TestSm2

sm2/sm2_test.go:28–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestSm2(t *testing.T) {
29 priv, err := GenerateKey(rand.Reader) // 生成密钥对
30 fmt.Println(priv)
31 if err != nil {
32 t.Fatal(err)
33 }
34 fmt.Printf("%v\n", priv.Curve.IsOnCurve(priv.X, priv.Y)) // 验证是否为sm2的曲线
35 pub := &priv.PublicKey
36 msg := []byte("123456")
37 d0, err := pub.EncryptAsn1(msg, rand.Reader)
38 if err != nil {
39 fmt.Printf("Error: failed to encrypt %s: %v\n", msg, err)
40 return
41 }
42 // fmt.Printf("Cipher text = %v\n", d0)
43 d1, err := priv.DecryptAsn1(d0)
44 if err != nil {
45 fmt.Printf("Error: failed to decrypt: %v\n", err)
46 }
47 fmt.Printf("clear text = %s\n", d1)
48 d2, err :=Encrypt(pub,msg, rand.Reader,C1C2C3)
49 if err != nil {
50 fmt.Printf("Error: failed to encrypt %s: %v\n", msg, err)
51 return
52 }
53 // fmt.Printf("Cipher text = %v\n", d0)
54 d3, err := Decrypt(priv,d2,C1C2C3)
55 if err != nil {
56 fmt.Printf("Error: failed to decrypt: %v\n", err)
57 }
58 fmt.Printf("clear text = %s\n", d3)
59 msg, _ = ioutil.ReadFile("ifile") // 从文件读取数据
60 sign, err := priv.Sign(rand.Reader, msg, nil) // 签名
61 if err != nil {
62 t.Fatal(err)
63 }
64
65 err = ioutil.WriteFile("TestResult", sign, os.FileMode(0644))
66 if err != nil {
67 t.Fatal(err)
68 }
69 signdata, _ := ioutil.ReadFile("TestResult")
70 ok := priv.Verify(msg, signdata) // 密钥验证
71 if ok != true {
72 fmt.Printf("Verify error\n")
73 } else {
74 fmt.Printf("Verify ok\n")
75 }
76 pubKey := priv.PublicKey
77 ok = pubKey.Verify(msg, signdata) // 公钥验证
78 if ok != true {
79 fmt.Printf("Verify error\n")
80 } else {
81 fmt.Printf("Verify ok\n")
82 }
83
84}
85

Callers

nothing calls this directly

Calls 8

GenerateKeyFunction · 0.85
EncryptFunction · 0.85
DecryptFunction · 0.85
IsOnCurveMethod · 0.80
EncryptAsn1Method · 0.80
DecryptAsn1Method · 0.80
SignMethod · 0.80
VerifyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…