可信空间修改
Showing
2 changed files
with
171 additions
and
0 deletions
| 1 | package com.csbr.qingcloud.portal.util; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.Map; | ||
| 5 | |||
| 6 | public class CheckDigitUtil { | ||
| 7 | private static final String VALID_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:"; | ||
| 8 | private static final Map<Character, Integer> CHAR_TO_VALUE_MAP = new HashMap<>(); | ||
| 9 | |||
| 10 | static { | ||
| 11 | for (int i = 0; i < VALID_CHARS.length(); i++) { | ||
| 12 | CHAR_TO_VALUE_MAP.put(VALID_CHARS.charAt(i), i); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | public static char calculateCheckDigit(String code) { | ||
| 17 | int weight = 1; | ||
| 18 | int total = 0; | ||
| 19 | for (int i = code.length() - 1; i >= 0; i--) { | ||
| 20 | char c = code.charAt(i); | ||
| 21 | if (!CHAR_TO_VALUE_MAP.containsKey(c)) { | ||
| 22 | throw new IllegalArgumentException("Invalid character in code: " + c); | ||
| 23 | } | ||
| 24 | int value = CHAR_TO_VALUE_MAP.get(c); | ||
| 25 | total += value * weight; | ||
| 26 | weight = (weight + 1) % 37; | ||
| 27 | if (weight == 0) { | ||
| 28 | weight = 1; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | int remainder = total % 37; | ||
| 32 | int checksum = (37 - remainder) % 37; | ||
| 33 | return VALID_CHARS.charAt(checksum); | ||
| 34 | } | ||
| 35 | |||
| 36 | public static void main(String[] args) { | ||
| 37 | String code = "1234567890L"; | ||
| 38 | System.out.println(calculateCheckDigit(code)); | ||
| 39 | } | ||
| 40 | } |
| 1 | package com.csbr.qingcloud.portal.util; | ||
| 2 | |||
| 3 | import org.apache.commons.lang3.StringUtils; | ||
| 4 | |||
| 5 | import java.security.SecureRandom; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * 标识码生成工具类 | ||
| 9 | * 1、平台、接入连接器编码 | ||
| 10 | * 2、数据产品、数据资源编码 | ||
| 11 | */ | ||
| 12 | public class IdentificationCodeUtil { | ||
| 13 | |||
| 14 | private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||
| 15 | private static SecureRandom random = new SecureRandom(); | ||
| 16 | |||
| 17 | |||
| 18 | /** | ||
| 19 | * 全域功能节点编码。类型码为“1”、主体标识码(18 位)、区域代码(4 位)、随机码(8 位)、 | ||
| 20 | * 校验码(1 位)。 | ||
| 21 | * @param mainIdentification | ||
| 22 | * @param areaCode | ||
| 23 | * @return | ||
| 24 | */ | ||
| 25 | public static String genGlobalFunctionNodeCode(String mainIdentification, | ||
| 26 | String areaCode){ | ||
| 27 | String identificationCode = String.format("1%s%s%s", mainIdentification,areaCode,generateRandomString(8)); | ||
| 28 | return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode); | ||
| 29 | } | ||
| 30 | |||
| 31 | /** | ||
| 32 | * 区域功能节点编码。类型码为“2”、主体标识码(18 位)、区域代码(4 位)、随机码(8 位)、 | ||
| 33 | * 校验码(1 位)。 | ||
| 34 | * @param mainIdentification | ||
| 35 | * @param areaCode | ||
| 36 | * @return | ||
| 37 | */ | ||
| 38 | public static String genRegionalFunctionNodeCode(String mainIdentification, | ||
| 39 | String areaCode){ | ||
| 40 | String identificationCode = String.format("2%s%s%s", mainIdentification,areaCode,generateRandomString(8)); | ||
| 41 | return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode); | ||
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * 行业功能节点编码。类型码为“3”、主体标识码(18 位)、行业代码(4 位)、随机码(8 位)、 | ||
| 46 | * 校验码(1 位) | ||
| 47 | * @param mainIdentification | ||
| 48 | * @param industryCode | ||
| 49 | * @return | ||
| 50 | */ | ||
| 51 | public static String genIndustryFunctionNodeCode(String mainIdentification, | ||
| 52 | String industryCode){ | ||
| 53 | String identificationCode = String.format("3%s%s%s", mainIdentification,industryCode,generateRandomString(8)); | ||
| 54 | return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode); | ||
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 | * 业务节点编码。类型码为“4”、主体标识码(18 位)、区域/行业代码(4 位,业务节点编码由 | ||
| 59 | * 区域功能节点分配时用区域功能节点的区域代码,业务节点编码由行业功能节点分配时用行 | ||
| 60 | * 业功能节点的行业代码)、随机码(8 位)、校验码(1 位)。 | ||
| 61 | * @param mainIdentification | ||
| 62 | * @param industryCode | ||
| 63 | * @return | ||
| 64 | */ | ||
| 65 | public static String genBusinessNodeCode(String mainIdentification, | ||
| 66 | String industryCode){ | ||
| 67 | String identificationCode = String.format("4%s%s%s", mainIdentification,industryCode,generateRandomString(8)); | ||
| 68 | return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode); | ||
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * 接入连接器编码。类型码为“5”、主体标识码(18 位)、区域/行业代码(4 位,接入连接器编 | ||
| 73 | * 码由区域功能节点分配时用区域功能节点的区域代码,接入连接器编码由行业功能节点分配 | ||
| 74 | * 时用行业功能节点的行业代码)、随机码(8 位)、校验码(1 位)。 | ||
| 75 | * @param mainIdentification | ||
| 76 | * @param industryCode | ||
| 77 | * @return | ||
| 78 | */ | ||
| 79 | public static String genConnectorCode(String mainIdentification, | ||
| 80 | String industryCode){ | ||
| 81 | String identificationCode = String.format("5%s%s%s", mainIdentification,industryCode,generateRandomString(8)); | ||
| 82 | return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode); | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * 数据产品、由类型码(1位)、主体标识码(18位)、平台区域/行业代 | ||
| 87 | * 码段(4位)、随机码(8位)、校验码(1位) | ||
| 88 | * @param mainIdentification | ||
| 89 | * @param industryCode | ||
| 90 | * @return | ||
| 91 | */ | ||
| 92 | public static String genDataProductCode(String mainIdentification, | ||
| 93 | String industryCode){ | ||
| 94 | String identificationCode = String.format("6%s%s%s", mainIdentification,industryCode,generateRandomString(8)); | ||
| 95 | return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode); | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 | * 数据资源编码、由类型码(1位)、主体标识码(18位)、平台区域/行业代 | ||
| 101 | * 码段(4位)、随机码(8位)、校验码(1位)、扩展码(最多32位)组成 | ||
| 102 | * @param mainIdentification | ||
| 103 | * @param industryCode | ||
| 104 | * @return | ||
| 105 | */ | ||
| 106 | public static String genDataResourceCode(String mainIdentification, | ||
| 107 | String industryCode, | ||
| 108 | String extensionCode){ | ||
| 109 | String identificationCode = String.format("7%s%s%s", mainIdentification,industryCode,generateRandomString(8)); | ||
| 110 | identificationCode = identificationCode + CheckDigitUtil.calculateCheckDigit(identificationCode); | ||
| 111 | if(StringUtils.isNotBlank(extensionCode)) { | ||
| 112 | return identificationCode+"."+extensionCode; | ||
| 113 | } | ||
| 114 | return identificationCode; | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | public static void main(String[] args) { | ||
| 120 | System.out.println(IdentificationCodeUtil.genGlobalFunctionNodeCode("420821199101126765", "1100")); | ||
| 121 | } | ||
| 122 | |||
| 123 | // 步骤2:生成一个随机字符串 | ||
| 124 | public static String generateRandomString(int length) { | ||
| 125 | StringBuilder sb = new StringBuilder(length); | ||
| 126 | for (int i = 0; i < length; i++) { | ||
| 127 | sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length()))); | ||
| 128 | } | ||
| 129 | return sb.toString(); | ||
| 130 | } | ||
| 131 | } |
-
Please register or sign in to post a comment