Making: github.com/jayknoxqu/i…
Identity Composition
The National standard of the People’s Republic of China (GB 11643-1999) stipulates that the citizenship number is a combination of characteristics code, consisting of seventeen digit body code and one digit check code.
The 18-digit combination is:
1, 1, 0, 1, 0, 2 | Y Y Y Y M M D D | 8 8 | 8 | X |
---|---|---|---|---|
Area code (6 bits) | Date of Birth code (8 bits) | Sequential code (2 bits) | Gender code (1 bit) | Check code (1 bit) |
- The area code refers to the administrative division code of the county (city, town, district) where the permanent household registration is located. For example, 110102 is Beijing – Xicheng District. But the identification numbers of residents in Hong Kong, Macao and Taiwan are only down to provincial level.
- Date of birth code indicates the Gregorian calendar year (4th digit), month (2nd digit) and day (2nd digit) in which a citizen was born.
- Sequence code refers to the serial number of people born in the same year, month and day within the area identified by the same area code.
- Gender codes An odd number indicates male and an even number indicates female.
- The last bit is the check code, which is the ISO 7064:1983,MOD 11-2 check code system. The verification code is one digit. However, if the verification code calculated by the verification code system is 10, X is used instead of 10 because the ID card number is limited to 18 digits.
Verification code calculation method
- 1.Mark the ID card number from right to left as.Is the check code;
- 2.Calculate weight coefficient
So:
i | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Wi | 7 | 9 | 10 | 5 | 8 | 4 | 2 | 1 | 6 | 3 | 7 | 9 | 10 | 5 | 8 | 4 | 2 | 1 |
- 3.To calculate
- 4.
Obtain the ID verification code using Python:
def get_check_digit(id_number):
"" Obtain verification code by ID number
check_sum = 0
for i in range(0.17):
check_sum += ((1< < (17 - i)) % 11) * int(id_number[i])
check_digit = (12 - (check_sum % 11)) % 11
return check_digit if check_digit < 10 else 'X'
Copy the code
Randomly generated ID card
From the above combination we can get the following code:
@classmethod
def generate_id(cls, sex=0):
Random generation of ID numbers, sex = 0 for female and sex = 1 for male
Generate a random area code (6 digits)
id_number = str(random.choice(const.AREA_INFO.keys()))
# Limit date of birth range (8 digits)
start, end = datetime.strptime("1960-01-01"."%Y-%m-%d"), datetime.strptime("2000-12-30"."%Y-%m-%d")
birth_days = datetime.strftime(start + timedelta(random.randint(0, (end - start).days + 1)), "%Y%m%d")
id_number += str(birth_days)
# Sequence code (2 digits)
id_number += str(random.randint(9.99))
# Gender code (1 digit)
id_number += str(random.randrange(sex, 10, step=2))
# Check code (1 digit)
return id_number + str(cls(id_number).get_check_digit())
Copy the code
Main functions of the tool class
if __name__ == '__main__':
random_sex = random.randint(0.1) # Generate male (1) or female (0) randomly
print IdNumber.generate_id(random_sex) # Generate id number randomly
print IdNumber('410326199507103197').area_id # address code :410326
print IdNumber('410326199507103197').get_area_name() Address: Ruyang County, Luoyang City, Henan Province
print IdNumber('410326199507103197').get_birthday() # birthday: 1995-7-10
print IdNumber('410326199507103197').get_age() # Age :23
print IdNumber('410326199507103197').get_sex() Gender :1(Male)
print IdNumber('410326199507103197').get_check_digit() # Check code :7
print IdNumber.verify_id('410326199507103198') # check if id is correct :False
Copy the code
[√]: address: github.com/jayknoxqu/i…