f07c9996 by xu

可信空间连接器入住接口

1 parent 34071a3e
......@@ -31,19 +31,20 @@ public class IDValidator {
if (idCard == null || !idCardPattern.matcher(idCard).matches()) {
return false;
}
// 2. 校验校验码
char[] idChars = idCard.toUpperCase().toCharArray(); // 统一转为大写
int sum = 0;
for (int i = 0; i < 17; i++) {
// 前17位必须为数字(正则已保证,此处可简化)
int num = idChars[i] - '0';
sum += num * WEIGHTS[i];
}
// 计算校验码
int checkCodeIndex = sum % 11;
char calculatedCheckCode = CHECK_CODES[checkCodeIndex];
// 对比最后一位校验码
return idChars[17] == calculatedCheckCode;
return true;
// // 2. 校验校验码
// char[] idChars = idCard.toUpperCase().toCharArray(); // 统一转为大写
// int sum = 0;
// for (int i = 0; i < 17; i++) {
// // 前17位必须为数字(正则已保证,此处可简化)
// int num = idChars[i] - '0';
// sum += num * WEIGHTS[i];
// }
// // 计算校验码
// int checkCodeIndex = sum % 11;
// char calculatedCheckCode = CHECK_CODES[checkCodeIndex];
// // 对比最后一位校验码
// return idChars[17] == calculatedCheckCode;
}
/**
......@@ -77,6 +78,7 @@ public class IDValidator {
public static void main(String[] args) {
// 测试案例
String[] testCases = {
"42019880101XXXX",
"110101199001011234", // 合法格式+正确校验码
"11010119900101123X", // 合法格式+正确校验码(X)
"110101199001011235", // 合法格式+错误校验码
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!