encrypt

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 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

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

NewBlowfishCipher new blowfish cipher

func NewCast5Cipher

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

NewCast5Cipher new cast5 cipher

func NewChacha20

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

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

func NewRc4Md5

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

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

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

func NewStream

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

NewStream new stream

func NewTwofishCipher

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

NewTwofishCipher new twofish cipher

func NewXteaCipher

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

NewXteaCipher new xtea cipher

func PCKSPadding

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

PCKSPadding PKCS#5和PKCS#7 填充

func PCKSUnPadding

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

PCKSUnPadding PKCS#5和PKCS#7 解填充

func RandIV

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

RandIV generate rand iv by rand.Reader

func Valid

func Valid(method, password string) bool

Valid method password is valid or not

Types

type Apply

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

Apply apply

type BlockCrypt

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

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

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

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

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

type IvSizeError int

IvSizeError iv size error

func (IvSizeError) Error

func (i IvSizeError) Error() string

Error implement Error interface

type KeyIvLen

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

KeyIvLen key and iv length interface

func GetCipher

func GetCipher(method string) (KeyIvLen, bool)

GetCipher 根据方法获得 Cipher information

type KeySizeError

type KeySizeError int

KeySizeError key size error

func (KeySizeError) Error

func (k KeySizeError) Error() string

Error implement Error interface

type Option

type Option func(apply Apply)

Option option

func WithGenerateIv

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

WithGenerateIv with custom generate new iv function

func WithNewIv

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

WithNewIv with custom generate new iv function Deprecated: use WithGenerateIv

type Stream

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

Stream stream newCipher

func (*Stream) New

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