Unified Social Credit Code verification python implementation __python

Source: Internet
Author: User
Tags diff modulus


The rules of the unified Social Credit code for the new version of the business license,


# -*- coding: utf-8 -*-
'''
Created on April 5, 2017
The 18-digit unified social credit code was officially implemented on October 1, 2015.
@author: dzm
'''
# I don't use I, O, Z, S, V in the unified social credit code
SOCIAL_CREDIT_CHECK_CODE_DICT = {
                '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8 ':8, '9': 9,
                'A': 10, 'B': 11, 'C': 12, 'D': 13, E': 14,, 'F': 15,, 'G': 16, 'H': 17,, 'J ':18, 'K':19, 'L':20, 'M':21, 'N':22, 'P':23, 'Q':24,
               'R':25, 'T':26, 'U':27, 'W':28, 'X':29, 'Y':30}
# GB11714-1997 Code character set in the national organization code preparation rules
ORGANIZATION_CHECK_CODE_DICT = {
                '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8 ':8, '9': 9,
                'A':10,'B':11,'C':12, 'D':13, 'E':14, 'F':15, 'G':16, 'H':17,'I ':18, 'J':19, 'K':20, 'L':21, 'M':22, 'N':23, 'O':24,'P':25, 'Q': 26,
               'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z ':35}
 
Class UnifiedSocialCreditIdentifier(object):
    '''
    Unified social credit code
    '''
 
    Def __init__(self):
        '''
        Constructor
        '''
    Def check_social_credit_code(self,code):
        '''
        Verify the checksum of the unified social credit code
        Calculate the checksum formula:
            C9 = 31-mod(sum(Ci*Wi), 31), where Ci is the i-th character of the organization code, Wi is the weighting factor of the ith position, and C9 is the check code
        '''
        # Weighting factor at position i
        Weighting_factor = [1,3,9,27,19,26,16,17,20,29,25,13,8,24,10,30,28]
        # ontology code
        Ontology_code = code[0:17]
        #码码
        Check_code = code[17]
        # Calculate the check code
        Tmp_check_code = self.gen_check_code(weighting_factor, ontology_code, 31, SOCIAL_CREDIT_CHECK_CODE_DICT)
        If tmp_check_code==check_code:
            Return True
        Else:
            Return False
 
    Def check_organization_code(self,code):
        '''
        Verify that the organization code is correct. The rule is prepared in accordance with GB 11714.
        The 9th to 17th digits of the unified social credit code are the subject identification code (organization code), a total of nine characters.
        Calculate the checksum formula:
            C9 = 11-mod(sum(Ci*Wi),11), where Ci is the i-th character of the organization code, Wi is the weighting factor of the ith position, and C9 is the check code
        @param code: Unified Social Credit Code
        '''
        # Weighting factor at position i
        Weighting_factor = [3,7,9,10,5,8,4,2]
        #第9~17号部主主标识码(Organization Code)
        Organization_code = code[8:17]
        # ontology code
        Ontology_code=organization_code[0:8]
        #码码
        Check_code = organization_code[8]
        #
        Print organization_code,ontology_code,check_code
        # Calculate the check code
        Tmp_check_code = self.gen_check_code(weighting_factor, ontology_code, 11, ORGANIZATION_CHECK_CODE_DICT)
        If tmp_check_code==check_code:
            Return True
        Else:
            Return False
 
    Def gen_check_code(self,weighting_factor,ontology_code, modulus,check_code_dict):
        '''
        @param weighting_factor: weighting factor
        @param ontology_code: ontology code
        @param modulus: modulus
        @param check_code_dict: character dictionary
        '''
        Total = 0
        For i in range(len(ontology_code)):
            If ontology_code[i].isdigit():
                Print ontology_code[i] ,weighting_factor[i]
                Total += int(ontology_code[i]) * weighting_factor[i]
            Else:
                Total += check_code_dict[ontology_code[i]]*weighting_factor[i]
        Diff = modulus - total % modulus
        Print diff
        Return check_code_dict.keys()[check_code_dict.values()[diff]]
 
 
 
If __name__ == '__main__':
    u = UnifiedSocialCreditIdentifier()
    Print u.check_organization_code(code='91421126331832178C')
    Print u.check_social_credit_code(code='91420100052045470K')

If you follow the eye of the day to find out how to obtain business information. Said in the first of their own according to the rules of the combination of Social Credit code, and then through the Social Credit code to the National Enterprise Credit information Publicity system to launch a reptile request, then at least
4*12*4000*pow (35,8) = 432360075000000000 combinations, which is to reverse the heavens, Obviously this way is not reliable


Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.