github.com/chain5j/chain5j-pkg@v1.0.7/crypto/signature/gmsm/serialize.go (about)

     1  // Package gmsm
     2  //
     3  // @author: xwc1125
     4  package gmsm
     5  
     6  import (
     7  	"crypto/ecdsa"
     8  	"errors"
     9  	"fmt"
    10  
    11  	"github.com/tjfoc/gmsm/sm2"
    12  	"github.com/tjfoc/gmsm/x509"
    13  )
    14  
    15  const (
    16  	PubKeyBytesLenCompressed   = 33
    17  	PubKeyBytesLenUncompressed = 65
    18  )
    19  const (
    20  	pubkeyCompressed   byte = 0x2 // y_bit + x coord
    21  	pubkeyUncompressed byte = 0x4 // x coord + y coord
    22  )
    23  
    24  func paddedAppend(size uint, dst, src []byte) []byte {
    25  	for i := 0; i < int(size)-len(src); i++ {
    26  		dst = append(dst, 0)
    27  	}
    28  	return append(dst, src...)
    29  }
    30  
    31  func MarshalPrivateKey(key *ecdsa.PrivateKey) ([]byte, error) {
    32  	return x509.MarshalSm2PrivateKey(FromECDSA(key), nil)
    33  }
    34  
    35  func UnmarshalPrivateKey(keyBytes []byte) (*sm2.PrivateKey, error) {
    36  	return x509.ParsePKCS8UnecryptedPrivateKey(keyBytes)
    37  }
    38  
    39  func MarshalPublicKey(pub *ecdsa.PublicKey) ([]byte, error) {
    40  	return sm2.Compress(FromECDSAPubKey(pub)), nil
    41  }
    42  
    43  func UnmarshalPublicKey(data []byte) (pub *ecdsa.PublicKey, err error) {
    44  	defer func() {
    45  		if r := recover(); r != nil {
    46  			switch p := r.(type) {
    47  			case error:
    48  				err = p
    49  			default:
    50  				err = fmt.Errorf("%v", r)
    51  			}
    52  		}
    53  	}()
    54  	publicKey := sm2.Decompress(data)
    55  	if publicKey == nil {
    56  		return nil, errors.New("invalid public key")
    57  	}
    58  	return &ecdsa.PublicKey{X: publicKey.X, Y: publicKey.Y, Curve: Curve()}, nil
    59  }
    60  
    61  func MarshalPrivateX509(prv *ecdsa.PrivateKey) ([]byte, error) {
    62  	return x509.MarshalSm2PrivateKey(FromECDSA(prv), nil)
    63  }
    64  
    65  func UnmarshalPrivateX509(data []byte) (*ecdsa.PrivateKey, error) {
    66  	privateKey, err := x509.ParseSm2PrivateKey(data)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return ToECDSA(privateKey), nil
    71  }
    72  
    73  // =================x509=================
    74  
    75  func MarshalPrivateKeyX509(key *ecdsa.PrivateKey) ([]byte, error) {
    76  	return x509.MarshalSm2UnecryptedPrivateKey(FromECDSA(key))
    77  }
    78  
    79  func UnmarshalPrivateKeyX509(keyBytes []byte) (*sm2.PrivateKey, error) {
    80  	return x509.ParseSm2PrivateKey(keyBytes)
    81  }
    82  
    83  func MarshalPublicKeyX509(pub *ecdsa.PublicKey) ([]byte, error) {
    84  	return x509.MarshalSm2PublicKey(FromECDSAPubKey(pub))
    85  }
    86  
    87  func UnmarshalPublicKeyX509(data []byte) (*ecdsa.PublicKey, error) {
    88  	publicKey, err := x509.ParseSm2PublicKey(data)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	return ToECDSAPubKey(publicKey), nil
    93  }