xcodec

package
v0.0.0-...-7424bab Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: BSD-3-Clause Imports: 25 Imported by: 3

Documentation

Overview

Package xcodec 编解码器

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	JSON = NewCodec("json", json.Marshal, json.Unmarshal, "application/json")

	Raw = NewCodec("raw", rawEncode, rawDecode, "application/octet-stream")

	Form = &FormCodec{}

	CSV = CSVCodec{}

	Text = TextCodec{}
)

Functions

func ContentType

func ContentType(c Encoder) (string, error)

func Convert

func Convert(from any, to any) error

Convert 类型转换(当前使用 JSON 做中间转换,性能一般)

func ConvertAs

func ConvertAs[T any](from any) (to T, err error)

ConvertAs 类型转换(当前使用 JSON 做中间转换,性能一般)

func Decode

func Decode(decoder Decoder, content []byte, obj any) error

func DecodeFromString

func DecodeFromString(dec Decoder, str string, obj any) error

DecodeFromString 使用 Decoder 将字符串 解码并赋值给 obj,若 obj 本身是字符串类型,则直接赋值

func EncodeToString

func EncodeToString(enc Encoder, obj any) (string, error)

EncodeToString 使用 Encoder 将 obj 编码为 字符串,若 obj 本身就是字符串,则直接返回

func FlateCompress

func FlateCompress(src []byte) ([]byte, error)

func FlateDecompress

func FlateDecompress(src []byte) ([]byte, error)

func GZipCompress

func GZipCompress(src []byte) ([]byte, error)

func GZipDecompress

func GZipDecompress(src []byte) ([]byte, error)

func JSONString

func JSONString(obj any) string

func ZLibCompress

func ZLibCompress(src []byte) ([]byte, error)

func ZLibDecompress

func ZLibDecompress(src []byte) ([]byte, error)

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

func (a *AesBlock) Decrypt(src []byte) ([]byte, error)

func (*AesBlock) Encrypt

func (a *AesBlock) Encrypt(src []byte) ([]byte, error)
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"

func (*AesBlock) ID

func (a *AesBlock) ID() []byte

type AesOFB

type AesOFB struct {
	// Key 必填,加密秘钥,若长度为 16, 24, 32 会直接使用,否则会使用 md5 值
	Key string

	// IV 初始化向量,可选,当不为空时,长度应为 16
	// 当为空时,会基于 key 生成
	IV string
	// contains filtered or unexported fields
}

func (*AesOFB) Decrypt

func (a *AesOFB) Decrypt(src []byte) ([]byte, error)

func (*AesOFB) Encrypt

func (a *AesOFB) Encrypt(src []byte) ([]byte, error)
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"

func (*AesOFB) ID

func (a *AesOFB) ID() []byte

type Base32

type Base32 struct {
	Encoder *base32.Encoding
}

func (Base32) Decrypt

func (b Base32) Decrypt(src []byte) ([]byte, error)

func (Base32) Encrypt

func (b Base32) Encrypt(src []byte) ([]byte, error)

type Base64

type Base64 struct {
	Encoder *base64.Encoding
}

func (Base64) Decrypt

func (b Base64) Decrypt(src []byte) ([]byte, error)

func (Base64) Encrypt

func (b Base64) Encrypt(src []byte) ([]byte, error)

type CSVCodec

type CSVCodec struct {
}

func (CSVCodec) ContentType

func (c CSVCodec) ContentType() string

func (CSVCodec) Decode

func (c CSVCodec) Decode(b []byte, a any) error

func (CSVCodec) Encode

func (c CSVCodec) Encode(a any) ([]byte, error)

func (CSVCodec) Name

func (c CSVCodec) Name() string

type Cipher

type Cipher interface {
	Encryptor
	Decrypter
}

func NewCipher

func NewCipher(enc EncryptFunc, dec DecryptFunc) Cipher

type Ciphers

type Ciphers []Cipher

Ciphers 多个 Cipher 的组合。 可以联合在一起链式工作,在 Encrypt 的时候,会依次正序调用。在 Decrypt 的时候,会依次倒序调用。

func (Ciphers) Decrypt

func (cs Ciphers) Decrypt(src []byte) (out []byte, err error)

func (Ciphers) Encrypt

func (cs Ciphers) Encrypt(src []byte) (out []byte, err error)

type Codec

type Codec interface {
	Name() string
	Encoder
	Decoder
}

func CodecWithCipher

func CodecWithCipher(coder Codec, c Cipher) Codec

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 DecodeFunc func([]byte, any) error

func (DecodeFunc) Decode

func (d DecodeFunc) Decode(v []byte, r any) error

type Decoder

type Decoder interface {
	Decode([]byte, any) error
}

func DecoderWithTranscoder

func DecoderWithTranscoder(dec Decoder, trans TranscoderFunc) Decoder

type DecryptFunc

type DecryptFunc func([]byte) ([]byte, error)

func (DecryptFunc) Decrypt

func (fn DecryptFunc) Decrypt(src []byte) ([]byte, error)

type Decrypter

type Decrypter interface {
	Decrypt(src []byte) ([]byte, error)
}

Decrypter 解密

type Decrypters

type Decrypters []Decrypter

func (Decrypters) Decrypt

func (ds Decrypters) Decrypt(src []byte) (out []byte, err error)

type EncodeFunc

type EncodeFunc func(any) ([]byte, error)

func (EncodeFunc) Encode

func (e EncodeFunc) Encode(v any) ([]byte, error)

type Encoder

type Encoder interface {
	Encode(any) ([]byte, error)
}

func EncoderWithTranscoder

func EncoderWithTranscoder(enc Encoder, trans TranscoderFunc) Encoder

type EncryptFunc

type EncryptFunc func([]byte) ([]byte, error)

func (EncryptFunc) Encrypt

func (fn EncryptFunc) Encrypt(src []byte) ([]byte, error)

type Encryptor

type Encryptor interface {
	Encrypt(src []byte) ([]byte, error)
}

Encryptor 加密

type Encryptors

type Encryptors []Encryptor

func (Encryptors) Encrypt

func (es Encryptors) Encrypt(src []byte) (out []byte, err error)

type FormCodec

type FormCodec struct {
}

func (FormCodec) ContentType

func (f FormCodec) ContentType() string

func (FormCodec) Decode

func (f FormCodec) Decode(bf []byte, a any) error

func (FormCodec) Encode

func (f FormCodec) Encode(a any) ([]byte, error)

func (FormCodec) Name

func (f FormCodec) Name() string

type HEX

type HEX struct{}

func (HEX) Decrypt

func (h HEX) Decrypt(src []byte) ([]byte, error)

func (HEX) Encrypt

func (h HEX) Encrypt(src []byte) ([]byte, error)

type HasContentType

type HasContentType interface {
	ContentType() string
}

type IDDecrypter

type IDDecrypter interface {
	Decrypter
	ID() []byte
}

type IDEncryptor

type IDEncryptor interface {
	Encryptor
	ID() []byte
}

type Int64Cipher

type Int64Cipher struct {
	// Cipher 必填 加密套件
	Cipher Cipher

	// Int64Encoder 可选编码器,默认为 xbase.Base62
	Int64Encoder *xbase.Encoding
}

Int64Cipher 将 int64 加密为字符串的算法

func (*Int64Cipher) Decode

func (n *Int64Cipher) Decode(str string) (int64, error)

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

func (TextCodec) Decode

func (t TextCodec) Decode(bytes []byte, obj any) error

Decode 将bytes 解析到 obj,具体规则如下:

  1. obj 实现了 TextUnmarshaler ,则优先使用
  2. 若 obj 是 string 或者 []byte 类型,则直接赋值
  3. 若 obj 是基础类型,如 number、bool 类型,则尝试解析赋值
  4. 返回错误

func (TextCodec) Encode

func (t TextCodec) Encode(obj any) ([]byte, error)

func (TextCodec) Name

func (t TextCodec) Name() string

type TranscoderFunc

type TranscoderFunc func([]byte) ([]byte, error)

func (TranscoderFunc) AsWriter

func (fn TranscoderFunc) AsWriter(out io.Writer) io.Writer

type TranscoderFuncs

type TranscoderFuncs []func([]byte) ([]byte, error)

func (TranscoderFuncs) AsWriter

func (ts TranscoderFuncs) AsWriter(out io.Writer) io.Writer

func (TranscoderFuncs) Transcoding

func (ts TranscoderFuncs) Transcoding(data []byte) (result []byte, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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