gm

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: Apache-2.0 Imports: 15 Imported by: 6

README

Crypto-gm

Guomi crypto algorithm implement.

Table of Contents

Usage

Sm3 computing hash
    msg := "abc"
    sm3 := NewSM3Hasher()
	h, err := sm3.Hash([]byte(msg))     //h is the result
Sm4 encryption and decryption
    msg := "abc"
    sm4 := new(SM4)
    key := []byte("1234567812345678")
    c, err := sm4.Encrypt(key, []byte(msg))
    //handle err
    o, err := sm4.Decrypt(key, c)
    //handle err
sm2
    privateKey, _ := GenerateSM2Key()
    pub, _ := privateKey.PublicKey()
    h := HashBeforeSM2(pub, []byte(msg))
    s, _ := privateKey.Sign(nil, h)
    b, _ := pub.Verify(nil, s, h)
sm9
    kgc := GenerateKGC()
    key := kgc.GenerateKey([]byte("hyperchain"))
    s, _ := key.Sign(nil, []byte(msg))
    ID, _ := key.PublicKey()
    b, _ := ID.Verify(nil, s, []byte(msg))

API

sm3

Get Hasher: func NewSM3Hasher() *Hasher

Computational hash: func (h *Hasher) Hash(msg []byte) (hash []byte, err error)

sm4

Encrypt: func (ea *SM4) Encrypt(key, originMsg []byte) (encryptedMsg []byte, err error)

Decrypt: func (ea *SM4) Decrypt(key, encryptedMsg []byte) (originMsg []byte, err error)

sm2

Generate private key: func GenerateSM2Key() (SM2PrivateKey, error)

signature: func (key *SM2PrivateKey) Sign(_ []byte, digest []byte) ([]byte, error)

verify: func (id *ID) Verify(_ []byte, signature, msg []byte) (valid bool, err error)

sm9

generate signature: func (sm9 *SM9) Sign(k []byte, msg []byte) (signature []byte, err error)

verify: func (sm9 *SM9) Verify(k []byte, signature, msg []byte) (valid bool, err error)

Mockgen

Install mockgen : go get github.com/golang/mock/mockgen

How to use?

  • source: Specify interface file
  • destination: Generated file name
  • package:The package name of the generated file
  • imports: Dependent package that requires import
  • aux_files: Attach a file when there is more than one file in the interface file
  • build_flags: Parameters passed to the build tool

Eg.mockgen -destination mock/mock_crypto.go -package crypto -source crypto.go

GitCZ

Note: Please use command npm install if you are the first time to use git cz in this repo.

Contribute

PRs are welcome!

Small note: If editing the Readme, please conform to the standard-readme specification.

##CUDA download : http://nexus.hyperchain.cn/repository/arch/cuda/libsm2cuda.so

License

crypto-gm is currently under Apache 2.0 license. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(priv *SM2PrivateKey, data []byte) ([]byte, error)

Decrypt sm2 ecc decrypt

func Encrypt

func Encrypt(pub *SM2PublicKey, data []byte, ivReader io.Reader) ([]byte, error)

Encrypt sm2 ecc encrypt

func GenerateSM2KeyForDH

func GenerateSM2KeyForDH(idA, idB, randA []byte, privateKey, publicAX, publicAY, publicBX, publicBY *big.Int, RB *SM2PublicKey, isInit bool) (*big.Int, *big.Int, []byte, error)

GenerateSM2KeyForDH generate a key using sm2 for dh idA is the ID of self, idB is the ID of another part, isInit indicates whether it is the initiator or not

func GetSM3Hasher

func GetSM3Hasher() hash.Hash

GetSM3Hasher get hash.Hash

func GetSm2Curve

func GetSm2Curve() elliptic.Curve

GetSm2Curve get sm2 Curve

func GetSm4Cipher

func GetSm4Cipher(key []byte) (cipher.Block, error)

GetSm4Cipher get cipher.Block

func HashBeforeSM2

func HashBeforeSM2(pub *SM2PublicKey, msg []byte) []byte

HashBeforeSM2 If hash is for using sm2, invoke this method.

func NewSM3IDHasher

func NewSM3IDHasher() hash.Hash

NewSM3IDHasher instruct a SM# Hasher

func Sm4DecryptCBC

func Sm4DecryptCBC(key, src []byte) ([]byte, error)

Sm4DecryptCBC decrypt with sm4, use CBC mode with iv

func Sm4EncryptCBC

func Sm4EncryptCBC(key, originMsg []byte, randReader io.Reader) ([]byte, error)

Sm4EncryptCBC encrypt with sm4, use CBC mode with iv iv is

Types

type Hasher

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

Hasher thw return value of function NewHasher

func NewSM3Hasher

func NewSM3Hasher() *Hasher

NewSM3Hasher instruct a SM# Hasher

func (*Hasher) BatchHash

func (h *Hasher) BatchHash(msg [][]byte) (hash []byte, err error)

BatchHash hash with two-dimensional array

func (*Hasher) BlockSize

func (h *Hasher) BlockSize() int

BlockSize hash block size

func (*Hasher) Hash

func (h *Hasher) Hash(msg []byte) (hash []byte, err error)

Hash compute hash

func (*Hasher) Reset

func (h *Hasher) Reset()

Reset hash reset

func (*Hasher) Size

func (h *Hasher) Size() int

Size hash size

func (*Hasher) Sum

func (h *Hasher) Sum(b []byte) []byte

Sum hash sum

func (*Hasher) Write

func (h *Hasher) Write(p []byte) (n int, err error)

type IDHasher

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

IDHasher thw return value of function NewHasher

func (*IDHasher) BatchHash

func (h *IDHasher) BatchHash(msg [][]byte) (hash []byte, err error)

BatchHash hash with two-dimensional array

func (*IDHasher) BlockSize

func (h *IDHasher) BlockSize() int

BlockSize hash block size

func (*IDHasher) Hash

func (h *IDHasher) Hash(msg []byte) (hash []byte, err error)

Hash compute hash

func (*IDHasher) Reset

func (h *IDHasher) Reset()

Reset hash reset

func (*IDHasher) Size

func (h *IDHasher) Size() int

Size hash size

func (*IDHasher) Sum

func (h *IDHasher) Sum(b []byte) []byte

Sum hash sum

func (*IDHasher) Write

func (h *IDHasher) Write(p []byte) (n int, err error)

type SM2

type SM2 struct {
}

SM2 SM2 instance is a tool to sign and verify. You can sign and verify via SM2PrivateKey and SM2PublicKey, SM2 instance is just a package of SM2PrivateKey's Sign and SM2PublicKey's Verify. If you need revoke Sign or Verify at a sepcific Key many times, we recommend using SM2PrivateKey and SM2PublicKey, which avoid decode and alloc repeatedly. All in all, SM2 is convenient; SM2PrivateKey and SM2PublicKey are faster.

func NewSM2

func NewSM2() *SM2

NewSM2 get a SM2 instance, input parameter is algorithm type

func (*SM2) Sign

func (sv *SM2) Sign(k []byte, digest []byte, _ io.Reader) (signature []byte, err error)

Sign get signature to digest, K is the private key

func (*SM2) Verify

func (sv *SM2) Verify(k []byte, signature, digest []byte) (valid bool, err error)

Verify verify signature ,K is the public key

type SM2PrivateKey

type SM2PrivateKey struct {
	K         [sm2KeyLen]byte
	PublicKey SM2PublicKey
}

SM2PrivateKey SM2 private key. never new(SM2PrivateKey), use NewSM2PrivateKey()

func GenerateSM2Key

func GenerateSM2Key() (*SM2PrivateKey, error)

GenerateSM2Key generate a pair of key,input is algorithm type

func (*SM2PrivateKey) Bytes

func (key *SM2PrivateKey) Bytes() ([]byte, error)

Bytes return key bytes. Inverse method of FromBytes(K []byte, opt AlgorithmOption)

func (*SM2PrivateKey) CalculatePublicKey

func (key *SM2PrivateKey) CalculatePublicKey() *SM2PrivateKey

CalculatePublicKey Calculate the public key contained in the private key when get a SM2PrivateKey by FromBytes(...), the public key contained is empty, you should invoke SetPublicKey(...) or CalculatePublicKey(). If you have the Public Key,SetPublicKey(...) is better and faster, since CalculatePublicKey() while calculate public key once again.

func (*SM2PrivateKey) FromBytes

func (key *SM2PrivateKey) FromBytes(k []byte, opt int) error

FromBytes parse a private Key from bytes, Inverse method of Bytes()

func (*SM2PrivateKey) Public

func (key *SM2PrivateKey) Public() std.PublicKey

Public Get SM2PublicKey from a SM2PrivateKey, if SM2PublicKey is empty, this method will invoke CalculatePublicKey().

func (*SM2PrivateKey) SetPublicKey

func (key *SM2PrivateKey) SetPublicKey(k *SM2PublicKey) *SM2PrivateKey

SetPublicKey Set the public key contained in the private key when get a SM2PrivateKey by FromBytes(...), the public key contained is empty, you should invoke SetPublicKey(...) or CalculatePublicKey(). If you have the Public Key,SetPublicKey(...) is better and faster, since CalculatePublicKey() while calculate public key once again.

func (*SM2PrivateKey) Sign

func (key *SM2PrivateKey) Sign(k, digest []byte, reader io.Reader) ([]byte, error)

Sign get signature of specific digest by SM2PrivateKey self,so the first parameter will be ignored

func (*SM2PrivateKey) SignBatch

func (key *SM2PrivateKey) SignBatch(k, digest []byte, reader io.Reader) ([]byte, error)

SignBatch get signature of specific digest by SM2PrivateKey self,so the first parameter will be ignored first bytes is flag

type SM2PublicKey

type SM2PublicKey struct {
	Curve elliptic.Curve
	X     [sm2KeyLen]byte
	Y     [sm2KeyLen]byte
}

SM2PublicKey SM2 public key. never new(SM2PublicKey), use NewSM2PublicKey()

func (*SM2PublicKey) Bytes

func (key *SM2PublicKey) Bytes() ([]byte, error)

Bytes return key bytes

func (*SM2PublicKey) FromBytes

func (key *SM2PublicKey) FromBytes(k []byte, opt int) error

FromBytes Parse a public key from 65 bytes and specific algorithm.The reverse method of Bytes()

func (*SM2PublicKey) Verify

func (key *SM2PublicKey) Verify(_, signature, digest []byte) (valid bool, err error)

Verify verify the signature by SM2PublicKey self, so the first parameter will be ignored.

type SM4

type SM4 struct {
}

SM4 a tool to encrypt and decrypt

func (*SM4) Decrypt

func (ea *SM4) Decrypt(key, encryptedMsg []byte) (originMsg []byte, err error)

Decrypt decrypt with sm4, use CBC mode with vi

func (*SM4) Encrypt

func (ea *SM4) Encrypt(key, originMsg []byte, reader io.Reader) (encryptedMsg []byte, err error)

Encrypt encrypt with sm4, use CBC mode with vi

type SM4Key

type SM4Key []byte

SM4Key represent sm4 key

func (SM4Key) Bytes

func (t SM4Key) Bytes() ([]byte, error)

Bytes return bytes

func (SM4Key) FromBytes

func (t SM4Key) FromBytes(k []byte, opt interface{}) []byte

FromBytes get a key from bytes

Directories

Path Synopsis
internal
sm2
sm3
sm4

Jump to

Keyboard shortcuts

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