Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions stdnum/kr/rrn.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@
Koreans or the issuing agency for foreigners, followed by two digits for the
community center number, one serial number and a check digit.

Numbers given out since 2020-10-05 (to newborns and when a number is
changed) have six random digits after the seventh digit, so for people born
from 2020-09-05 on only the date of birth and the seventh digit can be
checked.

More information:

* https://www.law.go.kr/lsSc.do?tabMenuId=tab18&p1=&subMenu=1&nwYn=1&section=&tabNo=&query=개인정보+보호법
* https://en.wikipedia.org/wiki/Resident_registration_number
* https://techscience.org/a/2015092901/
* https://m.go.seoul.co.kr/news/2019/12/18/20191218014003

>>> validate('971013-9019902')
'9710139019902'
Expand Down Expand Up @@ -99,14 +105,18 @@ def validate(number: str, allow_future: bool = True) -> str:
raise InvalidFormat()
if len(number) != 13:
raise InvalidLength()
get_birth_date(number, allow_future)
place_of_birth = int(number[7:9])
if place_of_birth > 96:
raise InvalidComponent()
# We cannot check the community center (CC), any information on
# valid/invalid CC digits is welcome.
if number[-1] != calc_check_digit(number[:-1]):
raise InvalidChecksum()
date_of_birth = get_birth_date(number, allow_future)
# Numbers given out since 2020-10-05 have six random digits after the
# seventh digit. A birth has to be reported within a month, so anyone
# born from 2020-09-05 on can have such a number.
if date_of_birth < datetime.date(2020, 9, 5):
place_of_birth = int(number[7:9])
if place_of_birth > 96:
raise InvalidComponent()
# We cannot check the community center (CC), any information on
# valid/invalid CC digits is welcome.
if number[-1] != calc_check_digit(number[:-1]):
raise InvalidChecksum()
return number


Expand Down
18 changes: 18 additions & 0 deletions tests/test_kr_rrn.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ Traceback (most recent call last):
InvalidComponent: ...


Numbers given out since 2020-10-05 have six random digits after the seventh
digit, so there is no place of birth or check digit to validate for people
born after the change. The date of birth is still checked.

>>> rrn.validate('210503-3999990') # born 2021-05-03
'2105033999990'
>>> rrn.validate('200905-4123456') # born 2020-09-05, reported after 2020-10-05
'2009054123456'
>>> rrn.validate('200904-4123456') # born 2020-09-04, reported before the change
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> rrn.validate('210230-3123456') # invalid date of birth 2021-02-30
Traceback (most recent call last):
...
InvalidComponent: ...


These have been randomly
`generated online <https://www.myfakeinfo.com/nationalidno/get-kr-resident-registration-number-with-name.php>`_
and should all be valid numbers.
Expand Down
Loading