You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.7 KiB
81 lines
2.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Apewer
|
|
{
|
|
|
|
/// <summary></summary>
|
|
public class ChinaUtility
|
|
{
|
|
|
|
private static bool CheckIdCode18(string idCode18)
|
|
{
|
|
// GB11643-1999
|
|
if (string.IsNullOrEmpty(idCode18) || idCode18.Length != 18) return false;
|
|
var code = idCode18;
|
|
|
|
// 数字。
|
|
var n = 0L;
|
|
if (long.TryParse(code.Remove(17), out n) == false) return false;
|
|
if (n < Math.Pow(10, 16)) return false;
|
|
if (long.TryParse(code.Replace('x', '0').Replace('X', '0'), out n) == false) return false;
|
|
|
|
// 省份。
|
|
var address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
|
|
if (address.IndexOf(code.Remove(2)) < 0) return false;
|
|
|
|
// 生日。
|
|
var birth = code.Substring(6, 8).Insert(6, "-").Insert(4, "-");
|
|
var time = new DateTime();
|
|
if (DateTime.TryParse(birth, out time) == false) return false;
|
|
|
|
// 校验码。
|
|
var cc = "10x98765432".ToCharArray();
|
|
var wi = "7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2".Split(',');
|
|
var ai = code.Remove(17).ToCharArray();
|
|
var sum = 0;
|
|
for (int i = 0; i < 17; i++)
|
|
{
|
|
sum += int.Parse(wi[i]) * int.Parse(ai[i].ToString());
|
|
}
|
|
var y = sum % 11;
|
|
if (cc[y] != code[17]) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
private static bool CheckIdCode15(string idCode15)
|
|
{
|
|
if (string.IsNullOrEmpty(idCode15) || idCode15.Length != 15) return false;
|
|
var code = idCode15;
|
|
|
|
// 数字。
|
|
var n = 0L;
|
|
if (long.TryParse(code, out n) == false || n < Math.Pow(10, 14)) return false;
|
|
|
|
// 省份。
|
|
var address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
|
|
if (address.IndexOf(code.Remove(2)) == -1) return false;
|
|
|
|
// 生日。
|
|
var birth = code.Substring(6, 6).Insert(4, "-").Insert(2, "-");
|
|
var time = new DateTime();
|
|
if (DateTime.TryParse(birth, out time) == false) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/// <summary></summary>
|
|
public static bool CheckIdCode(string idCode)
|
|
{
|
|
if (string.IsNullOrEmpty(idCode)) return false;
|
|
if (idCode.Length == 18) return CheckIdCode18(idCode);
|
|
if (idCode.Length == 15) return CheckIdCode15(idCode);
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
}
|