GithubHelp home page GithubHelp logo

jiy1012 / desensitization Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 23 KB

golang版脱敏,通过结构体tag配置的方式,执行脱敏。可在api返回json时统一调用处理,减少业务逻辑代码侵入。

License: GNU General Public License v3.0

Go 100.00%

desensitization's Introduction

desensitization

golang版脱敏,通过结构体tag配置的方式,执行脱敏。可在api返回json时统一调用处理,减少业务逻辑代码侵入。

使用方法:

type TestCommonFields struct {
	Phone    string `json:"phone" desensitization:"PHONE"`
	Email    string `json:"email" desensitization:"EMAIL"`
	UserName string `json:"user_name" desensitization:"CHINESE_NAME"`
	IDCard   string `json:"id_card" desensitization:"CHINESE_IDCARD"`
}

	if err := Desensitization(&p); err != nil {
		t.Errorf("Desensitization() error = %v", err)
	}

自定义脱敏规则: 参考desensitizer文件夹下的规则,实现接口

const Type = "CHINESE_IDCARD"

type Operator struct{}

// Desensitization
// 脱敏规则
// 15位 显示前三后四
// 18位 显示前三后四
func (Operator) Desensitization(in interface{}) (out interface{}, err error) {
	inStr := in.(string)
	if inStr == "" {
		return "", nil
	}
	l := len(inStr)
	if l == 15 {
		return inStr[:3] + strings.Repeat("*", l-3-4) + inStr[l-4:], nil
	} else if l == 18 {
		return inStr[:3] + strings.Repeat("*", l-3-4) + inStr[l-4:], nil
	}
	return inStr, nil
}

然后再调用之前注册即可

RegisterDesensitizer(email.Type, email.Operator{})

desensitization's People

Contributors

jiy1012 avatar

Stargazers

 avatar  avatar

Watchers

 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.