github.com/wfusion/gofusion@v1.1.14/common/utils/encode/types.go (about) 1 package encode 2 3 import ( 4 "errors" 5 "io" 6 7 "github.com/wfusion/gofusion/common/utils" 8 "github.com/wfusion/gofusion/common/utils/cipher" 9 "github.com/wfusion/gofusion/common/utils/clone" 10 "github.com/wfusion/gofusion/common/utils/compress" 11 ) 12 13 const ( 14 RndSeed int64 = -6544809196299914340 15 ) 16 17 var ( 18 ErrUnknownAlgorithm = errors.New("unknown encode algorithm") 19 ErrEncodeMethodNotFound = errors.New("not found encode method") 20 21 defaultBufferSize = 4 * 1024 // 4kb 22 ) 23 24 type Streamable interface { 25 Encode(dst io.Writer, src io.Reader) (n int64, err error) 26 Decode(dst io.Writer, src io.Reader) (n int64, err error) 27 } 28 29 type Codecable interface { 30 Encode(opts ...utils.OptionExtender) Codecable 31 Decode(opts ...utils.OptionExtender) Codecable 32 ToBytes() (dst []byte, err error) 33 ToString() (dst string, err error) 34 } 35 36 type option struct { 37 key, iv []byte 38 cipherMode cipher.Mode 39 cipherAlgo cipher.Algorithm 40 41 compressAlgo compress.Algorithm 42 printableAlgo Algorithm 43 } 44 45 func Cipher(algo cipher.Algorithm, mode cipher.Mode, key, iv []byte) utils.OptionFunc[option] { 46 return func(o *option) { 47 o.cipherAlgo = algo 48 o.cipherMode = mode 49 o.key = clone.SliceComparable(key) 50 o.iv = clone.SliceComparable(iv) 51 } 52 } 53 54 func Compress(algo compress.Algorithm) utils.OptionFunc[option] { 55 return func(o *option) { 56 o.compressAlgo = algo 57 } 58 } 59 60 func Encode(algo Algorithm) utils.OptionFunc[option] { 61 return func(o *option) { 62 o.printableAlgo = algo 63 } 64 }