Exemple #1
0
        /// <summary>
        /// 中国大陆身份证15位转18位
        /// </summary>
        public static string ConvertChinaPRIDCard_15_18(string idCardNo)
        {
            if (!RegExpHelper.IsAllNumber(idCardNo, 15, 15))
            {
                return(idCardNo);
            }

            var allCsNumber = new string[] { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };
            var allWiNumber = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

            var tmpNewNumber = new StringBuilder(idCardNo);

            tmpNewNumber.Insert(6, "19");

            var sum = 0;

            for (int i = 0; i < 17; i++)
            {
                sum += ConvertHelper.GetInt(tmpNewNumber[i].ToString()) * allWiNumber[i];
            }
            sum %= 11;

            tmpNewNumber.Append(allCsNumber[sum]);

            return(tmpNewNumber.ToString());
        }
Exemple #2
0
        /// <summary>
        /// 获取指定字符隐藏后的字符串
        /// </summary>
        /// <param name="str">待隐藏字符串</param>
        /// <param name="hiddenType">字符串隐藏类型</param>
        /// <param name="hiddenLength">隐藏长度</param>
        /// <param name="hiddenChar">隐藏字符</param>
        /// <returns></returns>
        public static string GetHiddenString(string str, StringHiddenType hiddenType = StringHiddenType.Start, int hiddenLength = 4, char hiddenChar = '*')
        {
            string retVal = str;

            if (RegExpHelper.IsRightLength(str, (uint)hiddenLength + 1))
            {
                if (hiddenType == StringHiddenType.Start)
                {
                    retVal = string.Format("{0}{1}", new string(hiddenChar, hiddenLength), str.Substring(hiddenLength));
                }
                else if (hiddenType == StringHiddenType.End)
                {
                    retVal = string.Format("{0}{1}", str.Substring(0, str.Length - hiddenLength), new string(hiddenChar, hiddenLength));
                }
                else if (hiddenType == StringHiddenType.Middle)
                {
                    var tmpArray = str.ToCharArray();
                    for (int i = 0; i < hiddenLength; i++)
                    {
                        var j = (tmpArray.Length - i) / 2;
                        while (j < tmpArray.Length && tmpArray[j] == '*')
                        {
                            j++;
                        }
                        tmpArray[j] = '*'; // 后面改成移除方式
                    }
                    retVal = new string(tmpArray);
                }
            }
            return(retVal);
        }
Exemple #3
0
 /// <summary>
 /// 隐藏中文姓名[不支持复姓,如"杨**"]
 /// </summary>
 public static string GetHiddenNameByChineseName(string chineseName, char replaceChar = '*')
 {
     if (RegExpHelper.IsRightLength(chineseName, 2))
     {
         return(string.Format("{0}{1}", GetFamilyByChineseName(chineseName), new string(replaceChar, 2)));
     }
     return(chineseName);
 }
Exemple #4
0
 /// <summary>
 /// 获取中文姓氏[不支持复姓,如"杨"]
 /// </summary>
 public static string GetFamilyByChineseName(string chineseName)
 {
     if (RegExpHelper.IsRightLength(chineseName, 2))
     {
         return(chineseName.Substring(0, 1));
     }
     return(chineseName);
 }
Exemple #5
0
        /// <summary>
        /// 是否是有效的中国大陆身份证[严格校验]
        /// </summary>
        public static bool IsChinaPRIDCard(string idCardNo)
        {
            if (string.IsNullOrEmpty(idCardNo))
            {
                return(false);
            }

            var tmpType  = idCardNo.Length;
            var tmpIndex = idCardNo.Length - 1;
            var tmpValue = idCardNo.Remove(tmpIndex);

            if (!RegExpHelper.IsAllNumber(tmpValue, (uint)tmpIndex, (uint)tmpIndex) || ConvertHelper.GetLong(tmpValue, 0) < Math.Pow(10, tmpIndex - 1))
            {
                return(false);
            }

            var provinceCodes = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";

            if (provinceCodes.IndexOf(idCardNo.Remove(2)) < 0)
            {
                return(false);
            }

            var interval = tmpType == 15 ? 2 : 0;
            var birthday = ConvertHelper.GetDateTime(idCardNo.Substring(6, 8 - interval).Insert(6 - interval, "-").Insert(4 - interval, "-"));

            if (birthday < InternalConstant.DefaultMSdbMinDate)
            {
                return(false);
            }

            if (tmpType == 15)
            {
                return(true);
            }

            // 继续18位校验
            var allCsNumber = new string[] { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };
            var allWiNumber = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            var allAiNumber = ConvertHelper.Cast <int>(tmpValue.ToList().ConvertAll((item) => { return(item.ToString()); })).ToArray();

            var sum = 0;

            for (int i = 0; i < tmpIndex; i++)
            {
                sum += allAiNumber[i] * allWiNumber[i];
            }

            var mod = 0;

            Math.DivRem(sum, 11, out mod);
            if (allCsNumber[mod] != idCardNo.Substring(tmpIndex, 1).ToLower())
            {
                return(false);
            }

            return(true);
        }
Exemple #6
0
 /// <summary>
 /// 替换SQL关键字
 /// </summary>
 public static string ReplaceSQLKeywords(string str, string replaceStr = "")
 {
     if (!string.IsNullOrEmpty(str))
     {
         while (true)
         {
             if (!RegExpHelper.IsContainSQLKeywords(str))
             {
                 break;
             }
             str = Regex.Replace(str, RegExpHelper.ExpressionSQLKeywords, replaceStr, RegexOptions.IgnoreCase);
         }
     }
     return(str);
 }
Exemple #7
0
        /// <summary>
        /// 获取文件的只读流
        /// </summary>
        public static Stream GetFileStream(string filePathOrUrl)
        {
            Stream retVal = null;

            try
            {
                if (!string.IsNullOrEmpty(filePathOrUrl))
                {
                    PathType pathType = 0;

                    var targetPath = filePathOrUrl;

                    if (RegExpHelper.IsUrl(filePathOrUrl))
                    {
                        pathType   = PathType.WebUrl;
                        targetPath = filePathOrUrl;
                    }
                    else if (RegExpHelper.IsRelativePath(filePathOrUrl))
                    {
                        pathType   = PathType.WindowPath;
                        targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePathOrUrl.Replace("/", @"\").TrimStart('/').TrimStart('\\'));
                    }
                    else if (RegExpHelper.IsPhysicalPath(filePathOrUrl))
                    {
                        pathType   = PathType.WindowPath;
                        targetPath = filePathOrUrl;
                    }

                    if (Enum.IsDefined(typeof(PathType), pathType))
                    {
                        switch (pathType)
                        {
                        case PathType.WindowPath:
                            retVal = new FileStream(targetPath, FileMode.Open, FileAccess.Read);
                            break;

                        case PathType.WebUrl:
                            retVal = NetHelper.HttpGetStream(targetPath);
                            break;
                        }
                    }
                }
            }
            catch { }
            return(retVal);
        }