censored

package module
v0.0.0-...-9b73e0a Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 9 Imported by: 2

README

Go Censored

String field encryption for struct, most used in GORM with MySQL.

Example

Also see censored_test.go.

package main

import "github.com/allape/gocensored"

type CompanyData struct {
	Info1       string `censored:"aes.hex"`
	Info2       string `censored:"aes.base64"`
	Info3       string `censored:"saltyaes.urlbase64"`
	PublicInfo1 string `censored:".hex"`
}

func main() {
	info := CompanyData{
		Info1:       "secret1",
		Info2:       "secret2",
		Info3:       "secret3",
		PublicInfo1: "public1",
	}

	censor, err := censored.NewDefaultCensor(&gocensored.Config{
		Password: []byte("1234_6789"),
	})
	if err != nil {
        panic(err)
    }

	err = censor.Encencor(&info)
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

View Source
const DefaultTagName = "censored"

Variables

This section is empty.

Functions

func WalkThroughStringFields

func WalkThroughStringFields(record any, handle HandleField) error

Types

type AESCensor

type AESCensor struct {
	Codec
}

func (*AESCensor) Decode

func (c *AESCensor) Decode(value, password []byte) ([]byte, error)

func (*AESCensor) Encode

func (c *AESCensor) Encode(value, password []byte) ([]byte, error)

type Censor

type Censor struct {
	Config *Config
	// contains filtered or unexported fields
}

func NewDefaultCensor

func NewDefaultCensor(config *Config) (_ *Censor, err error)

func (*Censor) Decensor

func (r *Censor) Decensor(record any) error

func (*Censor) Encencor

func (r *Censor) Encencor(record any) error

func (*Censor) RegisterCodec

func (r *Censor) RegisterCodec(name CodecName, codec Codec, enforced bool) error

func (*Censor) RegisterTransformer

func (r *Censor) RegisterTransformer(name TransformerName, transformer Transformer, enforced bool) error

type Codec

type Codec interface {
	Encode(value, password []byte) ([]byte, error)
	Decode(value, password []byte) ([]byte, error)
}

type CodecName

type CodecName string

type Config

type Config struct {
	TagName  string
	Password []byte
}

type HandleField

type HandleField func(field reflect.Value, fieldType reflect.StructField) error

type HexTransformer

type HexTransformer struct {
	Transformer
}

func (*HexTransformer) Decode

func (c *HexTransformer) Decode(value string) ([]byte, error)

func (*HexTransformer) Encode

func (c *HexTransformer) Encode(value []byte) (string, error)

type SaltyAESCensor

type SaltyAESCensor struct {
	Codec
}

func (*SaltyAESCensor) Decode

func (c *SaltyAESCensor) Decode(value, password []byte) ([]byte, error)

func (*SaltyAESCensor) Encode

func (c *SaltyAESCensor) Encode(value, password []byte) ([]byte, error)

type StdBase64Transformer

type StdBase64Transformer struct {
	Transformer
}

func (*StdBase64Transformer) Decode

func (c *StdBase64Transformer) Decode(value string) ([]byte, error)

func (*StdBase64Transformer) Encode

func (c *StdBase64Transformer) Encode(value []byte) (string, error)

type TagContent

type TagContent string

func (TagContent) Get

type Transformer

type Transformer interface {
	Encode(value []byte) (string, error)
	Decode(value string) ([]byte, error)
}

type TransformerName

type TransformerName string

type URLBase64Transformer

type URLBase64Transformer struct {
	Transformer
}

func (*URLBase64Transformer) Decode

func (c *URLBase64Transformer) Decode(value string) ([]byte, error)

func (*URLBase64Transformer) Encode

func (c *URLBase64Transformer) Encode(value []byte) (string, error)

Jump to

Keyboard shortcuts

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