GithubHelp home page GithubHelp logo

carychy / go-masker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ggwhite/go-masker

0.0 0.0 0.0 70 KB

Simple utility of creating a mask for sensitive information

Home Page: https://godoc.org/github.com/ggwhite/go-masker

License: MIT License

Go 100.00%

go-masker's Introduction

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

Mask the String

String methomd 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>

go-masker's People

Contributors

binsarjr avatar falconray0704 avatar ggwhite avatar ginioo avatar moredure avatar yalon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.