github.com/turingchain2020/turingchain@v1.1.21/system/crypto/sm2/sm2_test.go (about)

     1  package sm2
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/tjfoc/gmsm/sm2"
    10  )
    11  
    12  func TestAll(t *testing.T) {
    13  	testCrypto(t)
    14  	testFromBytes(t)
    15  	testCryptoCompress(t)
    16  }
    17  
    18  func testFromBytes(t *testing.T) {
    19  	require := require.New(t)
    20  
    21  	c := &Driver{}
    22  
    23  	priv, err := c.GenKey()
    24  	require.Nil(err)
    25  
    26  	priv2, err := c.PrivKeyFromBytes(priv.Bytes())
    27  	require.Nil(err)
    28  	require.Equal(true, priv.Equals(priv2))
    29  
    30  	s1 := string(priv.Bytes())
    31  	s2 := string(priv2.Bytes())
    32  	require.Equal(0, strings.Compare(s1, s2))
    33  
    34  	pub := priv.PubKey()
    35  	require.NotNil(pub)
    36  
    37  	pub2, err := c.PubKeyFromBytes(pub.Bytes())
    38  	require.Nil(err)
    39  	require.Equal(true, pub.Equals(pub2))
    40  
    41  	s1 = string(pub.Bytes())
    42  	s2 = string(pub2.Bytes())
    43  	require.Equal(0, strings.Compare(s1, s2))
    44  
    45  	var msg = []byte("hello world")
    46  	sign1 := priv.Sign(msg)
    47  	sign2 := priv2.Sign(msg)
    48  
    49  	sign3, err := c.SignatureFromBytes(sign1.Bytes())
    50  	require.Nil(err)
    51  	require.Equal(true, sign3.Equals(sign1))
    52  
    53  	require.Equal(true, pub.VerifyBytes(msg, sign1))
    54  	require.Equal(true, pub2.VerifyBytes(msg, sign1))
    55  	require.Equal(true, pub.VerifyBytes(msg, sign2))
    56  	require.Equal(true, pub2.VerifyBytes(msg, sign2))
    57  	require.Equal(true, pub.VerifyBytes(msg, sign3))
    58  	require.Equal(true, pub2.VerifyBytes(msg, sign3))
    59  }
    60  
    61  func testCrypto(t *testing.T) {
    62  	require := require.New(t)
    63  
    64  	c := &Driver{}
    65  
    66  	priv, err := c.GenKey()
    67  	require.Nil(err)
    68  	t.Logf("priv:%X, len:%d", priv.Bytes(), len(priv.Bytes()))
    69  
    70  	pub := priv.PubKey()
    71  	require.NotNil(pub)
    72  	t.Logf("pub:%X, len:%d", pub.Bytes(), len(pub.Bytes()))
    73  
    74  	msg := []byte("hello world")
    75  	signature := priv.Sign(msg)
    76  	t.Logf("sign:%X, len:%d", signature.Bytes(), len(signature.Bytes()))
    77  
    78  	ok := pub.VerifyBytes(msg, signature)
    79  	require.Equal(true, ok)
    80  }
    81  
    82  func testCryptoCompress(t *testing.T) {
    83  	require := require.New(t)
    84  
    85  	c := &Driver{}
    86  
    87  	priv, err := c.GenKey()
    88  	require.Nil(err)
    89  	t.Logf("priv:%X, len:%d", priv.Bytes(), len(priv.Bytes()))
    90  
    91  	pub := priv.PubKey()
    92  	require.NotNil(pub)
    93  	t.Logf("pub:%X, len:%d, string:%s", pub.Bytes(), len(pub.Bytes()), pub.KeyString())
    94  
    95  	pubkey := sm2.Decompress(pub.Bytes())
    96  
    97  	pubbytes := SerializePublicKey(pubkey, true)
    98  	pub2, err := c.PubKeyFromBytes(pubbytes)
    99  	assert.Nil(t, err)
   100  
   101  	pubbytes = SerializePublicKey(pubkey, false)
   102  	_, err = c.PubKeyFromBytes(pubbytes)
   103  	assert.Nil(t, err)
   104  
   105  	msg := []byte("hello world")
   106  	signature := priv.Sign(msg)
   107  	t.Logf("sign:%X, len:%d, string:%s", signature.Bytes(), len(signature.Bytes()), signature.String())
   108  
   109  	ok := pub.VerifyBytes(msg, signature)
   110  	require.Equal(true, ok)
   111  
   112  	assert.True(t, pub2.VerifyBytes(msg, signature))
   113  	assert.Nil(t, c.Validate(msg, pub.Bytes(), signature.Bytes()))
   114  }