公民身份号码的校验

///   <summary>
/// IsIdentityCardNumber (string) 的测试
/// </summary>
[TestMethod()]
public   void  IsIdentityCardNumberTest()
{
    Dictionary
< string bool >  testDatas  =   new  Dictionary < string bool > ();
    testDatas.Add(
" 320902198910010033 " true );
    testDatas.Add(
" 320924198905200279 " true );
    testDatas.Add(
" 642225198410042226 " true );
    testDatas.Add(
" 642221198408061772 " true );
    testDatas.Add("64222119841111197X"true);
    testDatas.Add("64222119841111197x"true);
    testDatas.Add(
" 642221198610100384 " true );
    testDatas.Add(
" 642221198606111101 " true );
    testDatas.Add(
" 520201197411212018 " true );

    
foreach  (KeyValuePair < string bool >  testData  in  testDatas)
    {
        
bool  actual  =  StringHelper.IsIdentityCardNumber(testData.Key);
        
bool  expected  =  testData.Value;

        Assert.AreEqual
< bool > (actual, expected,  string .Format( " 身份证号{0}的测试结果应该是{1} " , testData.Key, testData.Value));
    }
}

再来是代码:
public   static   bool  IsIdentityCardNumber( string  verifyNumber)
{
    
string [] cities  =   new   string []{
        
null ,    null ,    null ,    null ,    null ,    null ,    null ,    null ,    null null ,
        
null ,    " 北京 " " 天津 " " 河北 " " 山西 " " 内蒙 " null ,    null ,    null null ,
        
null ,    " 辽宁 " " 吉林 " " 黑龙 " null ,    null ,    null ,    null ,    null null ,
        
null ,    " 上海 " " 江苏 " " 浙江 " " 安微 " " 福建 " " 江西 " " 山东 " null null ,
        
null ,    " 河南 " " 湖北 " " 湖南 " " 广东 " " 广西 " " 海南 " null ,    null null ,
        
" 重庆 " " 四川 " " 贵州 " " 云南 " " 西藏 " null ,    null ,    null ,    null null ,
        
null ,    " 陕西 " " 甘肃 " " 青海 " " 宁夏 " " 新疆 " null ,    null ,    null null ,
        
null ,    " 台湾 " null ,    null ,    null ,    null ,    null ,    null ,    null null ,
        
null ,    " 香港 " " 澳门 " null ,    null ,    null ,    null ,    null ,    null null ,
        
null ,    " 国外 " };


    Regex regex 
=   new  Regex( @" ^/d{17}(/d|x|X)$ " );
    Match match 
=  regex.Match(verifyNumber);
    
if  ( ! match.Success)
    {
        
return   false ;
    }

    verifyNumber 
=  verifyNumber.ToLower();
    
if  (cities[ int .Parse(verifyNumber.Substring( 0 2 ))]  ==   null )
    {
        
return   false ;
    }

    
string  dateString  =   string .Format( " {0}-{1}-{2} " ,
        verifyNumber.Substring(
6 4 ),
        verifyNumber.Substring(
10 2 ),
        verifyNumber.Substring(
12 2 ));

    DateTime tempResultDate 
=  DateTime.Now;
    
if  ( ! DateTime.TryParse(dateString,  out  tempResultDate))
    {
        
return   false ;
    }

    
int [] wi  =   new   int [] {  7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2  };
    
char [] cLastChar  =   new   char [] {  ' 1 ' ' 0 ' ' x ' ' 9 ' ' 8 ' ' 7 ' ' 6 ' ' 5 ' ' 4 ' ' 3 ' ' 2 '  };
    
int  sum  =   0 ;
    
for  ( int  i  =   0 ; i  <   17 ; i ++ )
    {
        sum 
+=   int .Parse(verifyNumber[i].ToString())  *  wi[i];
    }
    
int  iModeResult  =  sum  %   11 ;
    
if  (cLastChar[iModeResult].ToString()  !=  verifyNumber[ 17 ].ToString())
    {
        
return   false ;
    }

    
return   true ;
}


关于代码部分的说明:
1、代码是在签名为private string CheckCidInfo(string cid) 的基础上修改的,原作者也没有找到。
2、原代码的校验位根本就没有按照规则么,也许是我没看明白,根本就没有出现加权指数阿。
3、增加了对最后X的大小写判断。
4、格式化了一下区域,10个一行,好找一些,比如吉林的代码我确认是22,也就是第三行第三列。
5、测试用例直接采用Visual Studio .NET 2005的单元测试用例。
6、在网上还有一个例子,在计算加权的时候,直接使用verifyNumber[i]*wi[i],这样其实是把身份证号指定数字的ASCII码和加权指数相乘,而不是那个位置的值。
7、最后狗屁一下,我相信这些代码的原作者已经修复了自己的Bug,可是大家转来转去的时候,从来都不检查一下或者至少验证一下阿。

附录:

18位身份证标准在国家质量技术监督局于1999年7月1日实施的GB11643-1999《公民身份号码》中做了明确的规定。 GB11643-1999《公民身份号码》为GB11643-1989《社会保障号码》的修订版,其中指出将原标准名称"社会保障号码"更名为"公民身份号码",另外GB11643-1999《公民身份号码》从实施之日起代替GB11643-1989。GB11643-1999《公民身份号码》主要内容如下:

一、范围
该标准规定了公民身份号码的编码对象、号码的结构和表现形式,使每个编码对象获得一个唯一的、不变的法定号码。

二、编码对象
公民身份号码的编码对象是具有中华人民共和国国籍的公民。

三、号码的结构和表示形式
1、号码的结构
公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
2、地址码
表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。
3、出生日期码
表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。
4、顺序码
表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。
5、校验码
(1)十七位数字本体码加权求和公式
S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和
Ai:表示第i位置上的身份证号码数字值
Wi:表示第i位置上的加权因子
Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2)计算模
Y = mod(S, 11)
(3)通过模得到对应的校验码
Y: 0 1 2 3 4 5 6 7 8 9 10
校验码: 1 0 X 9 8 7 6 5 4 3 2

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值