-
Notifications
You must be signed in to change notification settings - Fork 3
/
rrn.py
174 lines (135 loc) · 4.22 KB
/
rrn.py
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
167
168
169
170
171
172
173
174
import itertools
import re
from datetime import date, datetime
from typing import Optional
__version__ = '0.2.0'
HYPHEN = re.compile('[-–]')
BIRTH = 0, 6
MONTH_OF_BIRTH = 0, 4
MONTH_OF_BIRTH_FORMAT = '%y%m'
DAY_OF_BIRTH_LITERAL_FORMAT = '%y%m%d'
DAY_OF_BIRTH_DATE_FORMAT = '%Y%m%d'
SEX = 6
LOC = 7, 9
MAX_LOC = 97
HASH = 12
HASH_BASE = 11
def _validate_month_of_birth(rrn: str) -> bool:
try:
return datetime.strptime(
rrn[slice(*MONTH_OF_BIRTH)].ljust(MONTH_OF_BIRTH[1], '1'),
MONTH_OF_BIRTH_FORMAT
) is not None if len(rrn) >= 3 else True
except ValueError:
return False
def _validate_day_of_birth(rrn: str) -> bool:
try:
return datetime.strptime(
rrn[slice(*BIRTH)].ljust(BIRTH[1], '0'),
DAY_OF_BIRTH_LITERAL_FORMAT
) is not None if len(rrn) >= 5 else True
except ValueError:
return False
def _validate_birth(rrn: str) -> bool:
return _validate_month_of_birth(rrn) and _validate_day_of_birth(rrn)
def _validate_location(rrn: str) -> bool:
try:
return int(rrn[slice(*LOC)]) < MAX_LOC
except (TypeError, ValueError):
return True
def _validate_hash(rrn: str) -> bool:
try:
h = int(rrn[HASH])
s = sum(
a * int(b) for a, b in zip(
itertools.cycle(range(2, 10)),
rrn[:HASH]
)
)
expected = (HASH_BASE - (s % HASH_BASE)) % 10
return h == expected
except IndexError:
return True
def _is_valid_domestic_rrn(rrn: str) -> bool:
return (
rrn.isdigit() and
_validate_birth(rrn) and
_validate_location(rrn) and
_validate_hash(rrn)
)
def _is_valid_foreign_rrn(rrn: str) -> bool:
return rrn.isdigit() and _validate_birth(rrn)
def is_valid_rrn(rrn: str) -> bool:
"""
Validate given RRN and returns if it might be valid or not.
:param rrn: RRN string
:type rrn: str
:return: validity
:rtype: bool
"""
try:
rrn = HYPHEN.sub('', rrn)
if is_foreign(rrn):
return _is_valid_foreign_rrn(rrn)
else:
return _is_valid_domestic_rrn(rrn)
except TypeError:
return False
def _is_birthday_corresponding(rrn: str, birthday: date) -> Optional[bool]:
try:
return datetime.strptime(
'{century}{rrn}'.format(
century=birthday.year // 100,
rrn=rrn[slice(*BIRTH)]
),
DAY_OF_BIRTH_DATE_FORMAT
).date() == birthday
except (TypeError, ValueError):
return None
def _is_sex_corresponding(rrn: str, female: bool) -> Optional[bool]:
try:
return (int(rrn[SEX]) % 2 == 0) == female
except IndexError:
return None
def _is_foreignness_corresponding(rrn: str, foreign: bool) -> Optional[bool]:
f = is_foreign(rrn)
return f == foreign if f is not None else None
def is_foreign(rrn: str) -> Optional[bool]:
"""
Check if given RRN literal is foreigner or not.
It returns None when given RRN literal is too short to determine.
:param rrn: RRN literal
:return: expectation to be foreigner or not
"""
try:
return 5 <= int(rrn[SEX]) <= 8
except IndexError:
return None
def is_corresponding_rrn(
rrn: str,
*,
birthday: Optional[date]=None,
foreign: Optional[bool]=None,
female: Optional[bool]=None
) -> bool:
"""
Check given RRN if it corresponds with given information or not.
It returns True still if correspondence is undecidable. (ex. 6-digit RRN
literal does not contain any information about sex)
:param rrn: RRN literal
:param birthday: expected date of birth
:param foreign: expected to be foreigner or not
:param female: expected to be female or not
:return: correspondence
"""
try:
rrn = HYPHEN.sub('', rrn)
assert rrn.isdigit()
parts = (
birthday is None or _is_birthday_corresponding(rrn, birthday),
foreign is None or _is_foreignness_corresponding(rrn, foreign),
female is None or _is_sex_corresponding(rrn, female)
)
return all(p is None or p for p in parts)
except (AssertionError, TypeError):
return False