Documentation
¶
Overview ¶
Package xcodec 编解码器
Index ¶
- Variables
- func ContentType(c Encoder) (string, error)
- func Convert(from any, to any) error
- func ConvertAs[T any](from any) (to T, err error)
- func Decode(decoder Decoder, content []byte, obj any) error
- func DecodeFromString(dec Decoder, str string, obj any) error
- func EncodeToString(enc Encoder, obj any) (string, error)
- func FlateCompress(src []byte) ([]byte, error)
- func FlateDecompress(src []byte) ([]byte, error)
- func GZipCompress(src []byte) ([]byte, error)
- func GZipDecompress(src []byte) ([]byte, error)
- func JSONString(obj any) string
- func ZLibCompress(src []byte) ([]byte, error)
- func ZLibDecompress(src []byte) ([]byte, error)
- type AesBlock
- type AesOFB
- type Base32
- type Base64
- type CSVCodec
- type Cipher
- type Ciphers
- type Codec
- type DecodeExtra
- type DecodeFunc
- type Decoder
- type DecryptFunc
- type Decrypter
- type Decrypters
- type EncodeFunc
- type Encoder
- type EncryptFunc
- type Encryptor
- type Encryptors
- type FormCodec
- type HEX
- type HasContentType
- type IDDecrypter
- type IDEncryptor
- type Int64Cipher
- func (n *Int64Cipher) Decode(str string) (int64, error)
- func (n *Int64Cipher) DecodeInt64Bytes(str []byte) (int64, error)
- func (n *Int64Cipher) DecodeInt64String(str string) (int64, error)
- func (n *Int64Cipher) Encode(num int64) (string, error)
- func (n *Int64Cipher) EncodeInt64(num int64) string
- func (n *Int64Cipher) EncodeInt64Byte(num int64) []byte
- type TextCodec
- type TranscoderFunc
- type TranscoderFuncs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func ContentType ¶
func DecodeFromString ¶
DecodeFromString 使用 Decoder 将字符串 解码并赋值给 obj,若 obj 本身是字符串类型,则直接赋值
func EncodeToString ¶
EncodeToString 使用 Encoder 将 obj 编码为 字符串,若 obj 本身就是字符串,则直接返回
func FlateCompress ¶
func FlateDecompress ¶
func GZipCompress ¶
func GZipDecompress ¶
func JSONString ¶
func ZLibCompress ¶
func ZLibDecompress ¶
Types ¶
type AesBlock ¶
type AesBlock struct {
// Key 秘钥,必填,若长度为 16, 24, 32 则直接使用,否则使用 md5 值
Key string
// IV 初始化向量,可选,当不为空时,长度应为 16
// 当为空时,会基于 key 生成
IV string
// contains filtered or unexported fields
}
AesBlock AES 加解密
func (*AesBlock) Encrypt ¶
Example ¶
package main
import (
"fmt"
"github.com/xanygo/anygo/xcodec"
)
func main() {
ac := &xcodec.AesBlock{
Key: "demo",
}
str1, _ := ac.Encrypt([]byte("hello"))
fmt.Printf("Encrypt= %q\n", str1)
str2, _ := ac.Decrypt(str1)
fmt.Printf("Decrypt= %q\n", str2)
}
Output: Encrypt= "\xc8\xc4?\xa2\xf3\x00Ͳ\xc1~\xb1\xb7\x96\xe3\xe4\x82" Decrypt= "hello"
type AesOFB ¶
type AesOFB struct {
// Key 必填,加密秘钥,若长度为 16, 24, 32 会直接使用,否则会使用 md5 值
Key string
// IV 初始化向量,可选,当不为空时,长度应为 16
// 当为空时,会基于 key 生成
IV string
// contains filtered or unexported fields
}
func (*AesOFB) Encrypt ¶
Example ¶
package main
import (
"fmt"
"github.com/xanygo/anygo/xcodec"
)
func main() {
ac := &xcodec.AesOFB{
Key: "demo",
}
str1, _ := ac.Encrypt([]byte("hello"))
fmt.Printf("Encrypt= %q\n", str1)
str2, _ := ac.Decrypt(str1)
fmt.Printf("Decrypt= %q\n", str2)
}
Output: Encrypt= "2\xa0\x1c\x90\xb4" Decrypt= "hello"
type Cipher ¶
func NewCipher ¶
func NewCipher(enc EncryptFunc, dec DecryptFunc) Cipher
type Ciphers ¶
type Ciphers []Cipher
Ciphers 多个 Cipher 的组合。 可以联合在一起链式工作,在 Encrypt 的时候,会依次正序调用。在 Decrypt 的时候,会依次倒序调用。
type Codec ¶
func CodecWithCipher ¶
func NewCodec ¶
func NewCodec(name string, e EncodeFunc, d DecodeFunc, ct string) Codec
type DecodeExtra ¶
type DecodeExtra interface {
// NeedDecodeExtra 存储未定义字段的字段名,返回非空为有效。
// 并且返回的名字必须在 struct 中存在,而且必须是 map[string]any 类型
NeedDecodeExtra() string
}
DecodeExtra 当被解析的对象,实现了此接口的时候,并且 NeedDecodeExtra 返回了有效的字段名, 则会将未在 struct 中定义的字段,全部解析到指定的字段里。
type DecodeFunc ¶
type Decoder ¶
func DecoderWithTranscoder ¶
func DecoderWithTranscoder(dec Decoder, trans TranscoderFunc) Decoder
type DecryptFunc ¶
type Decrypters ¶
type Decrypters []Decrypter
type EncodeFunc ¶
type Encoder ¶
func EncoderWithTranscoder ¶
func EncoderWithTranscoder(enc Encoder, trans TranscoderFunc) Encoder
type EncryptFunc ¶
type Encryptors ¶
type Encryptors []Encryptor
type HasContentType ¶
type HasContentType interface {
ContentType() string
}
type IDDecrypter ¶
type IDEncryptor ¶
type Int64Cipher ¶
type Int64Cipher struct {
// Cipher 必填 加密套件
Cipher Cipher
// Int64Encoder 可选编码器,默认为 xbase.Base62
Int64Encoder *xbase.Encoding
}
Int64Cipher 将 int64 加密为字符串的算法
func (*Int64Cipher) DecodeInt64Bytes ¶
func (n *Int64Cipher) DecodeInt64Bytes(str []byte) (int64, error)
func (*Int64Cipher) DecodeInt64String ¶
func (n *Int64Cipher) DecodeInt64String(str string) (int64, error)
func (*Int64Cipher) Encode ¶
func (n *Int64Cipher) Encode(num int64) (string, error)
Example ¶
package main
import (
"fmt"
"github.com/xanygo/anygo/xcodec"
)
func main() {
ac := &xcodec.Int64Cipher{
Cipher: &xcodec.AesOFB{
Key: "demo",
},
}
nums := []int64{0, 1, 1000, 10000, 99999999}
for _, num := range nums {
str1, _ := ac.Encode(num)
fmt.Printf("Encode(%d) = %q\n", num, str1)
num1, _ := ac.Decode(str1)
fmt.Printf("Decode(%q) = %d\n\n", str1, num1)
}
}
Output: Encode(0) = "i1" Decode("i1") = 0 Encode(1) = "j1" Decode("j1") = 1 Encode(1000) = "kY6" Decode("kY6") = 1000 Encode(10000) = "E4P5" Decode("E4P5") = 10000 Encode(99999999) = "v24ZYJ2" Decode("v24ZYJ2") = 99999999
func (*Int64Cipher) EncodeInt64 ¶
func (n *Int64Cipher) EncodeInt64(num int64) string
func (*Int64Cipher) EncodeInt64Byte ¶
func (n *Int64Cipher) EncodeInt64Byte(num int64) []byte
type TextCodec ¶
type TextCodec struct{}
type TranscoderFunc ¶
type TranscoderFuncs ¶
func (TranscoderFuncs) Transcoding ¶
func (ts TranscoderFuncs) Transcoding(data []byte) (result []byte, err error)
Source Files
¶
Click to show internal directories.
Click to hide internal directories.