encrypt

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: MIT Imports: 19 Imported by: 9

README

encrypt

加密流

GoDoc Go.Dev reference Build Status codecov Action Status Go Report Card License Tag

s

Installation

    go get github.com/things-go/encrypt

Import:

    import "github.com/things-go/encrypt"

Example

License

This project is under MIT License. See the LICENSE file for the full license text.

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 The key argument should be the Blowfish key, from 1 to 56 bytes.

func NewCast5Cipher

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

NewCast5Cipher new cast5 cipher, The key size should 32

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 The key argument should be the Twofish key, 16, 24 or 32 bytes.

func NewXteaCipher

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

NewXteaCipher new xtea cipher The key argument should be the XTEA key. XTEA only supports 128 bit (16 byte) keys.

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 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

func NewBlockCipher

func NewBlockCipher(key []byte, newCipher func(key []byte) (cipher.Block, error), opts ...BlockOption) (BlockCrypt, error)

default new with newCipher and key, key should be reference newCipher required. newCipher support follow or implement func(key []byte) (cipher.Block, error):

aes
cipher
des
blowfish
cast5
twofish
xtea
tea

support:

cbc(default): cipher.NewCBCEncrypter, cipher.NewCBCDecrypter

func NewStreamCipher

func NewStreamCipher(key []byte, newCipher func(key []byte) (cipher.Block, error), opts ...StreamOption) (BlockCrypt, error)

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

aes
cipher
des
blowfish
cast5
twofish
xtea
tea

block stream cipher support:

cfb(default): cipher.NewCFBEncrypter, cipher.NewCFBDecrypter
ctr: cipher.NewCTR, cipher.NewCTR
ofb: cipher.NewOFB, cipher.NewOFB

type BlockOption

type BlockOption func(bs *blockBlock)

BlockOption option

func WithBlockCodec

func WithBlockCodec(newEncrypt, newDecrypt func(block cipher.Block, iv []byte) cipher.BlockMode) BlockOption

func WithBlockRandIV

func WithBlockRandIV(generateIv func(block cipher.Block) ([]byte, error)) BlockOption

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 StreamOption

type StreamOption func(bs *blockStream)

StreamOption option

func WithStreamCodec

func WithStreamCodec(newEncrypt, newDecrypt func(block cipher.Block, iv []byte) cipher.Stream) StreamOption

func WithStreamRandIV

func WithStreamRandIV(generateIv func(block cipher.Block) ([]byte, error)) StreamOption

Directories

Path Synopsis
_exmaple

Jump to

Keyboard shortcuts

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