Documentation
¶
Index ¶
- Constants
- Variables
- func MaskMap(input map[string]any) map[string]any
- func MaskSecret(val string, keepLen int, maskPattern string) string
- func MaskStruct(val any) any
- func MaskStructAndMap(s any) (any, error)
- func RegisterSecretMaskers(minLen, maxLen int, pattern string)
- type MaskerMap
- type MaskerSecret
Constants ¶
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 ¶
var ( // Mask is the global masker instance for handling secret masking Mask = masker.NewMaskerMarshaler() )
var MaskerMapDefault = (&MaskerMap{}).Default()
Mask is the global instance of MaskerMap with default configuration
Functions ¶
func MaskSecret ¶
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 ¶
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 ¶
MaskStructAndMap masks sensitive fields in a struct or map maskKey specifies the keys to be masked, separated by |
func RegisterSecretMaskers ¶
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) MaskMap ¶
MaskMap masks sensitive information in a map using the current configuration It processes nested maps and slices recursively
func (*MaskerMap) WithKeepLen ¶
WithKeepLen sets the number of characters to keep from sensitive values Returns the MaskerMap instance for method chaining
func (*MaskerMap) WithMaskPattern ¶
WithMaskPattern sets the pattern to use for masking Returns the MaskerMap instance for method chaining
func (*MaskerMap) WithSensitiveKeys ¶
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