crypto

package module
v0.0.8 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: 4 Imported by: 10

README

Crypto

Crypto Service Provider interface in go.

Table of Contents

Mockgen

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

How to use?

  • source: 指定接口文件
  • destination: 生成的文件名
  • package:生成文件的包名
  • imports: 依赖的需要import的包
  • aux_files:接口文件不止一个文件时附加文件
  • build_flags: 传递给build工具的参数

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

Eg.mockgen -destination mock/mock_engine.go -package crypto -source engine.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.

License

This project is currently under Apache 2.0 license. See the LICENSE file for details

Documentation

Index

Constants

View Source
const (
	//Hash message digest algorithm
	Hash = 0
	//Asymmetric asymmetric encryption algorithm
	Asymmetric = 8
	//Symmetrical symmetric encryption algorithm
	Symmetrical = 16
)
View Source
const (
	//None unknown algorithm type, type information is hidden in the content, for example PKCS8
	None = 0x0

	//Hash
	FakeHash         = 0x00 << Hash
	SHA1             = 0x10 << Hash
	SHA2             = 0x20 << Hash
	SHA3             = 0x30 << Hash
	KECCAK           = 0x40 << Hash
	SM3              = 0x50 << Hash
	Sm3WithPublicKey = 0x60 << Hash //with default SM2 userID: 1234567812345678
	Size224          = 0x01 << Hash
	Size256          = 0x00 << Hash
	Size384          = 0x02 << Hash
	Size512          = 0x03 << Hash
	SHA2_224         = SHA2 | Size224
	SHA2_256         = SHA2 | Size256
	SHA2_384         = SHA2 | Size384
	SHA2_512         = SHA2 | Size512
	SHA3_224         = SHA3 | Size224
	SHA3_256         = SHA3 | Size256
	SHA3_384         = SHA3 | Size384
	SHA3_512         = SHA3 | Size512
	KECCAK_224       = KECCAK | Size224
	KECCAK_256       = KECCAK | Size256
	KECCAK_384       = KECCAK | Size384
	KECCAK_512       = KECCAK | Size512

	//Asymmetric Algo
	Sm2p256v1        = 0x01 << Asymmetric
	Secp256k1        = 0x02 << Asymmetric
	Secp256r1        = 0x03 << Asymmetric
	Secp384r1        = 0x04 << Asymmetric
	Secp521r1        = 0x05 << Asymmetric
	Secp256k1Recover = 0x06 << Asymmetric
	Rsa2048          = 0x10 << Asymmetric
	Rsa3072          = 0x11 << Asymmetric
	Rsa4096          = 0x12 << Asymmetric
	Ed25519          = 0x20 << Asymmetric

	//Symmetrical Algo for Encrypt and Decrypt
	Sm4  = 0x01 << Symmetrical
	Aes  = 0x02 << Symmetrical
	Des3 = 0x03 << Symmetrical
	CBC  = 0x10 << Symmetrical
	ECB  = 0x20 << Symmetrical
	GCM  = 0x30 << Symmetrical
)

algorithm identifier const value table

Variables

View Source
var ErrNotSupport = fmt.Errorf("engine: this algo is not support")

ErrNotSupport this algo is not support

Functions

func ErrDevice

func ErrDevice(msg string) error

ErrDevice device internal error

func ErrIndexMissing

func ErrIndexMissing(index []byte) error

ErrIndexMissing index missing

Types

type Cryptor

type Cryptor interface {
	Encryptor
	Decryptor
}

Cryptor is interface that provide crypto function deprecated

type DecKey

type DecKey interface {
	EncKey
	Decrypt(cipher []byte) ([]byte, error)
	Destroy()
}

DecKey private key which can decrypt

type Decryptor

type Decryptor interface {
	// Decrypt decrypts ciphertext using key k.
	Decrypt(k, cipherText []byte) (plaintext []byte, err error)
}

Decryptor is a interface that provides decryption algorithms

type EncKey

type EncKey interface {
	GetKeyInfo() int
	Encrypt(msg []byte, reader io.Reader) ([]byte, error)
	//for more information, see  comment of VerifyKey.Bytes
	Bytes() []byte
}

EncKey public key which can encrypt

type Encryptor

type Encryptor interface {
	// Encrypt encrypts plaintext using key k.
	Encrypt(k, plaintext []byte, reader io.Reader) (cipherText []byte, err error)
}

Encryptor is a interface that provides encryption algorithms

type FlagReader

type FlagReader interface {
	io.Reader
	GetFlag() int
}

FlagReader reader use as flag

type Hasher

type Hasher interface {
	hash.Hash
	// Hash hashes messages msg.
	Hash(msg []byte) (hash []byte, err error)
	// BachHash If you need to hash a series of bytes slices, calling BachHash can reduce one copy. BenchHash implies a loop.
	// example: a,b,c,d are four big byte slice
	// BenchHash([][]bytes{a,b,c,d}) faster than Hash(bytes.Join([][]bytes{a,b,c,d},nil))
	// It reduce one join(...). Join implies one copy.
	BatchHash(msg [][]byte) (hash []byte, err error)
}

Hasher is a interface that provides hash algorithms

type Key

type Key interface {
	// Bytes converts this key to its byte representation,
	// if this operation is allowed.
	Bytes() ([]byte, error)

	//FromBytes It's revert method to Bytes()
	//K is a byte that needs to be parsed, and the meaning of opt depend on Keys, for example, it's maybe an algorithm type. If the parsing fails, return empty Key or nil.
	FromBytes(k []byte, opt int) error
}

Key represents a cryptographic key

type Level

type Level interface {
	GetLevel() ([]int, uint8)
}

Level priority of plugins

type PluginCreateDecKeyFunc

type PluginCreateDecKeyFunc interface {
	Level
	//generate a DecKey, and return index or key in raw form
	//if persistent is true, key should be persistent and return index
	//	or persistent is false, key should be persistent and output should in raw form
	CreateDecKey(persistent bool, mode int) (index []byte, k DecKey, err error)
}

PluginCreateDecKeyFunc create decryption function

type PluginCreateSignFunc

type PluginCreateSignFunc interface {
	Level
	//generate a SignKey, and return index or key in raw form
	//if persistent is true, key should be persistent and return index
	//	or persistent is false, key should be persistent and output should in raw form
	CreateSignKey(persistent bool, mode int) (index []byte, k SignKey, err error)
}

PluginCreateSignFunc create SignKey

type PluginCryptFunc

type PluginCryptFunc interface {
	Level
	GetSecretKey(mode int, pwd, key []byte) (SecretKey, error)
}

PluginCryptFunc symmetric encryption and decryption function

type PluginDecKeyFunc

type PluginDecKeyFunc interface {
	Level
	//enter index or raw privat key to generate a DecKey, key will not be persistent
	// a raw private key is a big integer
	GetDecKey(key []byte, mode int) (DecKey, error)
	//enter raw private key, return index, key should be persistent
	ImportDecKey(key []byte, mode int) (index []byte, err error)
}

PluginDecKeyFunc asymmetric decryption function

type PluginEncKeyFunc

type PluginEncKeyFunc interface {
	Level
	//enter a raw publicKey and mod, return a VerifyKey
	//see GetVerifyKey's comment for the meaning of a raw publicKey
	GetEncKey(key []byte, mode int) (EncKey, error)
}

PluginEncKeyFunc asymmetric encryption function

type PluginHashFunc

type PluginHashFunc interface {
	Level
	GetHash(mode int) (Hasher, error)
}

PluginHashFunc hash function

type PluginRandomFunc

type PluginRandomFunc interface {
	Level
	Rander() (io.Reader, error)
}

PluginRandomFunc random function

type PluginSignFunc

type PluginSignFunc interface {
	Level
	//enter index or raw privat key to generate a SignKey, key will not be persistent
	// a raw private key is a big integer
	GetSignKey(key []byte, mode int) (SignKey, error)
	//enter raw private key, return index, key should be persistent
	ImportSignKey(key []byte, mode int) (index []byte, err error)
}

PluginSignFunc sign function

type PluginVerifyFunc

type PluginVerifyFunc interface {
	Level
	//enter a raw publicKey and mod, return a VerifyKey
	//a raw publicKey means:
	// 1) for sm2, key is 65bytes and in 0x04||X||Y form, see GMT0009-2012 7.1
	//      http://www.gmbz.org.cn/main/viewfile/2018011001400692565.html may help
	// 2) for ecdsa, key is in 0x04||X||Y. The length depends on the curve, for example,
	//		65 bytes for secp256k1 and 133 for secp521r1, see 2.3.3 in [SEC1] uncompressed form.
	//		https://www.rfc-editor.org/rfc/rfc5480.txt may help
	// 3) for rsa, key is in PKCS#1 form, see RFC2313 RSAPublicKey
	//		RSAPublicKey ::= SEQUENCE {
	//     		modulus INTEGER, -- n
	//     		publicExponent INTEGER -- e }
	//		https://www.rfc-editor.org/rfc/rfc2313.txt may help
	GetVerifyKey(key []byte, mode int) (VerifyKey, error)
}

PluginVerifyFunc verify function

type PrivateKey

type PrivateKey interface {
	SignKey
	DecKey
}

PrivateKey represents the private key, able to sign and decrypt

type PublicKey

type PublicKey interface {
	VerifyKey
	EncKey
}

PublicKey represents the public key, capable of verification and encryption

type SecretKey

type SecretKey interface {
	Encrypt(src []byte, reader io.Reader) []byte
	Decrypt(src []byte) []byte
	Destroy()
}

SecretKey sym

type SignKey

type SignKey interface {
	VerifyKey
	Sign(msg []byte, hasher hash.Hash, rand io.Reader) ([]byte, error)
	Destroy()
}

SignKey private key which can sign

type Signer

type Signer interface {
	Key
	Sign(k, digest []byte, reader io.Reader) ([]byte, error)
}

Signer sign

type Verifier

type Verifier interface {
	Key
	// Verify verifies signature against key k and digest
	Verify(k, signature, digest []byte) (valid bool, err error)
}

Verifier is a interface that provides verifying algorithms

type VerifyKey

type VerifyKey interface {
	GetKeyInfo() int
	Verify(msg []byte, hasher hash.Hash, sig []byte) bool
	//return a raw
	//sm2: SM2PublicKey::=BIT STRING,	04||X||Y,	65 Bytes,	GMT0009-2012 7.1
	//ecdsa: PublicKeyBytes in PKIX publicKey
	//rsa: asn1{N, e}
	//for more information, see GetVerifyKey's comment
	Bytes() []byte
}

VerifyKey public key which can verify

Directories

Path Synopsis
Package crypto is a generated GoMock package.
Package crypto is a generated GoMock package.

Jump to

Keyboard shortcuts

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