peekmask

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 2 Imported by: 0

README

peekmask

A simple Go library for masking parts of strings with full UTF-8 support (including Japanese and emoji).

Features

  • Mask strings while keeping prefix and suffix visible
  • Full UTF-8 support (Japanese, emoji, and other multi-byte characters)
  • Customizable mask character
  • Minimum mask ratio setting (for security)
  • Simple and easy-to-use API

Installation

go get github.com/SpringMT/peekmask

Usage

Simple Usage

Mask a string with default settings (use * as mask):

package main

import (
    "fmt"
    "github.com/SpringMT/peekmask"
)

func main() {
    masked := peekmask.Mask("1234567890", 2, 3)
    fmt.Println(masked) // Output: 12*****890
    
    masked = peekmask.Mask("メールアドレス", 2, 2)
    fmt.Println(masked) // Output: メー***レス
}
Custom Configuration

Create a Masker with custom settings:

package main

import (
    "fmt"
    "github.com/SpringMT/peekmask"
)

func main() {
    // Create a Masker with custom settings
    masker := peekmask.New(
        '■',  // Mask character
        3,    // Number of characters to keep from the start
        3,    // Number of characters to keep from the end
        0.4,  // Minimum mask ratio (40%)
    )
    
    masked := masker.Mask("1234567890")
    fmt.Println(masked) // Output: 123■■■■890
    
    masked = masker.Mask("こんにちは世界")
    fmt.Println(masked) // Output: ■■■■■■■
}

API

Mask(s string, prefixLength, suffixLength int) string

Masks a string using default settings.

  • MaskChar: '*'
  • MinMaskRatio: 0.3
type Masker

A struct that holds the configuration for masking.

  • MaskChar: The character used for masking (e.g., '*', '■')
  • PrefixLength: Number of characters to keep from the start
  • SuffixLength: Number of characters to keep from the end
  • MinMaskRatio: Minimum masking ratio (0.0-1.0)
New(maskChar rune, prefixLength, suffixLength int, minMaskRatio float64) *Masker

Creates a new Masker with custom settings.

(m *Masker) Mask(s string) string

Masks a string based on the configuration.

About Minimum Mask Ratio

MinMaskRatio ensures that if the masking ratio falls below the specified value, all characters will be masked instead.

Example:

masker := peekmask.New('*', 4, 4, 0.5)
// For string "1234567890" (10 characters)
// Masked characters: 10 - 4 - 4 = 2 characters
// Mask ratio: 2/10 = 0.2 (20%)
// Since 0.2 < 0.5, all characters will be masked
masked := masker.Mask("1234567890")
fmt.Println(masked) // Output: **********

This ensures that even with short strings or configurations that would mask only a few characters, the information is still protected.

Testing

go test -v

Benchmarks:

go test -bench=.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mask

func Mask(s string, prefixLength, suffixLength int) string

Types

type Masker

type Masker struct {
	// MaskChar is the character used for masking
	MaskChar rune
	// PrefixLength is the number of characters to keep from the start
	PrefixLength int
	// SuffixLength is the number of characters to keep from the end
	SuffixLength int
	// MinMaskRatio is the minimum masking ratio (0.0-1.0)
	// If the masking ratio falls below this value, all characters will be masked
	MinMaskRatio float64
}

Masker holds the configuration for masking strings

func Default

func Default() *Masker

Default returns a Masker with default settings MaskChar: '*', PrefixLength: 2, SuffixLength: 2, MinMaskRatio: 0.3

func New

func New(maskChar rune, prefixLength, suffixLength int, minMaskRatio float64) *Masker

New creates a new Masker with the specified configuration

func (*Masker) Mask

func (m *Masker) Mask(s string) string

Mask masks the given string

Jump to

Keyboard shortcuts

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