可信空间连接器入住接口
Showing
1 changed file
with
15 additions
and
13 deletions
| ... | @@ -31,19 +31,20 @@ public class IDValidator { | ... | @@ -31,19 +31,20 @@ public class IDValidator { |
| 31 | if (idCard == null || !idCardPattern.matcher(idCard).matches()) { | 31 | if (idCard == null || !idCardPattern.matcher(idCard).matches()) { |
| 32 | return false; | 32 | return false; |
| 33 | } | 33 | } |
| 34 | // 2. 校验校验码 | 34 | return true; |
| 35 | char[] idChars = idCard.toUpperCase().toCharArray(); // 统一转为大写 | 35 | // // 2. 校验校验码 |
| 36 | int sum = 0; | 36 | // char[] idChars = idCard.toUpperCase().toCharArray(); // 统一转为大写 |
| 37 | for (int i = 0; i < 17; i++) { | 37 | // int sum = 0; |
| 38 | // 前17位必须为数字(正则已保证,此处可简化) | 38 | // for (int i = 0; i < 17; i++) { |
| 39 | int num = idChars[i] - '0'; | 39 | // // 前17位必须为数字(正则已保证,此处可简化) |
| 40 | sum += num * WEIGHTS[i]; | 40 | // int num = idChars[i] - '0'; |
| 41 | } | 41 | // sum += num * WEIGHTS[i]; |
| 42 | // 计算校验码 | 42 | // } |
| 43 | int checkCodeIndex = sum % 11; | 43 | // // 计算校验码 |
| 44 | char calculatedCheckCode = CHECK_CODES[checkCodeIndex]; | 44 | // int checkCodeIndex = sum % 11; |
| 45 | // 对比最后一位校验码 | 45 | // char calculatedCheckCode = CHECK_CODES[checkCodeIndex]; |
| 46 | return idChars[17] == calculatedCheckCode; | 46 | // // 对比最后一位校验码 |
| 47 | // return idChars[17] == calculatedCheckCode; | ||
| 47 | } | 48 | } |
| 48 | 49 | ||
| 49 | /** | 50 | /** |
| ... | @@ -77,6 +78,7 @@ public class IDValidator { | ... | @@ -77,6 +78,7 @@ public class IDValidator { |
| 77 | public static void main(String[] args) { | 78 | public static void main(String[] args) { |
| 78 | // 测试案例 | 79 | // 测试案例 |
| 79 | String[] testCases = { | 80 | String[] testCases = { |
| 81 | "42019880101XXXX", | ||
| 80 | "110101199001011234", // 合法格式+正确校验码 | 82 | "110101199001011234", // 合法格式+正确校验码 |
| 81 | "11010119900101123X", // 合法格式+正确校验码(X) | 83 | "11010119900101123X", // 合法格式+正确校验码(X) |
| 82 | "110101199001011235", // 合法格式+错误校验码 | 84 | "110101199001011235", // 合法格式+错误校验码 | ... | ... |
-
Please register or sign in to post a comment