masker

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: MIT Imports: 4 Imported by: 6

README

Go Masker

Simple package to mask sensitive data in Go structs. Wraps and extends go-mask with pre-configured rules and easier setup.

Install

go get github.com/goliatone/go-masker

Usage

Basic struct masking:

type User struct {
    Username string
    Password string `mask:"filled4"`  // Will mask with 4 chars
    APIKey   string `mask:"filled32"` // Will mask with 32 chars
}

user := User{
    Username: "john",
    Password: "secret123",
    APIKey:   "1234567890",
}

// Using default masker
masked, err := masker.Mask(user)

// Output:
// {
//   "username": "john",
//   "password": "****",
//   "api_key": "********************************"
// }

Custom masking:

// Create custom masker
m := masker.New()

// Register custom field mask
m.RegisterMaskField("api_key", "filled32")

// Register custom masking function
m.RegisterMaskStringFunc("custom", func(arg, value string) (string, error) {
    return strings.Repeat("*", len(value)), nil
})

Mask Types

  • hash: SHA1 hash of the string
  • fixed: Fixed 8 char mask
  • filled: Mask with specified length (e.g., filled4, filled32)
  • random: Random number for numeric types
  • zero: Sets to zero value

Default Masked Fields

These come pre-configured:

  • Password/password: 4 chars
  • SigningKey/signing_key: 32 chars
  • Authorization/authorization: 32 chars

Configuration

m := masker.New()

// Change tag name (default: "mask")
m.SetTagName("secure")

// Change mask character (default: "*")
m.SetMaskChar("#")

// Toggle type caching
m.Cache(false)

Supported Types

  • string (MaskStringFunc)
  • uint (MaskUintFunc)
  • int (MaskIntFunc)
  • float64 (MaskFloat64Func)
  • any (MaskAnyFunc)

Features

  • Pre-configured for common sensitive fields
  • Custom masking functions
  • Field-based masking
  • Thread safe
  • Type info caching
  • Supports multiple data types

License

MIT

Copyright (c) 2024 goliatone

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

View Source
const MaskTypePreserveEnds = "preserveEnds"

Variables

This section is empty.

Functions

func Mask

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

func MaskPreserveEnds added in v0.1.0

func MaskPreserveEnds(arg string, value string) (string, error)

Types

type MaskAnyFunc

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

type MaskFloat64Func

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

type MaskIntFunc

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

type MaskStringFunc

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

type MaskUintFunc

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

type Masker

type Masker struct {
	// contains filtered or unexported fields
}
var Default *Masker

func (*Masker) Cache

func (m *Masker) Cache(enable bool)

Cache can be toggled to cache the type information of the struct. default true

func (*Masker) Float64

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

Float64 masks the given argument float64

func (*Masker) Int

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

Int masks the given argument int

func (*Masker) Mask

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

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) MaskChar

func (m *Masker) MaskChar() string

MaskChar returns the current character used for masking.

func (*Masker) MaskFilledString

func (m *Masker) MaskFilledString(arg, value string) (string, error)

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

func (m *Masker) MaskFixedString(arg, value string) (string, error)

MaskFixedString masks with a fixed length (8 characters).

func (*Masker) MaskHashString

func (m *Masker) MaskHashString(arg, value string) (string, error)

MaskHashString masks and hashes (sha1) a string.

func (*Masker) MaskRandomFloat64

func (m *Masker) MaskRandomFloat64(arg string, value float64) (float64, error)

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

func (m *Masker) MaskRandomInt(arg string, value int) (int, error)

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) MaskZero

func (m *Masker) MaskZero(arg string, value any) (any, error)

MaskZero converts the value to its type's zero value.

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

func (m *Masker) RegisterMaskField(fieldName, maskType string)

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

func (m *Masker) SetMaskChar(s string)

SetMaskChar changes the character used for masking

func (*Masker) SetTagName

func (m *Masker) SetTagName(s string)

SetTagName allows you to change the tag name from "mask" to something else.

func (*Masker) String

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

String masks the given argument string

func (*Masker) Uint

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

Uint masks the given argument uint

Jump to

Keyboard shortcuts

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