sm2

package
v0.0.0-...-f6092e9 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SM2使用256位曲线,私钥为32字节
	PrivateKeySize = 32
	// SM2公钥为65字节(压缩格式为33字节)
	PublicKeySize = 65
	// 签名结果为64字节(r和s各32字节)
	SignatureSize = 64
)

密钥大小(字节)

Variables

View Source
var (
	ErrInvalidPrivateKey  = errors.New("sm2: 无效的私钥")
	ErrInvalidPublicKey   = errors.New("sm2: 无效的公钥")
	ErrInvalidSignature   = errors.New("sm2: 无效的签名")
	ErrInvalidCiphertext  = errors.New("sm2: 无效的密文")
	ErrDecryptionFailed   = errors.New("sm2: 解密失败")
	ErrVerificationFailed = errors.New("sm2: 验证失败")
)

错误定义

Functions

func P256

func P256() elliptic.Curve

P256 返回SM2推荐曲线参数

Types

type PrivateKey

type PrivateKey struct {
	D         *big.Int // 私钥值
	PublicKey          // 内嵌公钥
}

PrivateKey 表示SM2私钥

func (*PrivateKey) EncodePrivateKey

func (priv *PrivateKey) EncodePrivateKey() []byte

EncodePrivateKey 将私钥编码为字节流

type PublicKey

type PublicKey struct {
	X, Y *big.Int // 公钥坐标
}

PublicKey 表示SM2公钥

func (*PublicKey) EncodePublicKey

func (pub *PublicKey) EncodePublicKey() []byte

EncodePublicKey 将公钥编码为字节流

type SM2

type SM2 struct {
	// contains filtered or unexported fields
}

SM2 封装SM2算法功能

func New

func New() *SM2

New 创建一个新的SM2实例

func (*SM2) DecodePrivateKey

func (s *SM2) DecodePrivateKey(data []byte) (*PrivateKey, error)

DecodePrivateKey 从字节流解码私钥

func (*SM2) DecodePublicKey

func (s *SM2) DecodePublicKey(data []byte) (*PublicKey, error)

DecodePublicKey 从字节流解码公钥

func (*SM2) Decrypt

func (s *SM2) Decrypt(priv *PrivateKey, ciphertext []byte) ([]byte, error)

Decrypt 使用SM2算法解密密文

func (*SM2) Encrypt

func (s *SM2) Encrypt(pub *PublicKey, plaintext []byte, random io.Reader) ([]byte, error)

Encrypt 使用SM2算法加密消息

func (*SM2) GenerateKey

func (s *SM2) GenerateKey(random io.Reader) (*PrivateKey, error)

GenerateKey 生成SM2密钥对

func (*SM2) Sign

func (s *SM2) Sign(priv *PrivateKey, digest []byte) ([]byte, error)

Sign 使用SM2算法签名消息

func (*SM2) SignWithId

func (s *SM2) SignWithId(priv *PrivateKey, msg []byte, uid []byte) ([]byte, error)

SignWithId 使用SM2算法和用户标识进行数字签名

func (*SM2) Verify

func (s *SM2) Verify(pub *PublicKey, digest []byte, signature []byte) bool

Verify 使用SM2算法验证签名

func (*SM2) VerifyWithId

func (s *SM2) VerifyWithId(pub *PublicKey, msg []byte, signature []byte, uid []byte) bool

VerifyWithId 使用SM2算法和用户标识验证数字签名

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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