## [Python] validity test of ID number

XavierJ 2021-01-21 06:06:01
python validity test id number

# 1. Preface

National standard of the people's Republic of China GB 11643-1999《 Citizenship number 》 Specified in the ： The citizenship number is the signature combination code , from 17 Bit number ontology code and 1 Bit check code composition .

18 What's the way of bit number combination ：

among , The last bit is the check code , It is from the front 17 The number of bits is calculated in some way . The check code is one digit , But if the last check code is used, the check code calculated by the system is “10”, The ID number is 18 The regulation of bit , with “X” Substitution check code “10”.

The calculation method is as follows ：

1. Mark the ID number from left to right. a 1 , a 2 , ⋯ , a 18 \displaystyle a_{1},a_{2},\cdots ,a_{18} ; a 18 \displaystyle a_{18} It is the check code ;
2. Calculate the weight coefficient W i = 2 18 − i   m o d   11 \displaystyle W_{i}=2^{18-i}\ {\bmod {\ }}{11} ; among   m o d   \displaystyle \ \bmod {\ } Represents the remainder .
i i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
W i W_{i} 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
1. Calculation S = ∑ i = 1 17 a i ⋅ W i \displaystyle S=\sum _{i=1}^{17}a_{i}\cdot W_{i}
2. a 18 = ( 12 − ( S   m o d 1 1 ) ) m o d 1 1 \displaystyle a_{18}=(12-(S\ {\bmod {1}}1)){\bmod {1}}1

# 2. Code

def main(id):
sum = 0
for index, item in enumerate(id[:-1]):
sum += 2**(17-index) % 11*int(item)
num = (12 - sum % 11) % 11
if num < 10:
return ' Check by ' if id[-1] == str(num) else f' Check failed , The correct ending number should be ：{num}'
else:
return ' Check by ' if id[-1] == 'X' else f' Check failed , The correct ending number should be ：{num}'
if __name__ == '__main__':
test_id_1 = '532527195503267352'
test_id_2 = '53252719550326735X'
print(main(test_id_1))
print(main(test_id_2))
''' Output
Check by
Check failed , The correct ending number should be ：2
'''


# 3. Be careful

The correct check code is the necessary condition for the validity of ID number. , Not sufficient conditions . To ensure the validity of ID number. , Other conditions should also be met , For example, the number of digits must be 18 position , The address code shall comply with the provisions of the people's Republic of China on the code of administrative divisions , Also, the birth date code should be legal and so on . If you are interested , You can program it yourself . Of course , Here is also a packaged one python Third party Library id-validator.

## 3.1. install

pip install id-validator


## 3.2. Use

### 3.2.1. Verify the legitimacy of ID number.

Verify that ID number is legal. , Legal return True, Illegal return False：

>>> from id_validator import validator
>>> validator.is_valid('440308199901101512')
True
>>> validator.is_valid('44030819990110151X')
False


### 3.2.2. Get ID number information

When the ID number is legal , Return analysis information （ region 、 Date of birth 、 The constellation 、 the Chinese zodiac 、 Gender 、 Check bit ）, Illegal return False：

>>> from id_validator import validator
>>> validator.get_info('440308199901101512')
{

"abandoned": 0,
"address": " Yantian District, Shenzhen City, Guangdong Province ",
" Guangdong province, ",
" shenzhen ",
" Yantian district "
],
"age": 22,
"birthday_code": "1999-01-10",
"constellation": " Capricornus ",
"chinese_zodiac": " Mao rabbit ",
"sex": 1,
"length": 18,
"check_bit": "2"
}


### 3.2.3. Generate false data that can be verified

Forgery of identity card in accordance with verification ：

>>> from id_validator import validator
>>> validator.fake_id()
'410704200302268552'