github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/sm2/util_test.go (about)

     1  // Copyright 2022 s1ren@github.com/hxx258456.
     2  
     3  package sm2
     4  
     5  import (
     6  	"crypto/elliptic"
     7  	"crypto/rand"
     8  	"encoding/hex"
     9  	"fmt"
    10  	"math/big"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  func Test_toBytes(t *testing.T) {
    16  	type args struct {
    17  		value string
    18  	}
    19  	tests := []struct {
    20  		name string
    21  		args args
    22  		want string
    23  	}{
    24  		// TODO: Add test cases.
    25  		{"less than 32", args{"d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "00d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"},
    26  		{"equals 32", args{"58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"},
    27  	}
    28  	for _, tt := range tests {
    29  		t.Run(tt.name, func(t *testing.T) {
    30  			v, _ := new(big.Int).SetString(tt.args.value, 16)
    31  			if got := toBytes(elliptic.P256(), v); !reflect.DeepEqual(hex.EncodeToString(got), tt.want) {
    32  				t.Errorf("toBytes() = %v, want %v", hex.EncodeToString(got), tt.want)
    33  			}
    34  		})
    35  	}
    36  }
    37  
    38  func Test_getLastBitOfY(t *testing.T) {
    39  	type args struct {
    40  		y string
    41  	}
    42  	tests := []struct {
    43  		name string
    44  		args args
    45  		want uint
    46  	}{
    47  		// TODO: Add test cases.
    48  		{"0", args{"d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, 0},
    49  		{"1", args{"d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865ff"}, 1},
    50  	}
    51  	for _, tt := range tests {
    52  		t.Run(tt.name, func(t *testing.T) {
    53  			y, _ := new(big.Int).SetString(tt.args.y, 16)
    54  			if got := getLastBitOfY(y, y); got != tt.want {
    55  				t.Errorf("getLastBitOfY() = %v, want %v", got, tt.want)
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func Test_toPointXY(t *testing.T) {
    62  	type args struct {
    63  		bytes string
    64  	}
    65  	tests := []struct {
    66  		name string
    67  		args args
    68  		want string
    69  	}{
    70  		// TODO: Add test cases.
    71  		{"has zero padding", args{"00d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"},
    72  		{"no zero padding", args{"58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"}, "58d20d27d0632957f8028c1e024f6b02edf23102a566c932ae8bd613a8e865fe"},
    73  	}
    74  	for _, tt := range tests {
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			bytes, _ := hex.DecodeString(tt.args.bytes)
    77  			expectedInt, _ := new(big.Int).SetString(tt.want, 16)
    78  			if got := toPointXY(bytes); !reflect.DeepEqual(got, expectedInt) {
    79  				t.Errorf("toPointXY() = %v, want %v", got, expectedInt)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestSm2KeyHex(t *testing.T) {
    86  	priKey, err := GenerateKey(rand.Reader)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	pubKey := &priKey.PublicKey
    91  
    92  	priKeyStr := WriteSm2PrivToHex(priKey)
    93  	fmt.Printf("priKeyStr : %s\n", priKeyStr)
    94  
    95  	pubKeyStr := WriteSm2PubToHex(pubKey)
    96  	fmt.Printf("pubKeyStr : %s\n", pubKeyStr)
    97  
    98  	priKeyA, err := ReadSm2PrivFromHex(priKeyStr)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	if reflect.DeepEqual(priKey, priKeyA) {
   103  		fmt.Println("ReadSm2PrivFromHex OK")
   104  	} else {
   105  		fmt.Println("ReadSm2PrivFromHex NG")
   106  	}
   107  
   108  	pubKeyA, err := ReadSm2PubFromHex(pubKeyStr)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if reflect.DeepEqual(pubKey, pubKeyA) {
   113  		fmt.Println("ReadSm2PubFromHex OK")
   114  	} else {
   115  		fmt.Println("ReadSm2PubFromHex NG")
   116  	}
   117  }