隱藏手機號、郵箱等敏感信息

隱藏手機號、郵箱等敏感信息

Intro

做項目的時候,頁面上有一些敏感信息,需要用“*”隱藏一些比較重要的信息,于是打算寫一個通用的方法。

Let's do it !

Method 1:指定左右字符數(shù)量

Method 1.1 中間的*的個數(shù)和實際長度有關(guān)


       /// <summary>
        /// 隱藏敏感信息
        /// </summary>
        /// <param name="info">信息實體</param>
        /// <param name="left">左邊保留的字符數(shù)</param>
        /// <param name="right">右邊保留的字符數(shù)</param>
        /// <param name="basedOnLeft">當(dāng)長度異常時,是否顯示左邊 
        /// <code>true</code>顯示左邊,<code>false</code>顯示右邊
        /// </param>
        /// <returns></returns>
        public static string HideSensitiveInfo(string info, int left, int right, bool basedOnLeft=true)
        {
            if (String.IsNullOrEmpty(info))
            {
                return "";
            }
            StringBuilder sbText = new StringBuilder();
            int hiddenCharCount = info.Length - left - right;
            if (hiddenCharCount > 0)
            {
                string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
                sbText.Append(prefix);
                for (int i = 0; i < hiddenCharCount; i++)
                {
                    sbText.Append("*");
                }
                sbText.Append(suffix);
            }
            else
            {
                if (basedOnLeft)
                {
                    if (info.Length > left && left > 0)
                    {
                        sbText.Append(info.Substring(0, left) + "****");
                    }
                    else
                    {
                        sbText.Append(info.Substring(0, 1) + "****");
                    }
                }
                else
                {
                    if (info.Length > right && right > 0)
                    {
                        sbText.Append("****" + info.Substring(info.Length - right));
                    }
                    else
                    {
                        sbText.Append("****" + info.Substring(info.Length - 1));
                    }
                }
            }
            return sbText.ToString();
        }

Method 1.2 : 中間的*的個數(shù)固定


        /// <summary>
        /// 隱藏敏感信息
        /// </summary>
        /// <param name="info">信息實體</param>
        /// <param name="left">左邊保留的字符數(shù)</param>
        /// <param name="right">右邊保留的字符數(shù)</param>
        /// <param name="basedOnLeft">當(dāng)長度異常時,是否顯示左邊 
        /// <code>true</code>顯示左邊,<code>false</code>顯示右邊
        /// <returns></returns>
        public static string HideSensitiveInfo1(string info, int left, int right, bool basedOnLeft = true)
        {
            if (String.IsNullOrEmpty(info))
            {
                return "";
            }
            StringBuilder sbText = new StringBuilder();
            int hiddenCharCount = info.Length - left - right;
            if (hiddenCharCount > 0)
            {
                string prefix = info.Substring(0, left), suffix = info.Substring(info.Length - right);
                sbText.Append(prefix);
                sbText.Append("****");
                sbText.Append(suffix);
            }
            else
            {
                if (basedOnLeft)
                {
                    if (info.Length > left && left >0)
                    {
                        sbText.Append(info.Substring(0, left) + "****");
                    }
                    else
                    {
                        sbText.Append(info.Substring(0, 1) + "****");
                    }
                }
                else
                {
                    if (info.Length > right && right>0)
                    {
                        sbText.Append("****" + info.Substring(info.Length - right));
                    }
                    else
                    {
                        sbText.Append("****" + info.Substring(info.Length - 1));
                    }
                }
            }
            return sbText.ToString();
        }

Method 2 : “*”數(shù)量一定,設(shè)置為4個,按信息總長度的比例來取,默認左右各取1/3


       /// <summary>
        /// 隱藏敏感信息
        /// </summary>
        /// <param name="info">信息</param>
        /// <param name="sublen">信息總長與左子串(或右子串)的比例</param>
        /// <param name="basedOnLeft">當(dāng)長度異常時,是否顯示左邊,默認true,默認顯示左邊
        /// <code>true</code>顯示左邊,<code>false</code>顯示右邊
        /// <returns></returns>
        public static string HideSensitiveInfo(string info,int sublen = 3,bool basedOnLeft = true)
        {
            if (String.IsNullOrEmpty(info))
            {
                return "";
            }
            if (sublen<=1)
            {
                sublen = 3;
            }
            int subLength = info.Length / sublen;
            if (subLength > 0 && info.Length > (subLength*2) )
            {
                string prefix = info.Substring(0, subLength), suffix = info.Substring(info.Length - subLength);
                return prefix + "****" + suffix;
            }
            else
            {
                if (basedOnLeft)
                {
                    string prefix = subLength > 0 ? info.Substring(0, subLength) : info.Substring(0, 1);
                    return prefix + "****";
                }
                else
                {
                    string suffix = subLength > 0 ? info.Substring(info.Length-subLength) : info.Substring(info.Length-1);
                    return "****"+suffix;
                }
            }
        }

擴展

手機號 1


        /// <summary>
        /// 隱藏手機號詳情
        /// </summary>
        /// <param name="phone">手機號</param>
        /// <param name="left">左邊保留字符數(shù)</param>
        /// <param name="right">右邊保留字符數(shù)</param>
        /// <returns></returns>
        public static string HideTelDetails(string phone, int left = 3, int right = 4)
        {
            return HideSensitiveInfo(phone, left, right);
        }

測試結(jié)果如下:

隱藏手機詳情方式1

手機號 2


        /// <summary>
        /// 隱藏手機號詳情
        /// </summary>
        /// <param name="phone">手機號</param>
        /// <param name="left">左邊保留字符數(shù)</param>
        /// <param name="right">右邊保留字符數(shù)</param>
        /// <returns></returns>
        public static string HideTelDetails(string phone, int left = 3, int right = 4)
        {
            return HideSensitiveInfo1(phone, left, right);
        }

測試結(jié)果如下:

隱藏手機詳情方式2

郵件地址

        
        /// <summary>
        /// 隱藏右鍵詳情
        /// </summary>
        /// <param name="email">郵件地址</param>
        /// <param name="left">郵件頭保留字符個數(shù),默認值設(shè)置為3</param>
        /// <returns></returns>
        public static string HideEmailDetails(string email, int left = 3)
        {
            if (String.IsNullOrEmpty(email))
            {
                return "";
            }
            if (System.Text.RegularExpressions.Regex.IsMatch(email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))//如果是郵件地址
            {
                int suffixLen =email.Length - email.LastIndexOf('@');
                return HideSensitiveInfo(email, left, suffixLen,false);
            }
            else
            {
                return HideSensitiveInfo(email);
            }
        }

測試結(jié)果如下:

hide-email-details.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,108評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評論 19 139
  • 重新排版 理解navigator 這是一個簡單的例子,用Navigator來跳轉(zhuǎn)頁面,頁面之間傳遞參數(shù) (代碼是E...
    lyzaijs閱讀 1,096評論 0 3
  • 《遙遠的救世主》芮小丹的父親說:“他丁元英失去的只是一個女人,他以后可以有第二個,第三個女人。而我失去的是唯一的女...
    林小金閱讀 247評論 0 0

友情鏈接更多精彩內(nèi)容