masker

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 5 Imported by: 6

README

Golang Masker

Build Status codecov Go Report Card License GoDoc Release

Golang Masker is a simple utility of creating a mask for sensitive information.

Getting Started

$ go get -u github.com/ggwhite/go-masker

Demo

There are two ways to get a masker instance:

1. Get a instance directly from go-masker package
package main

import (
	masker "github.com/ggwhite/go-masker"
)

func main() {
	masker.Name("ggwhite")
	masker.ID("A123456789")
	masker.Mobile("0978978978")
}
2. Get a instance via masker.New()
package main

import (
	masker "github.com/ggwhite/go-masker"
)

func main() {
	m := masker.New()
	m.Name("ggwhite")
	m.ID("A123456789")
	m.Mobile("0978978978")
}

Mask Types

Type Const Tag Description
Name MName name mask the second letter and the third letter
Password MPassword password always return ************
Address MAddress addr keep first 6 letters, mask the rest
Email MEmail email keep domain and the first 3 letters
Mobile MMobile mobile mask 3 digits from the 4'th digit
Telephone MTelephone tel remove (, ), , - chart, and mask last 4 digits of telephone number, format to (??)????-????
ID MID id mask last 4 digits of ID number
CreditCard MCreditCard credit mask 6 digits from the 7'th digit
Struct MStruct struct mask the struct
URL MURL url mask the password field if present, eg http://admin:mysecretpassword@localhost:1234/uri

Mask the String

String method requires two parameters, a mask type CONST and a string:

package main

import (
	masker "github.com/ggwhite/go-masker"
)

func main() {
	masker.String(masker.MName, "ggwhite")
	masker.String(masker.MID, "A123456789")
	masker.String(masker.MMobile, "0987987987")
}

Result:

g**hite
A12345****
0987***987

Custom Mask

package main

import (
	masker "github.com/ggwhite/go-masker"
)

func main() {
	masker.String(masker.MName, "ggwhite")
	masker.String(masker.MID, "A123456789")
	masker.SetMask("-")
	masker.String(masker.MMobile, "0987987987")
}

Result:

g**hite
A12345****
0987---987

Mask the Struct

You can define your struct and add tag mask to let masker know what kind of the format to mask.

Field must be public in the struct.

package main

import (
	"log"
	masker "github.com/ggwhite/go-masker"
)

type Foo struct {
	Name   string `mask:"name"`
	Mobile string `mask:"mobile"`
}

func main() {
	foo := &Foo{
		Name:   "ggwhite",
		Mobile: "0987987987",
	}
	t, err := masker.Struct(foo)
	log.Println(t)
	log.Println(t.(*Foo))
	log.Println(err)
}

Result:

t = &{g**hite 0987***987} 
err = <nil>
Struct contain struct
package main

import (
	masker "github.com/ggwhite/go-masker"
)

type Foo struct {
	Name   string `mask:"name"`
	Mobile string `mask:"mobile"`
	Qoo    *Qoo   `mask:"struct"`
}

type Qoo struct {
	Name      string `mask:"name"`
	Telephone string `mask:"tel"`
}

func main() {
	foo := &Foo{
		Name:   "ggwhite",
		Mobile: "0987987987",
		Qoo: &Qoo{
			Name:      "gino",
			Telephone: "0287658765",
		},
	}
	t, err := masker.Struct(foo)
	log.Println(t)
	log.Println(t.(*Foo).Qoo)
	log.Println(err)
}

Result:

t = &{g**hite 0987***987 0xc00000a080}
t.Qoo = &{g**o (02)8765-****}
err = <nil>
Struct contain string slice
package main

import (
	masker "github.com/ggwhite/go-masker"
)

type Foo struct {
	Name   string `mask:"name"`
	Mobile string `mask:"mobile"`
	IDs    []string   `mask:"id"`
}

func main() {
	foo := &Foo{
		Name:   "ggwhite",
		Mobile: "0987987987",
		IDs: []string{
			"A123456789",
			"A987654321",
		},
	}
	t, err := masker.Struct(foo)
	log.Println(t)
	log.Println(err)
}

Result:

t = &{g**hite 0987***987 [A12345**** A98765****]}
err = <nil>

Documentation

Overview

Package masker Provide mask format of Taiwan usually used(Name, Address, Email, ID ...etc.),

Index

Constants

View Source
const (
	MPassword   mtype = "password"
	MName       mtype = "name"
	MAddress    mtype = "addr"
	MEmail      mtype = "email"
	MMobile     mtype = "mobile"
	MTelephone  mtype = "tel"
	MID         mtype = "id"
	MCreditCard mtype = "credit"
	MStruct     mtype = "struct"
	MURL        mtype = "url"
)

Maske Types of format string

Variables

This section is empty.

Functions

func Address

func Address(i string) string

Address keep first 6 letters, mask the rest

Example:

input: 台北市內湖區內湖路一段737巷1號1樓
output: 台北市內湖區******

func CreditCard

func CreditCard(i string) string

CreditCard mask 6 digits from the 7'th digit

Example:

input1: 1234567890123456 (VISA, JCB, MasterCard)(len = 16)
output1: 123456******3456
input2: 123456789012345 (American Express)(len = 15)
output2: 123456******345

func Email

func Email(i string) string

Email keep domain and the first 3 letters

Example:

input: ggw.chang@gmail.com
output: ggw****@gmail.com

func ID

func ID(i string) string

ID mask last 4 digits of ID number

Example:

input: A123456789
output: A12345****

func Mobile

func Mobile(i string) string

Mobile mask 3 digits from the 4'th digit

Example:

input: 0987654321
output: 0987***321

func Name

func Name(i string) string

Name mask the second letter and the third letter

Example:

input: ABCD
output: A**D

func Password

func Password(i string) string

Password always return "************"

func SetMask added in v1.0.5

func SetMask(mask string)

func String

func String(t mtype, i string) string

String mask input string of the mask type

Example:

masker.String(masker.MName, "ggwhite")
masker.String(masker.MID, "A123456789")
masker.String(masker.MMobile, "0987987987")

func Struct

func Struct(s interface{}) (interface{}, error)

Struct must input a interface{}, add tag mask on struct fields, after Struct(), return a pointer interface{} of input type and it will be masked with the tag format type

Example:

type Foo struct {
    Name      string `mask:"name"`
    Email     string `mask:"email"`
    Password  string `mask:"password"`
    ID        string `mask:"id"`
    Address   string `mask:"addr"`
    Mobile    string `mask:"mobile"`
    Telephone string `mask:"tel"`
    Credit    string `mask:"credit"`
    Foo       *Foo   `mask:"struct"`
}

func main() {
    s := &Foo{
        Name: ...,
        Email: ...,
        Password: ...,
        Foo: &{
            Name: ...,
            Email: ...,
            Password: ...,
        }
    }

    t, err := masker.Struct(s)

    fmt.Println(t.(*Foo))
}

func Telephone

func Telephone(i string) string

Telephone remove "(", ")", " ", "-" chart, and mask last 4 digits of telephone number, format to "(??)????-????"

Example:

input: 0227993078
output: (02)2799-****

Types

type Masker

type Masker struct {
	// contains filtered or unexported fields
}

Masker is a instance to marshal masked string

func New

func New() *Masker

New create Masker

func (*Masker) Address

func (m *Masker) Address(i string) string

Address keep first 6 letters, mask the rest

Example:

input: 台北市內湖區內湖路一段737巷1號1樓
output: 台北市內湖區******

func (*Masker) CreditCard

func (m *Masker) CreditCard(i string) string

CreditCard mask 6 digits from the 7'th digit

Example:

input1: 1234567890123456 (VISA, JCB, MasterCard)(len = 16)
output1: 123456******3456
input2: 123456789012345` (American Express)(len = 15)
output2: 123456******345`

func (*Masker) Email

func (m *Masker) Email(i string) string

Email keep domain and the first 3 letters

Example:

input: ggw.chang@gmail.com
output: ggw****@gmail.com

func (*Masker) ID

func (m *Masker) ID(i string) string

ID mask last 4 digits of ID number

Example:

input: A123456789
output: A12345****

func (*Masker) Mobile

func (m *Masker) Mobile(i string) string

Mobile mask 3 digits from the 4'th digit

Example:

input: 0987654321
output: 0987***321

func (*Masker) Name

func (m *Masker) Name(i string) string

Name mask the second letter and the third letter

Example:

input: ABCD
output: A**D

func (*Masker) Password

func (m *Masker) Password(i string) string

Password always return "************"

func (*Masker) String

func (m *Masker) String(t mtype, i string) string

String mask input string of the mask type

Example:

masker.String(masker.MName, "ggwhite")
masker.String(masker.MID, "A123456789")
masker.String(masker.MMobile, "0987987987")

func (*Masker) Struct

func (m *Masker) Struct(s interface{}) (interface{}, error)

Struct must input a interface{}, add tag mask on struct fields, after Struct(), return a pointer interface{} of input type and it will be masked with the tag format type

Example:

type Foo struct {
    Name      string `mask:"name"`
    Email     string `mask:"email"`
    Password  string `mask:"password"`
    ID        string `mask:"id"`
    Address   string `mask:"addr"`
    Mobile    string `mask:"mobile"`
    Telephone string `mask:"tel"`
    Credit    string `mask:"credit"`
    Foo       *Foo   `mask:"struct"`
}

func main() {
    s := &Foo{
        Name: ...,
        Email: ...,
        Password: ...,
        Foo: &{
            Name: ...,
            Email: ...,
            Password: ...,
        }
    }

    m := masker.New()

    t, err := m.Struct(s)

    fmt.Println(t.(*Foo))
}

func (*Masker) Telephone

func (m *Masker) Telephone(i string) string

Telephone remove "(", ")", " ", "-" chart, and mask last 4 digits of telephone number, format to "(??)????-????"

Example:

input: 0227993078
output: (02)2799-****

func (*Masker) URL added in v1.0.6

func (m *Masker) URL(i string) string

URL mask the password part of the URL if exists

Example:

input: http://admin:mysecretpassword@localhost:1234/uri
output:http://admin:xxxxx@localhost:1234/uri

Jump to

Keyboard shortcuts

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