encrypt

package
v0.0.0-...-49a3824 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: Apache-2.0 Imports: 17 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCipherUninitialized = errors.New("cipher needs initialize before encrypt/decrypt")
	ErrCapcityNotEnough    = errors.New("slice capcity is not enough")
	ErrAgain               = errors.New("require more data")
	ErrEmptyPassword       = errors.New("empty password")
	ErrUnsupportedMethod   = errors.New("unsupported encryption method")
)

Error type in encrypt packet

View Source
var (
	// NonceSize defined the largest number nonce can reach, once reached, the nonce will warpped
	NonceSize = 12
)

Functions

func CheckCipherMethod

func CheckCipherMethod(method string) error

CheckCipherMethod checks if the cipher method is supported

func HKDFSha1

func HKDFSha1(key, salt, dest []byte)

HKDFSha1 get the key for encyptor

Types

type Cipher

type Cipher interface {
	KeySize() int       // KeySize return the key length which cipher request
	InitBolckSize() int // InitBolckSize give out the size of init block for cipher init, also called as aslt or iv
	Copy() Cipher       // Copy return the copy of the cipher with given password. Need to inititalze before use
	//Close()                                // Close the cipher and release the resource
	Encrypt(src, dest []byte) (int, error) // Encrypt data from src to dest, return the bytes after encryption
	Decrypt(src, dest []byte) (int, error) // Decrypt data from src to dest, return the bytes after decryption
	Pack(src, dest []byte) (int, error)    // Pack will encrypt the src data block to dest and return the bytes encrypted, ONLY USED FOR UDP
	Unpack(src, dest []byte) (int, error)  // Unpack decrypt the src data block to dest, return the bytes decrypted, ONLY USED FOR UDP
	EncryptorInited() bool                 // EncryptorInited return wether encryptor is initalized
	InitEncryptor() ([]byte, error)        // InitEncryptor init the encryptor and return the randomly generated InintBolck
	DecryptorInited() bool                 // DecryptorInited return wether decryptor is initialized
	InitDecryptor(data []byte) error       // InitDecryptor initialize the decryptor with given initBolck
}

Cipher is the encdyptor and decryptor

func NewAEADCipher

func NewAEADCipher(method, password string) (c Cipher, err error)

NewAEADCipher creates a cipher that can be used in Dial() etc. Use cipher.Copy() to create a new cipher with the same method and password to avoid the cost of repeated cipher initialization.

func NewStreamCipher

func NewStreamCipher(method, password string) (c Cipher, err error)

NewStreamCipher creates a cipher that can be used in Dial() etc. Use cipher.Copy() to create a new cipher with the same method and password to avoid the cost of repeated cipher initialization.

func PickCipher

func PickCipher(method, passwd string) (Cipher, error)

PickCipher return the uninitialized cipher with given passwd

type DecOrEnc

type DecOrEnc int

DecOrEnc type, encrypt or decrypt, used when create cipher

const (
	// Decrypt as its name
	Decrypt DecOrEnc = iota
	// Encrypt as its name
	Encrypt
)

Jump to

Keyboard shortcuts

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