gmask

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

gmask

gmask is a Go library for masking sensitive information in variable.

Getting gmask

With Go module support, simply add the following import

import "github.com/asjdf/gmask"

to your code, and then go [build|run|test] will automatically fetch the necessary dependencies.

Otherwise, run the following Go command to install the gmask package:

go get -u github.com/asjdf/gmask

Demo

You can get more demo in gmask_test.go

func TestExample(t *testing.T) {
	demo := struct {
		PubInfo        string `json:"pub"`
		StrChar        string `json:"secretChar" mask:"char"`
		StrChar3       string `json:"secretChar8" mask:"char,3"`
		StrCharSameLen string `json:"secretCharSameLen" mask:"char,-1"`
		StrCharDash    string `json:"secretCharSameStr" mask:"char,,-"`
		StrRand        string `json:"secretRand" mask:"rand"`
		StrHash        string `json:"secretHash" mask:"hash"`
		StrZero        string `json:"secretZero" mask:"zero"`
	}{
		PubInfo:        "This field won't be masked",
		StrChar:        "This field will be ********",
		StrChar3:       "This field will be ***",
		StrCharSameLen: "This field will be same length *",
		StrCharDash:    "This field will be --------",
		StrRand:        "This field will be a random string",
		StrHash:        "This field will be a hash string",
		StrZero:        "This field will be a empty string",
	}
	fmt.Printf("%+v\n", demo)
	demoMasked, _ := Mask(demo)
	fmt.Printf("%+v\n", demoMasked)
}

output:

{PubInfo:This field won't be masked StrChar:This field will be ******** StrChar3:This field will be *** StrCharSameLen:This field will be same length * StrCharDash:This field will be -------- StrRand:This field will be a random string StrHash:This field will be a hash string StrZero:This field will be a empty string}
{PubInfo:This field won't be masked StrChar:******** StrChar3:*** StrCharSameLen:******************************** StrCharDash:-------- StrRand:vOvMXGbu StrHash:469e0aae1b45c13042c0f95e4a5bea77a2696bd9b7d8694a6023f1ad1b3479f6 StrZero:}

Available tags

How to Contribute

If you are interested in this project, you can contribute in the following ways:

  • Submitting issues: If you find any problems or have any suggestions, please submit an issue on GitHub.
  • Submitting pull requests: If you have solved a problem or implemented a new feature, please submit a pull request.
  • Improving documentation: We greatly need contributors to improve the documentation. If you find any deficiencies or errors in the documentation, please submit a pull request.

We appreciate any contributions to this project, and we thank you for your support!

Documentation

Index

Constants

View Source
const (
	MaskTypeZero   = "zero"
	MaskTypeChar   = "char"
	MaskTypeRandom = "rand"
	MaskTypeHash   = "hash"
)

Variables

This section is empty.

Functions

func Any

func Any(value any, tag ...string) (hit bool, output any, err error)

func Float64

func Float64(value float64, tag ...string) (float64, error)

func Int

func Int(value int, tag ...string) (int, error)

func Mask

func Mask[T any](target T) (ret T, err error)

func MaskCharString

func MaskCharString(defaultMaskChar ...string) func(value string, arg ...string) (string, error)

MaskCharString will generate a MaskStringFunc which will return the maskChar string with given length in tag, if not set length then will use the default length 8, if set length to -1 the length will equal to original string, if not set maskChar, the maskChar will be *

Example: `mask:"char,[length],[maskChar]"`

func MaskHashString

func MaskHashString(value string, arg ...string) (string, error)

MaskHashString returns the hash of the given string supported algorithms: md5, sha1, sha256 default algorithm is sha256

Example: `mask:"hash,[algorithm]"`

func MaskRandFloat64 added in v1.1.0

func MaskRandFloat64(_ float64, arg ...string) (float64, error)

MaskRandFloat64 returns a new random float64

Example: `mask:"rand,[max],[min],[digit]"`

func MaskRandInt added in v1.1.0

func MaskRandInt(_ int, arg ...string) (int, error)

MaskRandInt returns a new random int

Example: `mask:"rand,[max],[min]"`

func MaskRandString

func MaskRandString(value string, arg ...string) (new string, err error)

MaskRandString returns a random string The default length is 8. If set length to -1, the length of the output will be equal to the length of the input string

Example: `mask:"rand,[length]"`

func MaskRandUint added in v1.1.0

func MaskRandUint(_ uint, arg ...string) (uint, error)

MaskRandUint returns a new random uint if not set max value, the

Example: `mask:"rand,[max],[min]"`

func MaskZero

func MaskZero(value any, _ ...string) (any, error)

MaskZero will return the zero value of the given value

Example: `mask:"zero"`

func String

func String(value string, tag ...string) (string, error)

func Uint

func Uint(value uint, tag ...string) (uint, error)

Types

type MaskAnyFunc

type MaskAnyFunc func(value any, arg ...string) (any, error)

type MaskFloat64Func

type MaskFloat64Func func(value float64, arg ...string) (float64, error)

type MaskIntFunc

type MaskIntFunc func(value int, arg ...string) (int, error)

type MaskStringFunc

type MaskStringFunc func(value string, arg ...string) (string, error)

type MaskUintFunc

type MaskUintFunc func(value uint, arg ...string) (uint, error)

type Masker

type Masker struct {
	// contains filtered or unexported fields
}

func New

func New() *Masker

func (*Masker) Any

func (m *Masker) Any(value any, tag ...string) (hit bool, output any, err error)

func (*Masker) Float64

func (m *Masker) Float64(value float64, tag ...string) (float64, error)

func (*Masker) Int

func (m *Masker) Int(value int, tag ...string) (int, error)

func (*Masker) Mask

func (m *Masker) Mask(target any) (ret any, err error)

func (*Masker) RegMaskAnyFunc

func (m *Masker) RegMaskAnyFunc(maskName string, mask MaskAnyFunc) *Masker

func (*Masker) RegMaskFloat64Func

func (m *Masker) RegMaskFloat64Func(maskName string, mask MaskFloat64Func) *Masker

func (*Masker) RegMaskIntFunc

func (m *Masker) RegMaskIntFunc(maskName string, mask MaskIntFunc) *Masker

func (*Masker) RegMaskStringFunc

func (m *Masker) RegMaskStringFunc(maskName string, mask MaskStringFunc) *Masker

func (*Masker) RegMaskUintFunc

func (m *Masker) RegMaskUintFunc(maskName string, mask MaskUintFunc) *Masker

func (*Masker) String

func (m *Masker) String(value string, tag ...string) (string, error)

func (*Masker) Uint

func (m *Masker) Uint(value uint, tag ...string) (uint, error)

Jump to

Keyboard shortcuts

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