xcodec

package
v0.0.0-...-b147793 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: BSD-3-Clause Imports: 19 Imported by: 2

Documentation

Overview

Package xcodec 编解码器

Index

Examples

Constants

This section is empty.

Variables

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

	Raw = NewCodec("raw", rawEncode, rawDecode)
)

Functions

func Convert

func Convert(from any, to any) error

Convert 类型转换

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 Cipher

type Cipher interface {
	Encrypter
	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 NewCodec

func NewCodec(name string, e EncodeFunc, d DecodeFunc) Codec

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 Decrypter2

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

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 Encrypter

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

Encrypter 加密

type Encrypter2

type Encrypter2 interface {
	Encrypter
	ID() []byte
}

type Encrypters

type Encrypters []Encrypter

func (Encrypters) Encrypt

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

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