// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package util import ( "fmt" "strings" "unicode/utf8" ) // DesensVin 使vin脱敏, Desens => Desensitize // 输入:vin 车辆唯一编号 // 输出:脱敏后结果,如:LSVGJ4559C2053325 => LSVGJ45*******325 func DesensVin(vin string) string { if len(vin) != 17 { return "" } return string([]rune(vin)[0:8]) + strings.Repeat("*", 7) + string([]rune(vin)[15:]) } // DesensName 使姓名脱敏, Desens => Desensitize // 输入:name 姓名 // 输出:脱敏后结果,如:张三 => 张*, 王老五 => 王*五, 王五杰克 => 王**克 func DesensName(name string) string { nameRune := []rune(name) len := utf8.RuneCountInString(name) switch { case len == 1: return name case len == 2: return fmt.Sprintf("%s*", string(nameRune[:1])) case len <= 0: return "*" case len > 2: return fmt.Sprintf("%s%s%s", string(nameRune[:1]), strings.Repeat("*", len-2), string(nameRune[len-1:])) } return "" } // DesensIdCert 使身份证号码脱敏, Desens => Desensitize // 输入:idcert 身份证号码 // 输出:脱敏后结果,如:32312219990101342x => 3231**********342x func DesensIdCert(idcert string) string { if !IsIdCert(idcert) { return "*" } return fmt.Sprintf("%s%s%s", idcert[:4], strings.Repeat("*", 10), idcert[14:]) } // DesensIdCert 使手机号码脱敏, Desens => Desensitize // 输入:idcert 手机号码 // 输出:脱敏后结果,如:18180888088=> 1818****088 func DesensPhone(phone string) string { // 少于11位或已经包含了*,原样返回 if len(phone) < 11 || strings.Contains(phone, "*") { return phone } return fmt.Sprintf("%s****%s", phone[:3], phone[7:]) }