vmask

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaskKeepLen is the default number of characters to keep from the beginning of the string
	MaskKeepLen = 8

	// MaskPattern is the default mask pattern for masking
	MaskPattern = "*****"
)

Variables

View Source
var (
	// Mask is the global masker instance for handling secret masking
	Mask = masker.NewMaskerMarshaler()
)
View Source
var MaskerMapDefault = (&MaskerMap{}).Default()

Mask is the global instance of MaskerMap with default configuration

Functions

func MaskMap

func MaskMap(input map[string]any) map[string]any

MaskMap masks sensitive information in a map using the default configuration

func MaskSecret

func MaskSecret(val string, keepLen int, maskPattern string) string

MaskSecret masks a string while preserving a specified number of leading characters

val: the input string to mask keepLen: number of leading characters to preserve maskPattern: the pattern to use for masking

Return:

  • If input length is less than keepLen, preserves all available characters
  • Otherwise, preserves keepLen characters and appends the mask pattern

func MaskStruct

func MaskStruct(val any) any

MaskStruct masks sensitive fields in a struct It is a wrapper function that provides a simpler interface for masking operations by handling error logging internally.

val: The input struct or map to be masked

Return: The masked struct with sensitive fields replaced by mask patterns

Example:

type User struct {
    Password string `mask:"secret"`
}
user := &User{Password: "123456"}
masked := MaskStruct(user)
// Result: &User{Password: "12345*****"}

func MaskStructAndMap

func MaskStructAndMap(s any) (any, error)

MaskStructAndMap masks sensitive fields in a struct or map maskKey specifies the keys to be masked, separated by |

func RegisterSecretMaskers

func RegisterSecretMaskers(minLen, maxLen int, pattern string)

RegisterSecretMaskers registers secret maskers with configurable options

minLen: minimum number of characters to keep (inclusive) maxLen: maximum number of characters to keep (inclusive) pattern: the pattern to use for masking

This function registers: 1. A default masker with 8 characters preserved 2. Additional maskers for each length from minLen to maxLen

Types

type MaskerMap

type MaskerMap struct {
	// KeepLen specifies how many characters to keep from the beginning of sensitive values
	KeepLen int
	// MaskPattern is the pattern used to replace sensitive information
	MaskPattern string
	// SensitiveKeys contains keywords that indicate sensitive fields
	SensitiveKeys []string
}

MaskerMap represents a configurable map masking utility It provides methods to mask sensitive information in maps and slices

func (*MaskerMap) Default

func (m *MaskerMap) Default() *MaskerMap

Default returns a MaskerMap with default configuration

func (*MaskerMap) MaskMap

func (m *MaskerMap) MaskMap(input map[string]any) map[string]any

MaskMap masks sensitive information in a map using the current configuration It processes nested maps and slices recursively

func (*MaskerMap) WithKeepLen

func (m *MaskerMap) WithKeepLen(keepLen int) *MaskerMap

WithKeepLen sets the number of characters to keep from sensitive values Returns the MaskerMap instance for method chaining

func (*MaskerMap) WithMaskPattern

func (m *MaskerMap) WithMaskPattern(pattern string) *MaskerMap

WithMaskPattern sets the pattern to use for masking Returns the MaskerMap instance for method chaining

func (*MaskerMap) WithSensitiveKeys

func (m *MaskerMap) WithSensitiveKeys(keys []string) *MaskerMap

WithSensitiveKeys sets the sensitive keywords to detect Returns the MaskerMap instance for method chaining

type MaskerSecret

type MaskerSecret struct {
	KeepLen     int    // Number of leading characters to preserve
	MaskPattern string // Mask pattern to append after preserved characters
}

MaskerSecret implements secret masking with configurable prefix preservation

The masker replaces sensitive content with a pattern while keeping a prefix: - By default keeps 5 characters (when using "secret" mask type) - Supports variable length prefixes using "secret:N" format (5 ≤ N ≤ 12)

Examples:

MaskType: "secret"    -> Keep 5 chars + mask pattern (e.g. "12345*****")
MaskType: "secret:8"  -> Keep 8 chars + mask pattern (e.g. "12345678*****")

Note: If input length is shorter than KeepLen, shows full available prefix

func (*MaskerSecret) Default

func (m *MaskerSecret) Default() *MaskerSecret

Default returns a MaskerSecret with default values Default configuration: - KeepLen: 8 characters - MaskPattern: "*****"

func (*MaskerSecret) Marshal

func (m *MaskerSecret) Marshal(s string, val string) string

Marshal implements the masker.Marshaler interface It masks the input string while preserving the specified number of leading characters

func (*MaskerSecret) WithKeepLen

func (m *MaskerSecret) WithKeepLen(keepLen int) *MaskerSecret

WithKeepLen sets the number of characters to keep from the beginning of the string Returns the MaskerSecret instance for method chaining

func (*MaskerSecret) WithMaskPattern

func (m *MaskerSecret) WithMaskPattern(pattern string) *MaskerSecret

WithMaskPattern sets the pattern to use for masking Returns the MaskerSecret instance for method chaining

Jump to

Keyboard shortcuts

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