https://github.com/ggwhite/go-masker
Simple utility of creating a mask for sensitive information
https://github.com/ggwhite/go-masker
go golang golang-tools masker
Last synced: 2 months ago
JSON representation
Simple utility of creating a mask for sensitive information
- Host: GitHub
- URL: https://github.com/ggwhite/go-masker
- Owner: ggwhite
- License: mit
- Created: 2019-04-03T03:18:29.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-11-26T06:51:59.000Z (7 months ago)
- Last Synced: 2026-03-04T13:08:27.754Z (4 months ago)
- Topics: go, golang, golang-tools, masker
- Language: Go
- Homepage: https://godoc.org/github.com/ggwhite/go-masker
- Size: 84 KB
- Stars: 123
- Watchers: 4
- Forks: 35
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Masker v2
[](https://github.com/ggwhite/go-masker/actions)
[](https://godoc.org/github.com/ggwhite/go-masker)
[](https://goreportcard.com/report/github.com/ggwhite/go-masker)
[](https://github.com/ggwhite/go-masker/blob/master/LICENSE)
[](https://github.com/ggwhite/go-masker/releases/latest)
Go Masker v2 is a simple and extensible library for masking sensitive data in Go structs. Use struct tags to control how fields are masked — passwords, emails, IDs, credit cards, and more.
* [Install](#install)
* [Quick Start](#quick-start)
* [Masker Types](#masker-types)
* [Custom Mask Character](#custom-mask-character)
* [Abuse Masker](#abuse-masker)
* [Custom Masker](#custom-masker)
* [Contributors](#contributors)
## Install
```bash
go get -u github.com/ggwhite/go-masker/v2
```
## Quick Start
```go
package main
import (
"log"
masker "github.com/ggwhite/go-masker/v2"
)
type User struct {
Name string `mask:"name"`
Email string `mask:"email"`
Password string `mask:"password"`
Mobile string `mask:"mobile"`
}
func main() {
u := &User{
Name: "John Doe",
Email: "john@gmail.com",
Password: "secret",
Mobile: "0987654321",
}
m := masker.NewMaskerMarshaler()
masked, err := m.Struct(u)
if err != nil {
log.Fatal(err)
}
log.Println(masked) // &{J**n D**e joh****@gmail.com ************** 0987***321}
}
```
You can also use the package-level default instance:
```go
masked, err := masker.DefaultMaskerMarshaler.Struct(u)
```
## Masker Types
| Tag | Description | Example Input | Example Output |
|-----|-------------|---------------|----------------|
| `none` | No masking, return as-is | `foo` | `foo` |
| `password` | Always returns 14 asterisks | `secret` | `**************` |
| `name` | Masks middle characters | `John Doe` | `J**n D**e` |
| `addr` | Masks last 6 characters | `台北市內湖區內湖路一段737巷1號` | `台北市內湖區內湖路一段7******` |
| `email` | Keeps first 3 chars and domain | `john@gmail.com` | `joh****@gmail.com` |
| `mobile` | Masks 3 digits from 4th position | `0987654321` | `0987***321` |
| `tel` | Formats and masks last 4 digits | `0227993078` | `(02)2799-****` |
| `id` | Masks digits 7–10 | `A123456789` | `A12345****` |
| `credit` | Masks digits 7–12 | `4111111111111111` | `411111******1111` |
| `url` | Masks URL password | `http://user:pass@host` | `http://user:xxxxx@host` |
| `abuse` | Masks abusive words via trie | `bad word` | `*** word` |
| `struct` | Recursively masks nested struct | — | — |
### Masking Slices
String slices are also supported:
```go
type Foo struct {
Tags []string `mask:"name"`
}
```
### Masking Nested Structs
```go
type Address struct {
Street string `mask:"addr"`
}
type User struct {
Name string `mask:"name"`
Address Address `mask:"struct"`
}
```
## Custom Mask Character
By default, `*` is used as the mask character. Use `SetMasker` to change it:
```go
m := masker.NewMaskerMarshaler()
m.SetMasker("#")
masked, _ := m.Struct(u)
// Name "John" -> "J##n"
```
## Abuse Masker
The abuse masker uses a trie for efficient word matching and replacement.
### Basic Usage
```go
abuseWords := []string{"bad", "terrible", "awful"}
abuseMasker := masker.NewAbuseMaskerWithWords(abuseWords)
text := "This is a bad and terrible situation"
masked := abuseMasker.Marshal("*", text)
// Output: "This is a *** and ******** situation"
```
### Load Words from File
```go
loader := masker.NewAbuseWordLoader()
words, err := loader.LoadFromFile("abuse_words.txt")
if err != nil {
log.Fatal(err)
}
abuseMasker := masker.NewAbuseMaskerWithWords(words)
```
### Use with Struct Tags
```go
type Post struct {
Title string `mask:"name"`
Content string `mask:"abuse"`
}
m := masker.NewMaskerMarshaler()
m.Register(masker.MaskerTypeAbuse, masker.NewAbuseMaskerWithWords([]string{"bad"}))
masked, _ := m.Struct(&Post{Title: "Hello", Content: "bad content"})
```
## Custom Masker
Implement the `Masker` interface to create your own masker:
```go
type Masker interface {
Marshal(maskChar string, value string) string
}
```
Example:
```go
type SSNMasker struct{}
func (m *SSNMasker) Marshal(s, i string) string {
if len(i) != 9 {
return i
}
return "***-**-" + i[7:]
}
m := masker.NewMaskerMarshaler()
m.Register("ssn", &SSNMasker{})
type Person struct {
SSN string `mask:"ssn"`
}
```
## Contributors
Thanks to all the people who already contributed!