sm2

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	C1C3C2 = 0
	C1C2C3 = 1
)

Functions

func BytesCombine

func BytesCombine(pBytes ...[]byte) []byte

func CipherMarshal

func CipherMarshal(data []byte) ([]byte, error)

*sm2密文转asn.1编码格式 *sm2密文结构如下: * x * y * hash * CipherText

func CipherUnmarshal

func CipherUnmarshal(data []byte) ([]byte, error)

sm2密文asn.1编码格式转C1|C3|C2拼接格式

func Compress

func Compress(a *PublicKey) []byte

func Decrypt

func Decrypt(priv *PrivateKey, data []byte, mode int) ([]byte, error)

func DecryptAsn1

func DecryptAsn1(pub *PrivateKey, data []byte) ([]byte, error)

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

func Encrypt

func Encrypt(pub *PublicKey, data []byte, random io.Reader, mode int) ([]byte, error)

* sm2密文结构如下: * x * y * hash * CipherText

func EncryptAsn1

func EncryptAsn1(pub *PublicKey, data []byte, rand io.Reader) ([]byte, error)

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

func KeyExchangeA

func KeyExchangeA(klen int, ida, idb []byte, priA *PrivateKey, pubB *PublicKey, rpri *PrivateKey, rpubB *PublicKey) (k, s1, s2 []byte, err error)

KeyExchangeA 协商第二部,用户A调用,返回共享密钥k

func KeyExchangeB

func KeyExchangeB(klen int, ida, idb []byte, priB *PrivateKey, pubA *PublicKey, rpri *PrivateKey, rpubA *PublicKey) (k, s1, s2 []byte, err error)

**************************Key agreement algorithm**************************// KeyExchangeB 协商第二部,用户B调用, 返回共享密钥k

func P256Sm2

func P256Sm2() elliptic.Curve

func SignDataToSignDigit

func SignDataToSignDigit(sign []byte) (*big.Int, *big.Int, error)

func SignDigitToSignData

func SignDigitToSignData(r, s *big.Int) ([]byte, error)

func Sm2Sign

func Sm2Sign(priv *PrivateKey, msg, uid []byte, random io.Reader) (r, s *big.Int, err error)

func Sm2Verify

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

func Verify

func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool
    za, err := ZA(pub, uid)
	if err != nil {
		return
	}
	e, err := msgHash(za, msg)
	hash=e.getBytes()

func WNafReversed

func WNafReversed(wnaf []int8) []int8

func ZA

func ZA(pub *PublicKey, uid []byte) ([]byte, error)

ZA = H256(ENTLA || IDA || a || b || xG || yG || xA || yA)

Types

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *big.Int
}

func GenerateKey

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

func (*PrivateKey) Decrypt

func (priv *PrivateKey) Decrypt(_ io.Reader, msg []byte, _ crypto.DecrypterOpts) (plaintext []byte, err error)

crypto.Decrypter

func (*PrivateKey) DecryptAsn1

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

func (*PrivateKey) Public

func (priv *PrivateKey) Public() crypto.PublicKey

The SM2's private key contains the public key

func (*PrivateKey) Sign

func (priv *PrivateKey) Sign(random io.Reader, msg []byte, signer myhash.SignerOpts) ([]byte, error)

sign format = 30 + len(z) + 02 + len(r) + r + 02 + len(s) + s, z being what follows its size, ie 02+len(r)+r+02+len(s)+s

func (*PrivateKey) SignReader

func (priv *PrivateKey) SignReader(random io.Reader, message io.Reader, signer myhash.SignerOpts) ([]byte, error)

type PublicKey

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

func Decompress

func Decompress(a []byte) *PublicKey

func (*PublicKey) EncryptAsn1

func (pub *PublicKey) EncryptAsn1(data []byte, random io.Reader) ([]byte, error)

****************************Encryption algorithm****************************//

func (*PublicKey) Sm3Digest

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

func (*PublicKey) Verify

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

Jump to

Keyboard shortcuts

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