|
| 1 | +# in_.py - functions for handling Japanese Individual Numbers (IN) |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2025 Luca Sicurello |
| 5 | +# |
| 6 | +# This library is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU Lesser General Public |
| 8 | +# License as published by the Free Software Foundation; either |
| 9 | +# version 2.1 of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This library is distributed in the hope that it will be useful,<
8000
/span> |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public |
| 17 | +# License along with this library; if not, write to the Free Software |
| 18 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | +# 02110-1301 USA |
| 20 | + |
| 21 | +"""IN (個人番号, kojin bangō, Japanese Individual Number). |
| 22 | +
|
| 23 | +The Japanese Individual Number (個人番号, kojin bangō), often referred to as My |
| 24 | +number (マイナンバー, mai nambā), is assigned to identify citizens and residents. |
| 25 | +The number consists of 12 digits where the last digit is a check digit. No |
| 26 | +personal information (such as name, gender, date of birth, etc.) is encoded |
| 27 | +in the number. |
| 28 | +
|
| 29 | +More information: |
| 30 | +
|
| 31 | + * https://en.wikipedia.org/wiki/Individual_Number |
| 32 | + * https://digitalforensic.jp/2016/03/14/column404/ |
| 33 | +
|
| 34 | +>>> validate('6214 9832 0257') |
| 35 | +'621498320257' |
| 36 | +>>> validate('6214 9832 0258') |
| 37 | +Traceback (most recent call last): |
| 38 | + ... |
| 39 | +InvalidChecksum: ... |
| 40 | +>>> validate('6214 9832 025X') |
| 41 | +Traceback (most recent call last): |
| 42 | + ... |
| 43 | +InvalidFormat: ... |
| 44 | +>>> format('621498320257') |
| 45 | +'6214 9832 0257' |
| 46 | +""" |
| 47 | + |
| 48 | +from __future__ import annotations |
| 49 | + |
| 50 | +from stdnum.exceptions import * |
| 51 | +from stdnum.util import clean, isdigits |
| 52 | + |
| 53 | + |
| 54 | +def compact(number: str) -> str: |
| 55 | + """Convert the number to the minimal representation. This strips the |
| 56 | + number of any valid separators and removes surrounding whitespace.""" |
| 57 | + return clean(number, '- ').strip() |
| 58 | + |
| 59 | + |
| 60 | +def calc_check_digit(number: str) -> str: |
| 61 | + """Calculate the check digit. The number passed should not have |
| 62 | + the check digit included.""" |
| 63 | + weights = (6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 2) |
| 64 | + s = sum(w * int(n) for w, n in zip(weights, number)) % 11 |
| 65 | + return str(-s % 11 % 10) |
| 66 | + |
| 67 | + |
| 68 | +def validate(number: str) -> str: |
| 69 | + """Check if the number is valid. This checks the length and check |
| 70 | + digit.""" |
| 71 | + number = compact(number) |
| 72 | + if len(number) != 12: |
| 73 | + raise InvalidLength() |
| 74 | + if not isdigits(number): |
| 75 | + raise InvalidFormat() |
| 76 | + if calc_check_digit(number[:-1]) != number[-1]: |
| 77 | + raise InvalidChecksum() |
| 78 | + return number |
| 79 | + |
| 80 | + |
| 81 | +def is_valid(number: str) -> bool: |
| 82 | + """Check if the number is a valid IN.""" |
| 83 | + try: |
| 84 | + return bool(validate(number)) |
| 85 | + except ValidationError: |
| 86 | + return False |
| 87 | + |
| 88 | + |
| 89 | +def format(number: str) -> str: |
| 90 | + """Reformat the number to the presentation format found on |
| 91 | + My Number Cards.""" |
| 92 | + number = compact(number) |
| 93 | + return ' '.join( |
| 94 | + (number[0:4], number[4:8], number[8:12])) |
0 commit comments