Skip to content

Commit 7cba469

Browse files
deolekararthurdejong
authored andcommitted
Add Indian virtual identity number
Closes #428
1 parent bb20121 commit 7cba469

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

stdnum/in_/vid.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# vid.py - functions for handling Indian personal virtual identity numbers
2+
#
3+
# Copyright (C) 2024 Atul Deolekar
4+
#
5+
# This library is free software; you can redistribute it and/or
6+
# modify it under the terms of the GNU Lesser General Public
7+
# License as published by the Free Software Foundation; either
8+
# version 2.1 of the License, or (at your option) any later version.
9+
#
10+
# This library is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
# Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with this library; if not, write to the Free Software
17+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18+
# 02110-1301 USA
19+
20+
"""VID (Indian personal virtual identity number).
21+
22+
VID is a temporary, revocable 16-digit random number mapped with the Aadhaar number.
23+
VID is used in lieu of Aadhaar number whenever authentication or e-KYC services
24+
are performed.
25+
26+
VID is made up of 16 digits where the last digits is a check digit
27+
calculated using the Verhoeff algorithm. The numbers are generated in a
28+
random, non-repeating sequence and do not begin with 0 or 1.
29+
30+
More information:
31+
32+
* https://uidai.gov.in/en/contact-support/have-any-question/284-faqs/aadhaar-online-services/virtual-id-vid.html
33+
* https://uidai.gov.in/images/resource/UIDAI_Circular_11012018.pdf
34+
35+
>>> validate('2341234123412341')
36+
'2341234123412341'
37+
>>> validate('2341234123412342')
38+
Traceback (most recent call last):
39+
...
40+
InvalidChecksum: ...
41+
>>> validate('1341234123412341') # number should not start with 0 or 1
42+
Traceback (most recent call last):
43+
...
44+
InvalidFormat: ...
45+
>>> validate('13412341234123')
46+
Traceback (most recent call last):
47+
...
48+
InvalidLength: ...
49+
>>> validate('2222222222222222') # number cannot be a palindrome
50+
Traceback (most recent call last):
51+
...
52+
InvalidFormat: ...
53+
>>> format('2341234123412342')
54+
'2341 2341 2341 2342'
55+
>>> mask('2341234123412342')
56+
'XXXX XXXX XXXX 2342'
57+
"""
58+
59+
import re
60+
61+
from stdnum import verhoeff
62+
from stdnum.exceptions import *
63+
from stdnum.util import clean
64+
65+
66+
_vid_re = re.compile(r'^[2-9][0-9]{15}$')
67+
"""Regular expression used to check syntax of VID numbers."""
68+
69+
70+
def compact(number):
71+
"""Convert the number to the minimal representation. This strips the
72+
number of any valid separators and removes surrounding whitespace."""
73+
return clean(number, ' -').strip()
74+
75+
76+
def validate(number):
77+
"""Check if the number provided is a valid VID number. This checks
78+
the length, formatting and check digit."""
79+
number = compact(number)
80+
if len(number) != 16:
81+
raise InvalidLength()
82+
if not _vid_re.match(number):
83+
raise InvalidFormat()
84+
if number == number[::-1]:
85+
raise InvalidFormat() # VID cannot be a palindrome
86+
verhoeff.validate(number)
87+
return number
88+
89+
90+
def is_valid(number):
91+
"""Check if the number provided is a valid VID number. This checks
92+
the length, formatting and check digit."""
93+
try:
94+
return bool(validate(number))
95+
except ValidationError:
96+
return False
97+
98+
99+
def format(number):
100+
"""Reformat the number to the standard presentation format."""
101+
number = compact(number)
102+
return ' '.join((number[:4], number[4:8], number[8:12], number[12:]))
103+
104+
105+
def mask(number):
106+
"""Masks the first 8 digits as per Ministry of Electronics and
107+
Information Technology (MeitY) guidelines."""
108+
number = compact(number)
109+
return 'XXXX XXXX XXXX ' + number[-4:]

0 commit comments

Comments
 (0)