sm2

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sm2 实现国密 SM2 椭圆曲线公钥密码算法(GB/T 32918 系列)。

提供数字签名(GB/T 32918.2)与公钥加密(GB/T 32918.4),签名摘要算法为 SM3,默认用户标识为国密推荐值 "1234567812345678"。

曲线运算基于标准库 crypto/elliptic 的通用实现(SM2 推荐曲线参数见 GB/T 32918.5)。该通用实现不是常数时间的,在需要抵御本地时序侧信道的 场景下应改用带常数时间实现的库;对于服务端调用银行/支付网关接口的 典型用法,这一取舍换来的是零外部依赖与可完整审计的代码量。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPrivateKey 私钥不在有效区间 [1, n-2] 内。
	ErrInvalidPrivateKey = errors.New("sm2: 私钥不在有效区间内")
	// ErrInvalidPublicKey 公钥不是曲线上的有效点。
	ErrInvalidPublicKey = errors.New("sm2: 公钥不是曲线上的有效点")
	// ErrVerifyFailed 签名验证不通过。
	ErrVerifyFailed = errors.New("sm2: 验签失败")
	// ErrInvalidCipher 密文格式非法或已损坏。
	ErrInvalidCipher = errors.New("sm2: 密文格式非法")
)
View Source
var DefaultUID = []byte("1234567812345678")

DefaultUID 国密推荐的默认用户标识 ID_A(GB/T 32918.2 附录 A)。 招商银行、多数银行网关均使用该默认值。

Functions

func CheckPublicKey

func CheckPublicKey(pub *PublicKey) error

CheckPublicKey 校验公钥是否为曲线上的有效点。

func Decrypt

func Decrypt(priv *PrivateKey, cipherText []byte) ([]byte, error)

Decrypt 用私钥解密 ASN.1 DER 编码的 C1C3C2 密文(GB/T 32918.4-2016 7.1)。

func DecryptC1C3C2

func DecryptC1C3C2(priv *PrivateKey, data []byte) ([]byte, error)

DecryptC1C3C2 用私钥解密 0x04 || C1 || C3 || C2 拼接格式的密文。

func Encrypt

func Encrypt(pub *PublicKey, plain []byte) ([]byte, error)

Encrypt 用公钥加密明文,返回 C1C3C2 顺序、ASN.1 DER 编码的密文 (GB/T 32918.4-2016 6.1)。

func EncryptC1C3C2

func EncryptC1C3C2(pub *PublicKey, plain []byte) ([]byte, error)

EncryptC1C3C2 用公钥加密明文,返回 0x04 || C1 || C3 || C2 的拼接格式密文 (即未做 ASN.1 编码的裸拼接形式,部分网关使用该格式)。

func MarshalPrivateKeyHex

func MarshalPrivateKeyHex(priv *PrivateKey) string

MarshalPrivateKeyHex 将私钥编码为 64 字符小写 HEX 字符串。

func MarshalPublicKeyBase64

func MarshalPublicKeyBase64(pub *PublicKey) (string, error)

MarshalPublicKeyBase64 将公钥编码为 Base64 的 ASN.1 DER 公钥。

func MarshalPublicKeyDER

func MarshalPublicKeyDER(pub *PublicKey) ([]byte, error)

MarshalPublicKeyDER 将公钥编码为 ASN.1 DER 的 SubjectPublicKeyInfo 结构。

func MarshalPublicKeyUncompressed

func MarshalPublicKeyUncompressed(pub *PublicKey) []byte

MarshalPublicKeyUncompressed 将公钥编码为非压缩点格式(0x04 || X || Y)。

func P256

func P256() elliptic.Curve

P256 返回 SM2 推荐椭圆曲线。

func Sign

func Sign(priv *PrivateKey, msg, uid []byte) ([]byte, error)

Sign 用私钥对 msg 签名,返回 ASN.1 DER 编码的签名值(SEQUENCE{r, s})。 uid 传 nil 时使用 DefaultUID。

func SignHex

func SignHex(priv *PrivateKey, msg, uid []byte) (string, error)

SignHex 用私钥对 msg 签名,返回 r||s 拼接后的 64 字节 HEX 编码签名值。 部分银行网关(如招商银行对账文件接口)要求这种裸签名格式。

func SignToRS

func SignToRS(priv *PrivateKey, msg, uid []byte) (r, s *big.Int, err error)

SignToRS 用私钥对 msg 签名,返回签名值分量 r、s(GB/T 32918.2-2016 6.1)。

func Verify

func Verify(pub *PublicKey, msg, sig, uid []byte) error

Verify 用公钥校验 ASN.1 DER 编码的签名值。验签通过返回 nil。 uid 传 nil 时使用 DefaultUID。

func VerifyHex

func VerifyHex(pub *PublicKey, msg []byte, sigHex string, uid []byte) error

VerifyHex 用公钥校验 r||s 拼接的 64 字节 HEX 编码签名值。

func VerifyRS

func VerifyRS(pub *PublicKey, msg []byte, r, s *big.Int, uid []byte) error

VerifyRS 用公钥校验签名分量 r、s(GB/T 32918.2-2016 7.1)。

Types

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *big.Int
}

PrivateKey 是 SM2 私钥。

func GenerateKey

func GenerateKey() (*PrivateKey, error)

GenerateKey 生成一对新的 SM2 密钥。

func ParsePrivateKeyBytes

func ParsePrivateKeyBytes(raw []byte) (*PrivateKey, error)

ParsePrivateKeyBytes 从 32 字节大端整数解析 SM2 私钥。

func ParsePrivateKeyHex

func ParsePrivateKeyHex(hexKey string) (*PrivateKey, error)

ParsePrivateKeyHex 从 64 字符 HEX 字符串解析 SM2 私钥。

国密标准私钥为 32 字节大端整数,转 HEX 即 64 字符。多数银行网关 (如招商银行聚合支付)以该形式下发商户私钥。

func (*PrivateKey) Public

func (priv *PrivateKey) Public() *PublicKey

Public 返回对应的公钥。

type PublicKey

type PublicKey struct {
	Curve elliptic.Curve
	X, Y  *big.Int
}

PublicKey 是 SM2 公钥。

func ParsePublicKeyBase64

func ParsePublicKeyBase64(b64 string) (*PublicKey, error)

ParsePublicKeyBase64 从 Base64 编码的 ASN.1 DER 公钥解析 SM2 公钥。 招商银行等网关以此形式下发平台公钥。

func ParsePublicKeyDER

func ParsePublicKeyDER(der []byte) (*PublicKey, error)

ParsePublicKeyDER 从 ASN.1 DER 编码的 SubjectPublicKeyInfo 解析 SM2 公钥。

func ParsePublicKeyUncompressed

func ParsePublicKeyUncompressed(point []byte) (*PublicKey, error)

ParsePublicKeyUncompressed 从非压缩点格式(0x04 || X || Y,共 65 字节)解析公钥, 并校验该点位于曲线上。自行实现以避免调用已弃用的 elliptic.Unmarshal。

Jump to

Keyboard shortcuts

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