Documentation
¶
Overview ¶
Package masker provides a flexible data masking system for Go applications, allowing you to protect sensitive information in various data types.
The package wraps and extends github.com/showa-93/go-mask functionality, providing additional convenience methods and pre-configured masking rules for common sensitive fields.
By default, the package initializes with common masking rules for sensitive fields:
- Password/password: 4 character mask
- SigningKey/signing_key: 32 character mask
- Authorization/authorization: 32 character mask
Basic usage with the default masker:
type User struct {
Username string
Password string `mask:"filled4"`
}
user := User{
Username: "john",
Password: "secret123",
}
masked, err := masker.Mask(user)
if err != nil {
log.Fatal(err)
}
Available Masking Types:
- hash: Masks and hashes (sha1) a string
- fixed: Masks with a fixed length (8 characters)
- filled: Masks the entire string or with specified length
- random: For numeric types, generates random values within range
- zero: Sets the field to its zero value
Custom Masking:
m := &Masker{...}
m.RegisterMaskStringFunc("custom", func(arg, value string) (string, error) {
return strings.Repeat("*", len(value)), nil
})
Field Registration:
m := &Masker{...}
m.RegisterMaskField("api_key", "filled32")
Configuration:
The package supports various configuration options:
m.SetTagName("mask") // Change the struct tag name
m.SetMaskChar("*") // Change the masking character
m.Cache(true) // Enable/disable type information caching
Supported Types:
- string (MaskStringFunc)
- uint (MaskUintFunc)
- int (MaskIntFunc)
- float64 (MaskFloat64Func)
- any (MaskAnyFunc)
Thread Safety:
The package maintains a global Default masker instance that is safe for concurrent use after initialization. Custom masker instances should be handled with appropriate synchronization if modified after initialization.
Performance:
Type information caching is enabled by default to improve performance. For dynamic structures where fields change frequently, caching can be disabled using the Cache(false) method.
For additional information and updates, visit: https://github.com/goliatone/go-masker
Index ¶
- Constants
- func Mask[T any](target T) (ret T, err error)
- func MaskPreserveEnds(arg string, value string) (string, error)
- type MaskAnyFunc
- type MaskFloat64Func
- type MaskIntFunc
- type MaskStringFunc
- type MaskUintFunc
- type Masker
- func (m *Masker) Cache(enable bool)
- func (m *Masker) Float64(tag string, value float64) (float64, error)
- func (m *Masker) Int(tag string, value int) (int, error)
- func (m *Masker) Mask(target any) (ret any, err error)
- func (m *Masker) MaskChar() string
- func (m *Masker) MaskFilledString(arg, value string) (string, error)
- func (m *Masker) MaskFixedString(arg, value string) (string, error)
- func (m *Masker) MaskHashString(arg, value string) (string, error)
- func (m *Masker) MaskRandomFloat64(arg string, value float64) (float64, error)
- func (m *Masker) MaskRandomInt(arg string, value int) (int, error)
- func (m *Masker) MaskZero(arg string, value any) (any, error)
- func (m *Masker) RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
- func (m *Masker) RegisterMaskField(fieldName, maskType string)
- func (m *Masker) RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
- func (m *Masker) RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
- func (m *Masker) RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
- func (m *Masker) RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
- func (m *Masker) SetMaskChar(s string)
- func (m *Masker) SetTagName(s string)
- func (m *Masker) String(tag, value string) (string, error)
- func (m *Masker) Uint(tag string, value uint) (uint, error)
Constants ¶
const MaskTypePreserveEnds = "preserveEnds"
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Masker ¶
type Masker struct {
// contains filtered or unexported fields
}
var Default *Masker
func (*Masker) Cache ¶
Cache can be toggled to cache the type information of the struct. default true
func (*Masker) Mask ¶
Mask returns an object with the mask applied to any given object. The function's argument can accept any type, including pointer, map, and slice types, in addition to struct.
func (*Masker) MaskFilledString ¶
MaskFilledString masks the string length of the value with the same length. If you pass a number like "2" to arg, it masks with the length of the number.(**)
func (*Masker) MaskFixedString ¶
MaskFixedString masks with a fixed length (8 characters).
func (*Masker) MaskHashString ¶
MaskHashString masks and hashes (sha1) a string.
func (*Masker) MaskRandomFloat64 ¶
MaskRandomFloat64 converts a float64 to a random number. For example, if you pass "100.3" to arg, it sets a random number in the range of 0.000 to 99.999.
func (*Masker) MaskRandomInt ¶
MaskRandomInt converts an integer (int) into a random number. For example, if you pass "100" as the arg, it sets a random number in the range of 0-99.
func (*Masker) RegisterMaskAnyFunc ¶
func (m *Masker) RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
RegisterMaskAnyFunc registers a masking function that can be applied to any type. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskField ¶
RegisterMaskField allows you to register a mask tag to be applied to the value of a struct field or map key that matches the fieldName. If a mask tag is set on the struct field, it will take precedence.
func (*Masker) RegisterMaskFloat64Func ¶
func (m *Masker) RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
RegisterMaskFloat64Func registers a masking function for float64 values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskIntFunc ¶
func (m *Masker) RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
RegisterMaskIntFunc registers a masking function for int values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskStringFunc ¶
func (m *Masker) RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
RegisterMaskStringFunc registers a masking function for string values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskUintFunc ¶
func (m *Masker) RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
RegisterMaskUintFunc registers a masking function for uint values. The function will be applied when the uint slice set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) SetMaskChar ¶
SetMaskChar changes the character used for masking
func (*Masker) SetTagName ¶
SetTagName allows you to change the tag name from "mask" to something else.