gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/sm2/util_test.go (about) 1 // Copyright (c) 2022 zhaochun 2 // core-gm is licensed under Mulan PSL v2. 3 // You can use this software according to the terms and conditions of the Mulan PSL v2. 4 // You may obtain a copy of Mulan PSL v2 at: 5 // http://license.coscl.org.cn/MulanPSL2 6 // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 7 // See the Mulan PSL v2 for more details. 8 9 package sm2 10 11 import ( 12 "crypto/elliptic" 13 "crypto/rand" 14 "encoding/hex" 15 "fmt" 16 "math/big" 17 "reflect" 18 "testing" 19 ) 20 21 func Test_toBytes(t *testing.T) { 22 type args struct { 23 value string 24 } 25 tests := []struct { 26 name string 27 args args 28 want string 29 }{ 30 // TODO: Add test cases. 31 {"less than 32", args{"d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "00d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, 32 {"equals 32", args{"58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, 33 } 34 for _, tt := range tests { 35 t.Run(tt.name, func(t *testing.T) { 36 v, _ := new(big.Int).SetString(tt.args.value, 16) 37 if got := toBytes(elliptic.P256(), v); !reflect.DeepEqual(hex.EncodeToString(got), tt.want) { 38 t.Errorf("toBytes() = %v, want %v", hex.EncodeToString(got), tt.want) 39 } 40 }) 41 } 42 } 43 44 func Test_getLastBitOfY(t *testing.T) { 45 type args struct { 46 y string 47 } 48 tests := []struct { 49 name string 50 args args 51 want uint 52 }{ 53 // TODO: Add test cases. 54 {"0", args{"d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, 0}, 55 {"1", args{"d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865ff"}, 1}, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 y, _ := new(big.Int).SetString(tt.args.y, 16) 60 if got := getLastBitOfY(y, y); got != tt.want { 61 t.Errorf("getLastBitOfY() = %v, want %v", got, tt.want) 62 } 63 }) 64 } 65 } 66 67 func Test_toPointXY(t *testing.T) { 68 type args struct { 69 bytes string 70 } 71 tests := []struct { 72 name string 73 args args 74 want string 75 }{ 76 // TODO: Add test cases. 77 {"has zero padding", args{"00d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, 78 {"no zero padding", args{"58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 bytes, _ := hex.DecodeString(tt.args.bytes) 83 expectedInt, _ := new(big.Int).SetString(tt.want, 16) 84 if got := toPointXY(bytes); !reflect.DeepEqual(got, expectedInt) { 85 t.Errorf("toPointXY() = %v, want %v", got, expectedInt) 86 } 87 }) 88 } 89 } 90 91 func TestSm2KeyHex(t *testing.T) { 92 priKey, err := GenerateKey(rand.Reader) 93 if err != nil { 94 t.Fatal(err) 95 } 96 pubKey := &priKey.PublicKey 97 98 priKeyStr := WriteSm2PrivToHex(priKey) 99 fmt.Printf("priKeyStr : %s\n", priKeyStr) 100 101 pubKeyStr := WriteSm2PubToHex(pubKey) 102 fmt.Printf("pubKeyStr : %s\n", pubKeyStr) 103 104 priKeyA, err := ReadSm2PrivFromHex(priKeyStr) 105 if err != nil { 106 t.Fatal(err) 107 } 108 if reflect.DeepEqual(priKey, priKeyA) { 109 fmt.Println("ReadSm2PrivFromHex OK") 110 } else { 111 fmt.Println("ReadSm2PrivFromHex NG") 112 } 113 114 pubKeyA, err := ReadSm2PubFromHex(pubKeyStr) 115 if err != nil { 116 t.Fatal(err) 117 } 118 if reflect.DeepEqual(pubKey, pubKeyA) { 119 fmt.Println("ReadSm2PubFromHex OK") 120 } else { 121 fmt.Println("ReadSm2PubFromHex NG") 122 } 123 }