20188200 by xu

可信空间修改

1 parent 7388987d
package com.csbr.qingcloud.portal.util;
import java.util.HashMap;
import java.util.Map;
public class CheckDigitUtil {
private static final String VALID_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ$*+-./:";
private static final Map<Character, Integer> CHAR_TO_VALUE_MAP = new HashMap<>();
static {
for (int i = 0; i < VALID_CHARS.length(); i++) {
CHAR_TO_VALUE_MAP.put(VALID_CHARS.charAt(i), i);
}
}
public static char calculateCheckDigit(String code) {
int weight = 1;
int total = 0;
for (int i = code.length() - 1; i >= 0; i--) {
char c = code.charAt(i);
if (!CHAR_TO_VALUE_MAP.containsKey(c)) {
throw new IllegalArgumentException("Invalid character in code: " + c);
}
int value = CHAR_TO_VALUE_MAP.get(c);
total += value * weight;
weight = (weight + 1) % 37;
if (weight == 0) {
weight = 1;
}
}
int remainder = total % 37;
int checksum = (37 - remainder) % 37;
return VALID_CHARS.charAt(checksum);
}
public static void main(String[] args) {
String code = "1234567890L";
System.out.println(calculateCheckDigit(code));
}
}
package com.csbr.qingcloud.portal.util;
import org.apache.commons.lang3.StringUtils;
import java.security.SecureRandom;
/**
* 标识码生成工具类
* 1、平台、接入连接器编码
* 2、数据产品、数据资源编码
*/
public class IdentificationCodeUtil {
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static SecureRandom random = new SecureRandom();
/**
* 全域功能节点编码。类型码为“1”、主体标识码(18 位)、区域代码(4 位)、随机码(8 位)、
* 校验码(1 位)。
* @param mainIdentification
* @param areaCode
* @return
*/
public static String genGlobalFunctionNodeCode(String mainIdentification,
String areaCode){
String identificationCode = String.format("1%s%s%s", mainIdentification,areaCode,generateRandomString(8));
return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode);
}
/**
* 区域功能节点编码。类型码为“2”、主体标识码(18 位)、区域代码(4 位)、随机码(8 位)、
* 校验码(1 位)。
* @param mainIdentification
* @param areaCode
* @return
*/
public static String genRegionalFunctionNodeCode(String mainIdentification,
String areaCode){
String identificationCode = String.format("2%s%s%s", mainIdentification,areaCode,generateRandomString(8));
return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode);
}
/**
* 行业功能节点编码。类型码为“3”、主体标识码(18 位)、行业代码(4 位)、随机码(8 位)、
* 校验码(1 位)
* @param mainIdentification
* @param industryCode
* @return
*/
public static String genIndustryFunctionNodeCode(String mainIdentification,
String industryCode){
String identificationCode = String.format("3%s%s%s", mainIdentification,industryCode,generateRandomString(8));
return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode);
}
/**
* 业务节点编码。类型码为“4”、主体标识码(18 位)、区域/行业代码(4 位,业务节点编码由
* 区域功能节点分配时用区域功能节点的区域代码,业务节点编码由行业功能节点分配时用行
* 业功能节点的行业代码)、随机码(8 位)、校验码(1 位)。
* @param mainIdentification
* @param industryCode
* @return
*/
public static String genBusinessNodeCode(String mainIdentification,
String industryCode){
String identificationCode = String.format("4%s%s%s", mainIdentification,industryCode,generateRandomString(8));
return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode);
}
/**
* 接入连接器编码。类型码为“5”、主体标识码(18 位)、区域/行业代码(4 位,接入连接器编
* 码由区域功能节点分配时用区域功能节点的区域代码,接入连接器编码由行业功能节点分配
* 时用行业功能节点的行业代码)、随机码(8 位)、校验码(1 位)。
* @param mainIdentification
* @param industryCode
* @return
*/
public static String genConnectorCode(String mainIdentification,
String industryCode){
String identificationCode = String.format("5%s%s%s", mainIdentification,industryCode,generateRandomString(8));
return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode);
}
/**
* 数据产品、由类型码(1位)、主体标识码(18位)、平台区域/行业代
* 码段(4位)、随机码(8位)、校验码(1位)
* @param mainIdentification
* @param industryCode
* @return
*/
public static String genDataProductCode(String mainIdentification,
String industryCode){
String identificationCode = String.format("6%s%s%s", mainIdentification,industryCode,generateRandomString(8));
return identificationCode+CheckDigitUtil.calculateCheckDigit(identificationCode);
}
/**
* 数据资源编码、由类型码(1位)、主体标识码(18位)、平台区域/行业代
* 码段(4位)、随机码(8位)、校验码(1位)、扩展码(最多32位)组成
* @param mainIdentification
* @param industryCode
* @return
*/
public static String genDataResourceCode(String mainIdentification,
String industryCode,
String extensionCode){
String identificationCode = String.format("7%s%s%s", mainIdentification,industryCode,generateRandomString(8));
identificationCode = identificationCode + CheckDigitUtil.calculateCheckDigit(identificationCode);
if(StringUtils.isNotBlank(extensionCode)) {
return identificationCode+"."+extensionCode;
}
return identificationCode;
}
public static void main(String[] args) {
System.out.println(IdentificationCodeUtil.genGlobalFunctionNodeCode("420821199101126765", "1100"));
}
// 步骤2:生成一个随机字符串
public static String generateRandomString(int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
}
return sb.toString();
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!