using System;
using System.Text;

namespace JiaZhiQuan.Common.Utils
{
    public static class StrEncryUtils
    {
        /// <summary>
        /// 加密身份证号，只保留第一位和最后一位，中间替换成*
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string EncryUserIdNumber(this string id)
        {
            if (string.IsNullOrEmpty(id)) return "";
            int length = id.Length;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length - 2; i++)
            {
                sb.Append("*");
            }
            return $"{id.Substring(0, 1)}{sb}{id.Substring(length - 1, 1)}";
        }

        /// <summary>
        /// 产生随机字符串
        /// </summary>
        /// <returns>字符串位数</returns>
        public static string GetRandom(int length) {
            int number;
            char code;
            string checkCode = String.Empty;
            System.Random random = new Random();

            for (int i = 0; i < length ; i++) {
                number = random.Next();
                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));
                checkCode += code.ToString();
            }
            return checkCode;
        }
    }
}
