Organization code and unified social credit code verification rules and java verification tool class

Organization code

Edit coding rule
1. The national organization code consists of eight digit (or capital Latin letter) body code and one digit (or capital Latin letter) verification code. The ontology code adopts the sequential coding method of series (i.e. partition segment). The check code is calculated according to the following formula: C9=11-MOD (∑ Ci(i=1 → 8) × Wi,11), where: MOD represents the cofunction;
I represents the sequence number of code characters from left to right; Ci represents the value of code characters on the i-th digit (see attached table for specific code characters); C9 represents the check code;
Wi -- represents the weighting factor on the i-th digit, and its value is shown in the following table: when the value of C9 is 10, the check code shall be represented by the Latin capital letter X; when the value of C9 is 11, the check code shall be represented by 0.
2. For the convenience of manual identification, a hyphen "-" shall be used to separate the ontology code and the verification code. In machine reading, hyphens are omitted. The representation is: xxxxxxxx - X
3. In order to meet the special needs of each system management, the user-defined area specifies the ontology code PDY00001 to PDY99999 as the user-defined area for each system to prepare internal organization code. The organization code prepared in the user-defined area is not the basis for information exchange between systems.

Unified social credit code

Composition feature editor
First, the organization code is embedded as the entity identification code. The uniqueness of organization code ensures that social credit code will not be repeated. In other words, the uniqueness of organization code is perfectly "inherited" to unified social credit code.
The second is to add the administrative division code before the organization code. This combination is not difficult to find, that is, the tax registration certificate number. In this way, the compatibility of unified social code is improved. In the transitional period, tax authorities can use this nesting rule to upgrade to the new credit code system more easily.
The third is to reserve the first two places for the registration authority and institution category, so that the unified social credit code can be more clear and efficient in application. The first place is convenient for the registration authority to manage, which can be used as a search item, the second place can accurately classify the organization and facilitate the detailed management.
Fourth, the main identification code of unified social credit code has a large capacity. The combination of alphanumeric numbers and exponential growth ensures that a large number of organizations can be accommodated for a long time without upgrading.
Fifthly, the number of unified social credit code is 18, which is the same as that of ID card. This ingenious design can realize the unification of registration, retrieval and filling in forms in the application of "two code management and two people" in the future.
Sixthly, the subject identification code embedded in the unified social credit code has a check bit, and its 18th bit is also a check bit. Compared with the ID card number, it is double check, which ensures the number accuracy.

java verification tool class

package ;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * Organization code / unified social credit code verification tool class
 * 
 * 1.isUniformSocialCreditCode Method used to judge whether the unified social credit code is effective
 * 2.isOrganizationCertificate Method to determine whether the organization code is valid
 * 
 * 
 *  
 */

public class RegexUtil {
	private static final String BASE_CODE_STRING = "0123456789ABCDEFGHJKLMNPQRTUWXY";
    private static final char[] BASE_CODE_ARRAY = BASE_CODE_STRING.toCharArray();
    private static final List<Character> BASE_CODES = new ArrayList<Character>();
    private static final String BASE_CODE_REGEX = "[" + BASE_CODE_STRING + "]{18}";
    private static final int[] WEIGHT = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28};

    static {
        for (char c : BASE_CODE_ARRAY) {
            BASE_CODES.add(c);
        }
    }
	
    /**
     * Weighting factor
     */
    private static int power[] = {3,7,9,10,5,8,4,2};
    /**
     * Judge whether the unified social credit code is valid
     * @param a
     * @param b
     * @return
     */
	public static boolean isUniformSocialCreditCode(String socialCreditCode) {
		if (StringUtils.isBlank(socialCreditCode) || !Pattern.matches(BASE_CODE_REGEX, socialCreditCode)) {
            return false;
        }
        char[] businessCodeArray = socialCreditCode.toCharArray();
        char check = businessCodeArray[17];
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            char key = businessCodeArray[i];
            sum += (BASE_CODES.indexOf(key) * WEIGHT[i]);
        }
        int value = 31 - sum % 31;
        return check == BASE_CODE_ARRAY[value % 31];
    }
    /**
     * Determine whether the organization code is valid
     * @param organizationCertificate
     * @return
     */
    public static boolean isOrganizationCertificate(String organizationCertificate) {
        String temp = organizationCertificate.toUpperCase();
        if (temp.contains("-")) {
            temp = temp.replace("-", "");
        }
        if(temp.length()!=9){
            return false;
        }
        // Get the first 8 bits
        String pre8 = temp.substring(0,8);
        char[] pre8chars = pre8.toCharArray();// 0~z;
        // Get check code
        String code = temp.substring(8,9);
        boolean isCode = isCode(code,sum(pre8chars));
        return isCode;
    }
 
    /**
     * Summation
     * @param bit
     * @return
     */
    private static int sum(char[] bit){
        int sum = 0;
        for(int i=0;i<bit.length;i++){
            int intTemp = bit[i]>'9'?(bit[i]-'A'+10):Integer.parseInt(bit[i]+"");
            System.out.print(" "+intTemp);
            sum +=intTemp*power[i];
        }
        return  sum;
    }
 
    /**
     * Judge whether the check code of mechanism code is consistent with the calculated check code
     * @param a
     * @param b
     * @return
     */
    private static boolean isCode(String a,int b){
        String codeTEmp = (11- b%11)==10?"X":(11- b%11)==11?0+"":(11- b%11)+"";
        return a.equals(codeTEmp);
    }
    
}

Tags: Java Apache

Posted on Thu, 04 Jun 2020 19:09:11 -0700 by pieychpi