sm2

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 类型
	C1C3C2 = sm2.C1C3C2
	C1C2C3 = sm2.C1C2C3
)

Variables

View Source
var (
	// 获取 Cipher 类型
	GetCipherFromName = pkcs8.GetCipherFromName
	// 获取 hash 类型
	GetHashFromName = pkcs8.GetHashFromName
)
View Source
var (
	ErrKeyMustBePEMEncoded = errors.New("go-cryptobin/sm2: invalid key: Key must be a PEM encoded PKCS1 or PKCS8 key")
	ErrNotECPrivateKey     = errors.New("go-cryptobin/sm2: key is not a valid SM2 private key")
)

Functions

This section is empty.

Types

type EncodingType

type EncodingType uint

数据编码方式 marshal data type

const (
	EncodingASN1 EncodingType = 1 + iota
	EncodingBytes
)

type HashFunc

type HashFunc = func() hash.Hash

HashFunc

type Opts

type Opts = pkcs8.Opts

配置

type PBKDF2Opts

type PBKDF2Opts = pkcs8.PBKDF2Opts

PBKDF2 配置

type SM2

type SM2 struct {

	// 错误
	Errors []error
	// contains filtered or unexported fields
}

*

  • 国密 SM2 加密 *
  • @create 2022-4-16
  • @author deatil

func FromBase64String

func FromBase64String(data string) SM2

Base64数据

func FromBytes

func FromBytes(data []byte) SM2

字节

func FromHexString

func FromHexString(data string) SM2

16进制数据

func FromPKCS1PrivateKey

func FromPKCS1PrivateKey(key []byte) SM2

PKCS1 私钥

func FromPKCS1PrivateKeyWithPassword

func FromPKCS1PrivateKeyWithPassword(key []byte, password string) SM2

PKCS1 私钥带密码

func FromPKCS8PrivateKey

func FromPKCS8PrivateKey(key []byte) SM2

PKCS8 私钥

func FromPKCS8PrivateKeyWithPassword

func FromPKCS8PrivateKeyWithPassword(key []byte, password string) SM2

PKCS8 私钥带密码

func FromPrivateKey

func FromPrivateKey(key []byte) SM2

私钥

func FromPrivateKeyWithPassword

func FromPrivateKeyWithPassword(key []byte, password string) SM2

私钥带密码

func FromPublicKey

func FromPublicKey(key []byte) SM2

公钥,默认只有一种类型

func FromString

func FromString(data string) SM2

字符

func GenerateKey

func GenerateKey() SM2

生成密钥对

func GenerateKeyWithSeed

func GenerateKeyWithSeed(reader io.Reader) SM2

使用自定义数据生成密钥对

func New

func New() SM2

构造函数

func NewSM2

func NewSM2() SM2

构造函数

func (SM2) AppendError

func (this SM2) AppendError(errs ...error) SM2

添加错误

func (SM2) CheckKeyPair

func (this SM2) CheckKeyPair() bool

检测公钥私钥是否匹配

func (SM2) CreatePKCS1PrivateKey

func (this SM2) CreatePKCS1PrivateKey() SM2

生成私钥 pem 数据

func (SM2) CreatePKCS1PrivateKeyWithPassword

func (this SM2) CreatePKCS1PrivateKeyWithPassword(password string, opts ...string) SM2

生成私钥带密码 pem 数据

func (SM2) CreatePKCS8PrivateKey

func (this SM2) CreatePKCS8PrivateKey() SM2

生成私钥 pem 数据

func (SM2) CreatePKCS8PrivateKeyWithPassword

func (this SM2) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any) SM2

生成 PKCS8 私钥带密码 pem 数据 CreatePKCS8PrivateKeyWithPassword("123", "AES256CBC", "SHA256")

func (SM2) CreatePrivateKey

func (this SM2) CreatePrivateKey() SM2

生成私钥 pem 数据,默认使用 PKCS8 编码 使用: obj := New().GenerateKey() priKey := obj.CreatePrivateKey().ToKeyString()

func (SM2) CreatePrivateKeyWithPassword

func (this SM2) CreatePrivateKeyWithPassword(password string, opts ...any) SM2

生成私钥带密码 pem 数据

func (SM2) CreatePublicKey

func (this SM2) CreatePublicKey() SM2

生成公钥 pem 数据

func (SM2) Decrypt

func (this SM2) Decrypt() SM2

私钥解密

func (SM2) DecryptASN1

func (this SM2) DecryptASN1() SM2

私钥解密,解析 asn.1 编码格式的密文内容

func (SM2) DecryptASN1ECB

func (this SM2) DecryptASN1ECB() SM2

私钥解密,返回 asn.1 编码格式的密文内容, ECB 模式

func (SM2) DecryptBytes

func (this SM2) DecryptBytes() SM2

私钥解密

func (SM2) DecryptBytesECB

func (this SM2) DecryptBytesECB() SM2

私钥解密, ECB 模式

func (SM2) DecryptECB

func (this SM2) DecryptECB() SM2

私钥解密

func (SM2) Encrypt

func (this SM2) Encrypt() SM2

公钥加密

func (SM2) EncryptASN1

func (this SM2) EncryptASN1() SM2

公钥加密,返回 asn.1 编码格式的密文内容

func (SM2) EncryptASN1ECB

func (this SM2) EncryptASN1ECB() SM2

公钥加密,返回 asn.1 编码格式的密文内容, ECB 模式

func (SM2) EncryptBytes

func (this SM2) EncryptBytes() SM2

公钥加密

func (SM2) EncryptBytesECB

func (this SM2) EncryptBytesECB() SM2

公钥加密, ECB 模式

func (SM2) EncryptECB

func (this SM2) EncryptECB() SM2

公钥加密

func (SM2) Error

func (this SM2) Error() error

获取错误

func (SM2) FromBase64String

func (this SM2) FromBase64String(data string) SM2

Base64数据

func (SM2) FromBytes

func (this SM2) FromBytes(data []byte) SM2

字节

func (SM2) FromHexString

func (this SM2) FromHexString(data string) SM2

16进制数据

func (SM2) FromPKCS1PrivateKey

func (this SM2) FromPKCS1PrivateKey(key []byte) SM2

PKCS1 私钥

func (SM2) FromPKCS1PrivateKeyDer

func (this SM2) FromPKCS1PrivateKeyDer(der []byte) SM2

PKCS1 编码 DER 私钥

func (SM2) FromPKCS1PrivateKeyWithPassword

func (this SM2) FromPKCS1PrivateKeyWithPassword(key []byte, password string) SM2

PKCS1 私钥带密码

func (SM2) FromPKCS8PrivateKey

func (this SM2) FromPKCS8PrivateKey(key []byte) SM2

PKCS8 私钥

func (SM2) FromPKCS8PrivateKeyDer

func (this SM2) FromPKCS8PrivateKeyDer(der []byte) SM2

PKCS8 编码 DER 私钥

func (SM2) FromPKCS8PrivateKeyWithPassword

func (this SM2) FromPKCS8PrivateKeyWithPassword(key []byte, password string) SM2

PKCS8 私钥带密码

func (SM2) FromPrivateKey

func (this SM2) FromPrivateKey(key []byte) SM2

私钥

func (SM2) FromPrivateKeyBytes

func (this SM2) FromPrivateKeyBytes(priBytes []byte) SM2

私钥明文, hex 或者 base64 解码后

func (SM2) FromPrivateKeyString

func (this SM2) FromPrivateKeyString(keyString string) SM2

私钥明文 private-key: 07e4********;

func (SM2) FromPrivateKeyWithPassword

func (this SM2) FromPrivateKeyWithPassword(key []byte, password string) SM2

私钥带密码

func (SM2) FromPublicKey

func (this SM2) FromPublicKey(key []byte) SM2

公钥,默认只有一种类型

func (SM2) FromPublicKeyBytes

func (this SM2) FromPublicKeyBytes(pubBytes []byte) SM2

公钥明文, hex 或者 base64 解码后

func (SM2) FromPublicKeyCompressString

func (this SM2) FromPublicKeyCompressString(key string) SM2

公钥明文压缩 public-key: 027c******** || 036c******** 0333B01B61D94A775DA72A0BFF9AB324DE672EA0977584D23AF34F8150223305B0 023613B13F252F6FB2374A85D93C7FFE9CCAD1231BE866F5FE69255312CE85B9FF

func (SM2) FromPublicKeyDer

func (this SM2) FromPublicKeyDer(der []byte) SM2

DER 公钥

func (SM2) FromPublicKeyString

func (this SM2) FromPublicKeyString(key string) SM2

公钥明文

func (SM2) FromPublicKeyUncompressString

func (this SM2) FromPublicKeyUncompressString(keyString string) SM2

公钥明文未压缩 (hexStringX + hexStringY) public-key: 047c********.

func (SM2) FromPublicKeyXYBytes

func (this SM2) FromPublicKeyXYBytes(XBytes, YBytes []byte) SM2

公钥 x,y 字节对

func (SM2) FromPublicKeyXYString

func (this SM2) FromPublicKeyXYString(xString, yString string) SM2

公钥 x,y 16进制字符对 [xString: xHexString, yString: yHexString]

func (SM2) FromString

func (this SM2) FromString(data string) SM2

字符

func (SM2) GenerateKey

func (this SM2) GenerateKey() SM2

生成密钥对

func (SM2) GenerateKeyWithSeed

func (this SM2) GenerateKeyWithSeed(reader io.Reader) SM2

使用自定义数据生成密钥对

func (SM2) GetData

func (this SM2) GetData() []byte

get data

func (SM2) GetEncoding

func (this SM2) GetEncoding() EncodingType

get Encoding type

func (SM2) GetErrors

func (this SM2) GetErrors() []error

get errors

func (SM2) GetKeyData

func (this SM2) GetKeyData() []byte

get key Data

func (SM2) GetMode

func (this SM2) GetMode() sm2.Mode

get mode

func (SM2) GetParsedData

func (this SM2) GetParsedData() []byte

get parsedData

func (SM2) GetPrivateKey

func (this SM2) GetPrivateKey() *sm2.PrivateKey

get PrivateKey

func (SM2) GetPrivateKeyCurve

func (this SM2) GetPrivateKeyCurve() elliptic.Curve

get PrivateKey Curve

func (SM2) GetPrivateKeyDString

func (this SM2) GetPrivateKeyDString() string

get PrivateKey D hex string

func (SM2) GetPrivateKeyString

func (this SM2) GetPrivateKeyString() string

get PrivateKey data hex string

func (SM2) GetPublicKey

func (this SM2) GetPublicKey() *sm2.PublicKey

get PublicKey

func (SM2) GetPublicKeyCompressString

func (this SM2) GetPublicKeyCompressString() string

get PublicKey Compress Hex string

func (SM2) GetPublicKeyCurve

func (this SM2) GetPublicKeyCurve() elliptic.Curve

get PublicKey Curve

func (SM2) GetPublicKeyUncompressString

func (this SM2) GetPublicKeyUncompressString() string

get PublicKey Uncompress Hex string

func (SM2) GetPublicKeyXString

func (this SM2) GetPublicKeyXString() string

get PublicKey X hex string

func (SM2) GetPublicKeyXYString

func (this SM2) GetPublicKeyXYString() string

get PublicKey X and Y Hex string

func (SM2) GetPublicKeyYString

func (this SM2) GetPublicKeyYString() string

get PublicKey Y hex string

func (SM2) GetUID

func (this SM2) GetUID() []byte

get uid

func (SM2) GetVerify

func (this SM2) GetVerify() bool

get verify data

func (SM2) MakeKeyDer

func (this SM2) MakeKeyDer() SM2

生成密钥 der 数据

func (SM2) MakePublicKey

func (this SM2) MakePublicKey() SM2

生成公钥

func (SM2) OnError

func (this SM2) OnError(fn SM2ErrorFunc) SM2

引出错误信息

func (SM2) ParsePKCS1PrivateKeyFromPEM

func (this SM2) ParsePKCS1PrivateKeyFromPEM(key []byte) (*sm2.PrivateKey, error)

解析 PKCS1 私钥

func (SM2) ParsePKCS1PrivateKeyFromPEMWithPassword

func (this SM2) ParsePKCS1PrivateKeyFromPEMWithPassword(key []byte, password string) (*sm2.PrivateKey, error)

解析 PKCS1 私钥带密码

func (SM2) ParsePKCS8PrivateKeyFromPEM

func (this SM2) ParsePKCS8PrivateKeyFromPEM(key []byte) (*sm2.PrivateKey, error)

解析 PKCS8 私钥

func (SM2) ParsePKCS8PrivateKeyFromPEMWithPassword

func (this SM2) ParsePKCS8PrivateKeyFromPEMWithPassword(key []byte, password string) (*sm2.PrivateKey, error)

解析 PKCS8 带密码的私钥

func (SM2) ParsePrivateKeyFromPEM

func (this SM2) ParsePrivateKeyFromPEM(key []byte) (*sm2.PrivateKey, error)

解析私钥,默认为 PKCS8

func (SM2) ParsePrivateKeyFromPEMWithPassword

func (this SM2) ParsePrivateKeyFromPEMWithPassword(key []byte, password string) (*sm2.PrivateKey, error)

解析私钥带密码,默认为 PKCS8

func (SM2) ParsePublicKeyFromPEM

func (this SM2) ParsePublicKeyFromPEM(key []byte) (*sm2.PublicKey, error)

解析公钥

func (SM2) SetMode

func (this SM2) SetMode(data string) SM2

设置 mode C1C3C2 = 0 | C1C2C3 = 1

func (SM2) SetSignHash

func (this SM2) SetSignHash(data string) SM2

设置 hash 类型 可用参数可查看 Hash 结构体数据

func (SM2) SetUID

func (this SM2) SetUID(data string) SM2

设置 uid

func (SM2) Sign

func (this SM2) Sign() SM2

私钥签名

func (SM2) SignASN1

func (this SM2) SignASN1() SM2

私钥签名 ASN1

func (SM2) SignBytes

func (this SM2) SignBytes() SM2

私钥签名 Bytes 兼容[招行]

func (SM2) ToBase64String

func (this SM2) ToBase64String() string

输出Base64

func (SM2) ToBytes

func (this SM2) ToBytes() []byte

输出字节

func (SM2) ToHexString

func (this SM2) ToHexString() string

输出Hex

func (SM2) ToKeyBytes

func (this SM2) ToKeyBytes() []byte

私钥/公钥

func (SM2) ToKeyString

func (this SM2) ToKeyString() string

私钥/公钥

func (SM2) ToString

func (this SM2) ToString() string

输出字符

func (SM2) ToVerify

func (this SM2) ToVerify() bool

验证结果

func (SM2) ToVerifyInt

func (this SM2) ToVerifyInt() int

验证结果,返回 int 类型

func (SM2) Verify

func (this SM2) Verify(data []byte) SM2

公钥验证 使用原始数据[data]对比签名后数据

func (SM2) VerifyASN1

func (this SM2) VerifyASN1(data []byte) SM2

公钥验证 ASN1 使用原始数据[data]对比签名后数据

func (SM2) VerifyBytes

func (this SM2) VerifyBytes(data []byte) SM2

公钥验证 Bytes 兼容[招行] 使用原始数据[data]对比签名后数据

func (SM2) WithData

func (this SM2) WithData(data []byte) SM2

设置 data

func (SM2) WithEncoding

func (this SM2) WithEncoding(encoding EncodingType) SM2

设置编码方式

func (SM2) WithEncodingASN1

func (this SM2) WithEncodingASN1() SM2

设置 ASN1 编码方式

func (SM2) WithEncodingBytes

func (this SM2) WithEncodingBytes() SM2

设置明文编码方式

func (SM2) WithErrors

func (this SM2) WithErrors(errs []error) SM2

设置错误

func (SM2) WithMode

func (this SM2) WithMode(data sm2.Mode) SM2

设置 mode

func (SM2) WithParsedData

func (this SM2) WithParsedData(data []byte) SM2

设置 parsedData

func (SM2) WithPrivateKey

func (this SM2) WithPrivateKey(data *sm2.PrivateKey) SM2

设置 PrivateKey

func (SM2) WithPublicKey

func (this SM2) WithPublicKey(data *sm2.PublicKey) SM2

设置 PublicKey

func (SM2) WithSignHash

func (this SM2) WithSignHash(data HashFunc) SM2

设置 hash 类型

func (SM2) WithUID

func (this SM2) WithUID(data []byte) SM2

设置 uid

func (SM2) WithVerify

func (this SM2) WithVerify(data bool) SM2

设置 verify

type SM2ErrorFunc

type SM2ErrorFunc = func([]error)

错误方法

type ScryptOpts

type ScryptOpts = pkcs8.ScryptOpts

Scrypt 配置

Jump to

Keyboard shortcuts

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