gosm2

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DecryptFormat

type DecryptFormat = EncryptFormat

DecryptFormat 定义解密格式类型

type DecryptResult

type DecryptResult struct {
	*Result
}

DecryptResult 包装解密结果,支持链式调用转换格式

type EncryptFormat

type EncryptFormat int

EncryptFormat 定义加密格式类型

const (
	// C1C3C2 格式
	C1C3C2 EncryptFormat = iota
	// C1C2C3 格式
	C1C2C3
	// C1C3C2Compressed 压缩格式
	C1C3C2Compressed
	// C1C2C3Compressed 压缩格式
	C1C2C3Compressed
)

type EncryptResult

type EncryptResult struct {
	*Result
}

EncryptResult 包装加密结果,支持链式调用转换格式

type KeyPair

type KeyPair struct {
	PrivateKeyPEM string
	PublicKeyPEM  string
	PrivateKeyHex string
	PublicKeyHex  string
}

KeyPair 结构体包含PEM和HEX格式的密钥对

type Result

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

Result 包装通用结果数据

func (*Result) ToBase64

func (r *Result) ToBase64() (string, error)

ToBase64 将结果转换为Base64编码字符串

func (*Result) ToBytes

func (r *Result) ToBytes() ([]byte, error)

ToBytes 返回原始字节数据

func (*Result) ToHex

func (r *Result) ToHex() (string, error)

ToHex 将结果转换为十六进制字符串

type SM2

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

SM2 封装了SM2算法相关的操作

func NewSM2

func NewSM2(opts ...SM2Option) *SM2

NewSM2 创建一个新的SM2实例

func (*SM2) Decrypt

func (s *SM2) Decrypt(privateKey *sm2.PrivateKey, ciphertextByte []byte, format DecryptFormat) *DecryptResult

Decrypt 使用SM2私钥解密数据,支持指定格式

func (*SM2) DecryptFromBase64

func (s *SM2) DecryptFromBase64(privateKey *sm2.PrivateKey, base64Data string, format DecryptFormat) *DecryptResult

DecryptFromBase64 使用SM2私钥解密Base64编码字符串数据,支持指定格式

func (*SM2) DecryptFromBytes

func (s *SM2) DecryptFromBytes(privateKey *sm2.PrivateKey, byteData []byte, format DecryptFormat) *DecryptResult

DecryptFromBytes 使用SM2私钥解密字节数据,支持指定格式

func (*SM2) DecryptFromHex

func (s *SM2) DecryptFromHex(privateKey *sm2.PrivateKey, hexData string, format DecryptFormat) *DecryptResult

DecryptFromHex 使用SM2私钥解密十六进制字符串数据,支持指定格式

func (*SM2) Encrypt

func (s *SM2) Encrypt(publicKey *sm2.PublicKey, plainTextByte []byte, format EncryptFormat) *EncryptResult

Encrypt 使用SM2公钥加密数据,支持指定格式

func (*SM2) GenerateKeyPair

func (s *SM2) GenerateKeyPair() (*KeyPair, error)

GenerateKeyPair 生成SM2密钥对

func (*SM2) ParsePrivateKeyFromHex

func (s *SM2) ParsePrivateKeyFromHex(hexKey string) (*sm2.PrivateKey, error)

ParsePrivateKeyFromHex 从十六进制字符串解析私钥

func (*SM2) ParsePrivateKeyFromPem

func (s *SM2) ParsePrivateKeyFromPem(pemKey string) (*sm2.PrivateKey, error)

ParsePrivateKeyFromPem 从PEM格式字符串解析私钥

func (*SM2) ParsePublicKeyFromHex

func (s *SM2) ParsePublicKeyFromHex(hexKey string) (*sm2.PublicKey, error)

ParsePublicKeyFromHex 从十六进制字符串解析公钥

func (*SM2) ParsePublicKeyFromPem

func (s *SM2) ParsePublicKeyFromPem(pemKey string) (*sm2.PublicKey, error)

ParsePublicKeyFromPem 从PEM格式字符串解析公钥

func (*SM2) PrivateKeyToHex

func (s *SM2) PrivateKeyToHex(privateKey *sm2.PrivateKey) (string, error)

PrivateKeyToHex 将私钥转换为十六进制字符串

func (*SM2) PrivateKeyToPem

func (s *SM2) PrivateKeyToPem(privateKey *sm2.PrivateKey) (string, error)

PrivateKeyToPem 将私钥转换为PEM格式字符串

func (*SM2) PublicKeyToHex

func (s *SM2) PublicKeyToHex(publicKey *sm2.PublicKey) (string, error)

PublicKeyToHex 将公钥转换为十六进制字符串

func (*SM2) PublicKeyToPem

func (s *SM2) PublicKeyToPem(publicKey *sm2.PublicKey) (string, error)

PublicKeyToPem 将公钥转换为PEM格式字符串

func (*SM2) SaveKeyPair

func (s *SM2) SaveKeyPair(keyPair *KeyPair, privateFile, pubFile string) error

SaveKeyPair 保存密钥对到文件(使用keyPair中的内容,可能已去除header)

func (*SM2) SaveKeyPairRaw

func (s *SM2) SaveKeyPairRaw(privateKeyPem, publicKeyPem, privateFile, pubFile string) error

SaveKeyPairRaw 保存原始密钥对到文件(保留完整PEM格式)

func (*SM2) Sign

func (s *SM2) Sign(privateKey *sm2.PrivateKey, dataByte []byte) *SignResult

Sign 使用SM2私钥对数据进行签名

func (*SM2) VerifyFromBase64

func (s *SM2) VerifyFromBase64(publicKey *sm2.PublicKey, dataByte []byte, base64Signature string) bool

VerifyFromBase64 使用SM2公钥验证Base64编码字符串签名

func (*SM2) VerifyFromBytes

func (s *SM2) VerifyFromBytes(publicKey *sm2.PublicKey, dataByte, signatureByte []byte) bool

VerifyFromBytes 使用SM2公钥验证签名

func (*SM2) VerifyFromHex

func (s *SM2) VerifyFromHex(publicKey *sm2.PublicKey, dataByte []byte, hexSignature string) bool

VerifyFromHex 使用SM2公钥验证十六进制字符串签名

type SM2Option

type SM2Option func(*sm2Options)

SM2Option 是用于配置SM2的函数类型

func WithRand

func WithRand(reader io.Reader) SM2Option

WithRand 设置随机数生成器

func WithSave

func WithSave(privateFile, pubFile string) SM2Option

WithSave 设置是否自动保存密钥到文件

func WithStripHeader

func WithStripHeader(strip bool) SM2Option

WithStripHeader 设置是否去除公钥和私钥PEM格式的头部和尾部

func WithUnescapeHTML

func WithUnescapeHTML(unescape bool) SM2Option

WithUnescapeHTML 设置是否进行HTML转义处理

type SignResult

type SignResult struct {
	*Result
}

SignResult 包装签名结果,支持链式调用转换格式

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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