ID card number generation, verification code calculation, ID card number generation Verification Code

Source: Internet
Author: User

ID card number generation, verification code calculation, ID card number generation Verification Code

The second generation ID card number consists of 18 digits: 6-digit location code + 8-digit Date of Birth + 3-digit sequence code + 1-digit Verification Code

Here, the verification code is calculated as follows:

The first step is the ing weight; the second step is the product; the third step is the addition of the remainder; the last step is to find the code table to get the last 1 Character

The number in the red direction is smaller and the calculation speed is faster.

import random
import time
 
# Region code. This example only lists three characters.
Areas = ('20180101', '20180101', '20180101', '20180101 ')
# Top 17 weights of ID cards
W17 = (7, 9, 10, 5, 8,
4, 2, 1, 6, 3,
7, 9, 10, 5, 8,
4, 2)
# Check bit dictionary
Crc_dict = {0: '1', 1: '0', 2: 'X', 3: '9', 4: '8', 5: '7 ', 6: '6', 7: '5', 8: '4', 9: '3', 10: '2 '}


# Generating ID card numbers
Def create_zjhm ():
While True:
#6-digit Area
Zjhm = random. choice (areas)
#8-digit Date of birth
T_start = time. mktime (1970, 1, 1, 9, 0, 0, 0, 0 ))
T_end = time. mktime (1995, 1, 1, 9, 0, 0, 0, 0 ))
Random. randrange (start = t_start, stop = t_end)
Zjhm + = time. strftime ('% Y % m % d', time. gmtime (random. randrange (t_start, stop = t_end )))
#3-digit numerical order
Zjhm + = '% 03d' % random. randrange (start = 0, stop = 999)

#1-digit verification bit. the last verification bit is calculated based on the first 17-digit number. It can also be used for ID card verification.
# Multiply the sum by the weight and divide the sum by 11 for remainder.
Crc = sum (map (lambda z: z [0] * z [1], zip (w17, [int (I) for I in zjhm]) % 11
# Map Characters Based on the remainder
Crc = crc_dict.get (crc)
If not crc:
Time. sleep (1)
Continue

Zjhm + = crc
If not zjhm or len (zjhm )! = 18:
Time. sleep (1)
Continue

Return zjhm


If _ name _ = '_ main __':
Print (create_zjhm ())
The calculation result is as follows:

610622198009132706
410901198206105966
63792219800922634x

Process finished with exit code 0

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.