sm2

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sm2 exposes an SM2 implementation backed by github.com/emmansun/gmsm.

Index

Constants

View Source
const (
	KeyType        = crpt.SM2
	PublicKeySize  = 65 // 0x04 || X || Y (32 bytes each)
	PrivateKeySize = 32
	AddressSize    = PublicKeySize
)

Variables

View Source
var (
	KeyTypeByte            = byte(KeyType)
	ErrWrongPublicKeySize  = fmt.Errorf("%w, should be %d bytes", crpt.ErrWrongPublicKeySize, PublicKeySize)
	ErrWrongPrivateKeySize = fmt.Errorf("%w, should be %d bytes", crpt.ErrWrongPrivateKeySize, PrivateKeySize)
	ErrInvalidSignature    = errors.New("sm2: invalid signature encoding")
	DefaultSignerOpts      = NewSignerOpts(true, nil, crpt.SM3)
)

Functions

func New

func New(opts *SignerOpts) (*sm2Crpt, error)

New creates an SM2 Crpt with the provided default signer options.

func NewWithCryptoSignerOpts

func NewWithCryptoSignerOpts(opts crpt.SignerOpts) (*sm2Crpt, error)

NewWithCryptoSignerOpts creates an SM2 Crpt from generic crypto.SignerOpts.

Types

type Address

type Address = crpt.Address

Address matches the global crpt.Address alias for clarity.

type PrivateKey

type PrivateKey struct {
	*crpt.BasePrivateKey
	// contains filtered or unexported fields
}

PrivateKey wraps the gmsm private key implementation.

func NewPrivateKey

func NewPrivateKey(b []byte, opts crpt.SignerOpts) (*PrivateKey, error)

NewPrivateKey constructs a PrivateKey from raw scalar bytes.

func (PrivateKey) Bytes

func (priv PrivateKey) Bytes() []byte

func (PrivateKey) Equal

func (priv PrivateKey) Equal(o crpt.PrivateKey) bool

func (PrivateKey) Public

func (priv PrivateKey) Public() crpt.PublicKey

func (PrivateKey) SignDigest

func (priv PrivateKey) SignDigest(digest []byte, rand io.Reader, opts crpt.SignerOpts,
) (crpt.Signature, error)

func (PrivateKey) SignMessage

func (priv PrivateKey) SignMessage(message []byte, rand io.Reader, opts crpt.SignerOpts,
) (crpt.Signature, error)

type PublicKey

type PublicKey struct {
	*crpt.BasePublicKey
	// contains filtered or unexported fields
}

PublicKey wraps an uncompressed SM2 public key.

func NewPublicKey

func NewPublicKey(b []byte, opts crpt.SignerOpts) (*PublicKey, error)

NewPublicKey constructs a PublicKey from its uncompressed bytes.

func (PublicKey) Address

func (pub PublicKey) Address() Address

Address returns the transaction address backing this key.

func (PublicKey) Bytes

func (pub PublicKey) Bytes() []byte

Bytes returns the uncompressed public key bytes.

func (PublicKey) Equal

func (pub PublicKey) Equal(o crpt.PublicKey) bool

Equal runs in constant time over the compressed bytes.

func (PublicKey) VerifyDigest

func (pub PublicKey) VerifyDigest(digest []byte, sig crpt.Signature, opts crpt.SignerOpts,
) (bool, error)

func (PublicKey) VerifyMessage

func (pub PublicKey) VerifyMessage(message []byte, sig crpt.Signature, opts crpt.SignerOpts,
) (bool, error)

type SignerOpts

type SignerOpts struct {
	*gsm2.SM2SignerOption
	// contains filtered or unexported fields
}

SignerOpts controls how SM2 operations are performed.

func NewSignerOpts

func NewSignerOpts(forceGMSign bool, uid []byte, hash crpt.Hash) *SignerOpts

func (*SignerOpts) Clone

func (o *SignerOpts) Clone() crpt.SignerOptsExtended

Clone creates a copy of the SignerOpts.

func (*SignerOpts) HashFunc

func (o *SignerOpts) HashFunc() crpt.Hash

HashFunc implements crypto.SignerOpts.

func (*SignerOpts) SetHash

func (o *SignerOpts) SetHash(hash crpt.Hash)

SetHash sets the hash function for the SignerOpts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL