encrypt

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package encrypt implement common encrypt and decrypt for stream

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInputInvalidLength     = errors.New("encoded message length must be more than zero")
	ErrInputNotMoreABlock     = errors.New("decoded message length must be more than a block size")
	ErrInputNotMultipleBlocks = errors.New("decoded message length must be multiple of block size")
	ErrInvalidIvSize          = errors.New("iv length must equal block size")
	ErrUnPaddingOutOfRange    = errors.New("unPadding out of range")
)

error defined

Functions

func CipherMethods

func CipherMethods() []string

CipherMethods 获取Cipher的所有支持方法

func Evp2Key

func Evp2Key(password string, keyLen int) (key []byte)

Evp2Key evp to key

func HasCipherMethod

func HasCipherMethod(method string) (ok bool)

HasCipherMethod 是否有method方法

func NewBlowfishCipher added in v0.0.9

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

NewBlowfishCipher new blowfish cipher

func NewCast5Cipher added in v0.0.9

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

NewCast5Cipher new cast5 cipher

func NewChacha20 added in v0.0.9

func NewChacha20(key, iv []byte) (cipher.Stream, error)

NewChacha20 new chacha20 key size should 32, iv size should one of 12,24

func NewRc4Md5 added in v0.0.9

func NewRc4Md5(key, iv []byte) (cipher.Stream, error)

NewRc4Md5 new rc4-md5 key size should 16, iv size should one of 6,16

func NewSalsa20 added in v0.0.9

func NewSalsa20(key, iv []byte) (cipher.Stream, error)

NewSalsa20 new salsa20 key size should 32, iv size should one of 8

func NewStream added in v0.0.4

func NewStream(method string, key, iv []byte, encrypt bool) (cipher.Stream, error)

NewStream new stream

func NewTwofishCipher added in v0.0.9

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

NewTwofishCipher new twofish cipher

func NewXteaCipher added in v0.0.9

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

NewXteaCipher new xtea cipher

func PCKSPadding added in v0.0.9

func PCKSPadding(origData []byte, blockSize int) []byte

PCKSPadding PKCS#5和PKCS#7 填充

func PCKSUnPadding added in v0.0.9

func PCKSUnPadding(origData []byte) ([]byte, error)

PCKSUnPadding PKCS#5和PKCS#7 解填充

func RandIV added in v0.0.9

func RandIV(block cipher.Block) ([]byte, error)

RandIV generate rand iv by rand.Reader

func Valid added in v0.0.8

func Valid(method, password string) bool

Valid method password is valid or not

Types

type Apply added in v0.0.9

type Apply interface {
	// contains filtered or unexported methods
}

Apply apply

type BlockCrypt added in v0.0.9

type BlockCrypt interface {
	// BlockSize returns the mode's block size.
	BlockSize() int
	// Encrypt plain text. return iv + cipher text
	Encrypt(plainText []byte) ([]byte, error)
	// Encrypt cipher text(iv + cipher text). plain text.
	Decrypt(cipherText []byte) ([]byte, error)
}

BlockCrypt block crypt interface

type BlockModeCipher added in v0.0.9

type BlockModeCipher struct {
	NewEncrypt func(block cipher.Block, iv []byte) cipher.BlockMode
	NewDecrypt func(block cipher.Block, iv []byte) cipher.BlockMode
}

BlockModeCipher block mode cipher support:

cbc: cipher.NewCBCEncrypter, cipher.NewCBCDecrypter

func (*BlockModeCipher) New added in v0.0.9

func (sf *BlockModeCipher) New(key []byte,
	newCipher func(key []byte) (cipher.Block, error), opts ...Option) (BlockCrypt, error)

New new with newCipher and key newCipher support follow or implement func(key []byte) (cipher.Block, error):

aes
cipher
des
blowfish
cast5
twofish
xtea
tea

type BlockStreamCipher added in v0.0.9

type BlockStreamCipher struct {
	NewEncrypt func(block cipher.Block, iv []byte) cipher.Stream
	NewDecrypt func(block cipher.Block, iv []byte) cipher.Stream
}

BlockStreamCipher block stream cipher support:

cfb: cipher.NewCFBEncrypter, cipher.NewCFBDecrypter
ctr: cipher.NewCTR, cipher.NewCTR
ofb: cipher.NewOFB, cipher.NewOFB

func (*BlockStreamCipher) New added in v0.0.9

func (sf *BlockStreamCipher) New(key []byte,
	newCipher func(key []byte) (cipher.Block, error), opts ...Option) (BlockCrypt, error)

New new with newCipher and key newCipher support follow or implement func(key []byte) (cipher.Block, error):

aes
cipher
des
blowfish
cast5
twofish
xtea
tea

type Cipher

type Cipher struct {
	Write cipher.Stream
	Read  cipher.Stream
}

Cipher implement write and read cipher.Stream

func NewCipher

func NewCipher(method, password string) (*Cipher, error)

NewCipher new cipher method support:

aes-128-cfb
aes-192-cfb
aes-256-cfb
aes-128-ctr
aes-192-ctr
aes-256-ctr
aes-128-ofb
aes-192-ofb
aes-256-ofb
des-cfb
des-ctr
des-ofb
3des-cfb
3des-ctr
3des-ofb
blowfish-cfb
blowfish-ctr
blowfish-ofb
cast5-cfb
cast5-ctr
cast5-ofb
twofish-128-cfb
twofish-192-cfb
twofish-256-cfb
twofish-128-ctr
twofish-192-ctr
twofish-256-ctr
twofish-128-ofb
twofish-192-ofb
twofish-256-ofb
tea-cfb
tea-ctr
tea-ofb
xtea-cfb
xtea-ctr
xtea-ofb
rc4-md5
rc4-md5-6
chacha20
chacha20-ietf
salsa20

type IvSizeError added in v0.0.9

type IvSizeError int

IvSizeError iv size error

func (IvSizeError) Error added in v0.0.9

func (i IvSizeError) Error() string

Error implement Error interface

type KeyIvLen added in v0.0.9

type KeyIvLen interface {
	KeyLen() int
	IvLen() int
}

KeyIvLen key and iv length interface

func GetCipher added in v0.0.9

func GetCipher(method string) (KeyIvLen, bool)

GetCipher 根据方法获得 Cipher information

type KeySizeError added in v0.0.9

type KeySizeError int

KeySizeError key size error

func (KeySizeError) Error added in v0.0.9

func (k KeySizeError) Error() string

Error implement Error interface

type Option added in v0.0.9

type Option func(apply Apply)

Option option

func WithGenerateIv added in v0.1.1

func WithGenerateIv(generateIv func(block cipher.Block) ([]byte, error)) Option

WithGenerateIv with custom generate new iv function

func WithNewIv added in v0.0.9

func WithNewIv(generateIv func(block cipher.Block) ([]byte, error)) Option

WithNewIv with custom generate new iv function Deprecated: use WithGenerateIv

type Stream added in v0.0.9

type Stream struct {
	NewStream func(block cipher.Block, iv []byte) cipher.Stream
}

Stream stream newCipher

func (*Stream) New added in v0.0.9

func (sf *Stream) New(key, iv []byte, newCipher func(key []byte) (cipher.Block, error)) (cipher.Stream, error)

New new with newCipher and key,iv

Jump to

Keyboard shortcuts

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