-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastChecker.java
166 lines (150 loc) · 4.81 KB
/
FastChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
public class FastChecker {
//проверка ИИН
public static boolean softCheckIinAndBin(String value){
if(value == null) return false;
int len;
if( (len = value.length()) != 12) {
return false;
}
for(int i = 0; i < len; ++i) {
if(value.charAt(i) < '0' || value.charAt(i) > '9') {
return false;
}
}
return true;
}
//полная проверка ИИН
public static boolean fullCheckIin(String value){
return softCheckIinAndBin(value)
&& checkBirthDate(value)
&& checkIinBinSum(value);
}
private static boolean checkBirthDate(String value) {
final char m1 = value.charAt(2);
final char m2 = value.charAt(3);
if(m1 == '0' && m2 == '2') {
final char d1 = value.charAt(4);
final char d2 = value.charAt(5);
final char y1 = value.charAt(0);
final char y2 = value.charAt(1);
char last;
if(y1 == 0 && y2 == 0 && (value.charAt(6) >= '1' && value.charAt(6) < '4')) {
last = '8';
} else {
switch (y1) {
case '0':
case '2':
case '4':
case '6':
case '8':
switch (y2) {
case '0':
case '4':
case '8':
last = '9';
break;
default:
last = '8';
break;
}
break;
case '1':
case '3':
case '5':
case '7':
case '9':
switch (y2) {
case '2':
case '6':
last = '9';
break;
default:
last = '8';
break;
}
break;
default:
return false;
}
}
switch (d1) {
case '0':
if(d2 < '1' || d2 > '9') {
return false;
}
break;
case '1':
if(d2 < '0' || d2 > '9') {
return false;
}
break;
case '2':
if(d2 < '0' || d2 > last) {
return false;
}
break;
default:
return false;
}
} else {
char last;
if(m1 == '0' && (m2 == '1' || m2 == '3' || m2 == '5' || m2 == '7' || m2 == '8')
|| m1 == '1' && (m2 == '0' || m2 == '2')) {
last = '1';
} else if (m1 == '0' && (m2 == '4' || m2 == '6' || m2 == '9')
|| m1 == '1' && m2 == '1') {
last = '0';
} else {
return false;
}
final char d1 = value.charAt(4);
final char d2 = value.charAt(5);
switch (d1) {
case '0':
if(d2 < '1' || d2 > '9') {
return false;
}
break;
case '1':
case '2':
if(d2 < '0' || d2 > '9') {
return false;
}
break;
case '3':
if(d2 < '0' || d2 > last) {
return false;
}
break;
default:
return false;
}
}
return true;
}
//проверка контрольной суммы ИИН
private static boolean checkIinBinSum(String value){
int endDigit = value.charAt(11) - '0';
int sum = 0;
for (int i = 0; i < 11; ++i) {
sum += (i + 1) * (value.charAt(i) - '0');
}
int k = sum % 11;
if (k == 10) {
sum = 0;
int t;
for (int i = 0; i < 11; ++i) {
t = i + 3;
if(t >= 11) {
t -= 11;
}
sum += t * (value.charAt(i) - '0');
}
k = sum % 11;
if (k == 10 || k != endDigit) {
return false;
}
}
return k == endDigit;
}
}