using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MEAS.Common { public class DesensitizationHelper { //定义一个用于保存静态变量的实例 private static DesensitizationHelper instance = null; //定义一个保证线程同步的标识 private static readonly object locker = new object(); //构造函数为私有,使外界不能创建该类的实例 private DesensitizationHelper() { } public static DesensitizationHelper Instance { get { if (instance == null) { lock (locker) { if (instance == null) instance = new DesensitizationHelper(); } } return instance; } } /// <summary> /// 姓名敏感处理 /// </summary> /// <param name="fullName">姓名</param> /// <returns>脱敏后的姓名</returns> public string SetSensitiveName(string fullName) { if (string.IsNullOrEmpty(fullName)) return string.Empty; string name = string.Empty; try { string familyName = fullName.Substring(0, 1); string end = fullName.Substring(fullName.Length - 1, 1); //长度为2 if (fullName.Length <= 2) name = familyName + "*"; //长度大于2 else if (fullName.Length >= 3) { name = familyName.PadRight(fullName.Length - 1, '*') + end; } } catch { return name; } return name; } /// <summary> /// 身份证脱敏 /// </summary> /// <param name="idCardNo">身份证号</param> /// <returns>脱敏后的身份证号</returns> public string SetSensitiveIdCardNo(string idCardNo) { if (string.IsNullOrEmpty(idCardNo) || (idCardNo.Length != 15 && idCardNo.Length != 18)) return idCardNo; string card = string.Empty; try { string begin = idCardNo.Substring(0, 6); string middle = idCardNo.Substring(6, 8); string end = idCardNo.Substring(14, idCardNo.Length - 14); card = begin + "********" + end; } catch { return card; } return card; } } }
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!