masker

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 5 Imported by: 0

README

Go Masker v2

build workflow GoDoc Go Report Card License Release

Go Masker v2 is a tool for masking sensitive data in Go code. It provides a simple and convenient way to replace sensitive information, such as passwords or API keys, with placeholder values.

Install

To install Go Masker v2, you can use the following command:

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

Usage

To use Go Masker v2, you can create a new instance of the Masker type and then use its methods to mask sensitive data. For example:

package main

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

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

func main() {
    foo := &Foo{
        Name:   "ggwhite",
        Mobile: "0987987987",
    }

    m := masker.NewMaskerMarshaler()

    t, err := m.Struct(foo)
    log.Println(t)
    log.Println(err)
}

This will produce the following output:

t = &{g**hite 0987***987}
err = <nil>

For more information about how to use Go Masker v2, please refer to the documentation.

Custom Masker

You can also create a custom masker by implementing the Masker interface. For example:

package main

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

type MyEmailMasker struct{}

func (m *MyEmailMasker) Marshal(s, i string) string {
	return "myemailmasker"
}

type MyMasker struct{}

func (m *MyMasker) Marshal(s, i string) string {
	return "mymasker"
}

func main() {
    m := masker.NewMaskerMarshaler()

    // Register custom masker and override default masker
	m.Register(masker.MaskerTypeEmail, &MyEmailMasker{})

	log.Println(m.Marshal(masker.MaskerTypeEmail, "email")) // myemailmasker <nil>

	// Register custom masker and use it
	m.Register("mymasker", &MyMasker{})

	log.Println(m.Marshal("mymasker", "1234567")) // mymasker <nil>
}

Contributors

Thanks to all the people who already contributed!

Documentation

Index

Constants

This section is empty.

Variables

DefaultMaskerMarshaler is a default masker marshaler It has default maskers and default masker Default maskers are:

  • NoneMasker
  • PasswordMasker
  • NameMasker
  • AddressMasker
  • EmailMasker
  • MobileMasker
  • TelephoneMasker
  • IDMasker
  • CreditMasker
  • URLMasker

Default masker is "*" It is used for masking sensitive data

Functions

This section is empty.

Types

type AddressMasker

type AddressMasker struct{}

AddressMasker is a masker for address

func (*AddressMasker) Marshal

func (m *AddressMasker) Marshal(s string, i string) string

Marshal masks address It masks last 6 digits of address Example:

AddressMasker{}.Marshal("*", "台北市內湖區內湖路一段737巷1號1樓") // returns "台北市內湖區******"

type CreditMasker

type CreditMasker struct{}

func (*CreditMasker) Marshal

func (m *CreditMasker) Marshal(s string, i string) string

Marshal masks credit card number It mask 6 digits from the 7'th digit Example:

CreditMasker{}.Marshal("*", "4111111111111111") // returns "**** **** **** 1111"

type EmailMasker

type EmailMasker struct{}

EmailMasker is a masker for email

func (*EmailMasker) Marshal

func (m *EmailMasker) Marshal(s string, i string) string

Marshal masks email It keep domain and the first 3 letters Example:

AddressMasker{}.Marshal("*", "ggw.chang@gmail.com") // returns "ggw****@gmail.com"

type IDMasker

type IDMasker struct{}

IDMasker is a masker for ID

func (*IDMasker) Marshal

func (m *IDMasker) Marshal(s string, i string) string

Marshal masks ID It mask last 4 digits of ID number Example:

IDMasker{}.Marshal("*", "1234567890") // returns "1234****890"

type Masker

type Masker interface {
	Marshal(string, string) string
}

Masker is an interface for masking sensitive data

type MaskerMarshaler

type MaskerMarshaler struct {
	Maskers map[MaskerType]Masker
	// contains filtered or unexported fields
}

MaskerMarshaler is a masker marshaler

func NewMaskerMarshaler

func NewMaskerMarshaler() *MaskerMarshaler

NewMaskerMarshaler returns a new masker marshaler It has default maskers and default masker Default maskers are:

  • NoneMasker
  • PasswordMasker
  • NameMasker
  • AddressMasker
  • EmailMasker
  • MobileMasker
  • TelephoneMasker
  • IDMasker
  • CreditMasker
  • URLMasker

Default masker is "*" It is used for masking sensitive data

func (*MaskerMarshaler) Get

func (m *MaskerMarshaler) Get(t MaskerType) (Masker, error)

Get returns a masker by masker type It is used for getting a masker by masker type Example:

m := masker.NewMaskerMarshaler()
masker, _ := m.Get(masker.MaskerTypePassword)
log.Println(masker) // &{PasswordMasker}

func (*MaskerMarshaler) List

func (m *MaskerMarshaler) List() []MaskerType

List returns a list of masker types It is used for listing all masker types Example:

m := masker.NewMaskerMarshaler()
log.Println(m.List()) // [password name addr email tel id url none mobile credit]

func (*MaskerMarshaler) Marshal

func (m *MaskerMarshaler) Marshal(t MaskerType, value string) (string, error)

Marshal returns a masked value by masker type It is used for masking sensitive data Example:

m := masker.NewMaskerMarshaler()
log.Println(m.Marshal(masker.MaskerTypeNone, "none"))                               // none <nil>
log.Println(m.Marshal(masker.MaskerTypePassword, "password"))                       // ************** <nil>
log.Println(m.Marshal(masker.MaskerTypeName, "name"))                               // n**e <nil>
log.Println(m.Marshal(masker.MaskerTypeAddress, "address"))                         // addres****** <nil>
log.Println(m.Marshal(masker.MaskerTypeEmail, "email"))                             // ema**** <nil>
log.Println(m.Marshal(masker.MaskerTypeMobile, "mobile"))                           // mobi*** <nil>
log.Println(m.Marshal(masker.MaskerTypeTel, "tel"))                                 // tel <nil>
log.Println(m.Marshal(masker.MaskerTypeID, "id"))                                   // id**** <nil>
log.Println(m.Marshal(masker.MaskerTypeCredit, "4111111111111111"))                 // 411111******1111 <nil>
log.Println(m.Marshal(masker.MaskerTypeURL, "http://john:password@localhost:3000")) // http://john:xxxxx@localhost:3000 <nil>

func (*MaskerMarshaler) Register

func (m *MaskerMarshaler) Register(t MaskerType, masker Masker)

Register adds a masker by masker type It is used for adding or override a masker by masker type Example:

m := masker.NewMaskerMarshaler()
m.Register(masker.MaskerTypePassword, &PasswordMasker{})
log.Println(m.List()) // [password name addr email tel id url none mobile credit]

func (*MaskerMarshaler) SetMasker

func (m *MaskerMarshaler) SetMasker(masker string)

func (*MaskerMarshaler) Struct

func (m *MaskerMarshaler) 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"`
	URL       string `mask:"url"`
	Foo       *Foo   `mask:"struct"`
}

func main() {
	m := masker.NewMaskerMarshaler()
	log.Println(m.List()) // [password name addr email tel id url none mobile credit]
	foo1 := &Foo{
		Name:      "John Doe",
		Email:     "john@gmail.com",
		Password:  "password",
		ID:        "1234567890",
		Address:   "123 Main St",
		Mobile:    "1234567890",
		Telephone: "1234567890",
		Credit:    "4111111111111111",
		URL:       "http://john:password@localhost:3000",
		Foo: &Foo{
			Name:      "John Doe",
			Email:     "john@gmail.com",
			Password:  "password",
			ID:        "1234567890",
			Address:   "123 Main St",
			Mobile:    "1234567890",
			Telephone: "1234567890",
			Credit:    "4111111111111111",
			URL:       "http://john:password@localhost:3000",
		},
	}

	foo2, _ := m.Struct(foo1)

	log.Println(foo1) // &{John Doe john@gmail.com password 1234567890 123 Main St 1234567890 1234567890 4111111111111111 http://john:password@localhost:3000 0xc0000001e0}
	log.Println(foo1.Foo) // &{John Doe john@gmail.com password 1234567890 123 Main St 1234567890 1234567890 4111111111111111 http://john:password@localhost:3000 <nil>}
	log.Println(foo2) // &{J**n D**e joh****@gmail.com ************** 123456**** 123 Ma****** 1234***890 (12)3456-**** 411111******1111 http://john:xxxxx@localhost:3000 0xc000000320}
	log.Println(foo2.(*Foo).Foo) // &{J**n D**e joh****@gmail.com ************** 123456**** 123 Ma****** 1234***890 (12)3456-**** 411111******1111 http://john:xxxxx@localhost:3000 <nil>}
}

func (*MaskerMarshaler) Unregister

func (m *MaskerMarshaler) Unregister(t MaskerType)

Unregister removes a masker by masker type It is used for removing a masker by masker type Example:

m := masker.NewMaskerMarshaler()
m.Unregister(masker.MaskerTypePassword)
log.Println(m.List()) // [name addr email tel id url none mobile credit]

type MaskerType

type MaskerType string

MaskerType is a string type for masker type

const (
	MaskerTypeNone     MaskerType = "none"
	MaskerTypePassword MaskerType = "password"
	MaskerTypeName     MaskerType = "name"
	MaskerTypeAddress  MaskerType = "addr"
	MaskerTypeEmail    MaskerType = "email"
	MaskerTypeMobile   MaskerType = "mobile"
	MaskerTypeTel      MaskerType = "tel"
	MaskerTypeID       MaskerType = "id"
	MaskerTypeCredit   MaskerType = "credit"
	MaskerTypeURL      MaskerType = "url"
	MaskerTypeStruct   MaskerType = "struct"
)

MaskerType constants

type MobileMasker

type MobileMasker struct{}

MobileMasker is a masker for mobile

func (*MobileMasker) Marshal

func (m *MobileMasker) Marshal(s string, i string) string

Marshal masks mobile It mask 3 digits from the 4'th digit Example:

MobileMasker{}.Marshal("*", "0987654321") // returns "0987***321"

type NameMasker

type NameMasker struct{}

func (*NameMasker) Marshal

func (m *NameMasker) Marshal(s string, i string) string

Marshal masks name It mask the second letter and the third letter Example:

NameMasker{}.Marshal("*", "name") // returns "n**e"
NameMasker{}.Marshal("*", "ABCD") // returns "A**D"

type NoneMasker

type NoneMasker struct{}

NameMasker is a masker for name

func (*NoneMasker) Marshal

func (m *NoneMasker) Marshal(i string, value string) string

Marshal masks name It returns the same value Example:

NoneMasker{}.Marshal("*", "name") // returns "name"

type PasswordMasker

type PasswordMasker struct{}

PasswordMasker is a masker for password

func (*PasswordMasker) Marshal

func (m *PasswordMasker) Marshal(s string, i string) string

Marshal masks password It returns 14 asterisks Example:

PasswordMasker{}.Marshal("*", "password") // returns "**************"
PasswordMasker{}.Marshal("&", "password") // returns "&&&&&&&&&&&&&&"

type TelephoneMasker

type TelephoneMasker struct{}

TelephoneMasker is a masker for telephone

func (*TelephoneMasker) Marshal

func (m *TelephoneMasker) Marshal(s string, i string) string

Marshal masks telephone It remove "(", ")", " ", "-" chart, and mask last 4 digits of telephone number, format to "(??)????-????" Example:

TelephoneMasker{}.Marshal("*", "0227993078") // returns "(02)2799-****"

type URLMasker

type URLMasker struct{}

URLMasker is a masker for URL

func (*URLMasker) Marshal

func (m *URLMasker) Marshal(s, i string) string

Marshal masks URL It mask the password part of the URL if exists

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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