Skip to content

Commit

Permalink
Merge pull request #7 from Arkajit-Datta/main
Browse files Browse the repository at this point in the history
Major Refactoring of Decode.py
  • Loading branch information
tanmoysrt committed Oct 20, 2022
2 parents 236d5b7 + 2499cc2 commit 55b2f2e
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions pyaadhaar/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,51 @@
class AadhaarSecureQr:
# This is the class for Adhaar Secure Qr code.. In this version of code the data is in encrypted format
# The special thing of this type of QR is that we can extract the photo of user from the data
# This class now supports 2022 version of Aadhaar QR codes [version-2]
# For more information check here : https://103.57.226.101/images/resource/User_manulal_QR_Code_15032019.pdf

def __init__(self, base10encodedstring):
self.base10encodedstring = base10encodedstring
self.details = ["version","email_mobile_status","referenceid", "name", "dob", "gender", "careof", "district", "landmark",
"house", "location", "pincode", "postoffice", "state", "street", "subdistrict", "vtc"]
self.delimeter = []
self.delimeter = [-1]
self.data = {}

bytes_array = base10encodedstring.to_bytes(5000, 'big').lstrip(b'\x00')

self._convert_base10encoded_to_decompressed_array()
self._check_aadhaar_version()
self._create_delimeter()
self._extract_info_from_decompressed_array()

def _convert_base10encoded_to_decompressed_array(self):
# This function converts base10encoded string to a decompressed array
bytes_array = self.base10encodedstring.to_bytes(5000, 'big').lstrip(b'\x00')
self.decompressed_array = zlib.decompress(
bytes_array, 16+zlib.MAX_WBITS)

# FIX: Patch for the 2022 Aadhaar Cards
# The decoded data from these cards is reproducing a 'V2' at 0th and 1st index
def _check_aadhaar_version(self):
# This function will check for the new 2022 version-2 Aadhaar QRs
# If not found it will remove the "version" key from self.details, Defaulting to normal Secure QRs
if not self.decompressed_array[0:2].decode("ISO-8859-1") == 'V2':
self.details.pop(0)

self.delimeter.append(-1)

def _create_delimeter(self):
# This function creates the delimeter which is used to extract the information from the decompressed array
for i in range(len(self.decompressed_array)):
if self.decompressed_array[i] == 255:
self.delimeter.append(i)

def _extract_info_from_decompressed_array(self):
for i in range(len(self.details)):
self.data[self.details[i]] = self.decompressed_array[self.delimeter[i] + 1:self.delimeter[i+1]].decode("ISO-8859-1")

self.data['adhaar_last_4_digit'] = self.data['referenceid'][0:4]
self.data['adhaar_last_digit'] = self.data['referenceid'][3]

if self.data['email_mobile_status'] == "0":
self.data['email'] = "no"
self.data['mobile'] = "no"
elif self.data['email_mobile_status'] == "1":
self.data['email'] = "yes"
self.data['mobile'] = "no"
elif self.data['email_mobile_status'] == "2":
self.data['email'] = "no"
self.data['mobile'] = "yes"
elif self.data['email_mobile_status'] == "3":
self.data['email'] = "yes"
self.data['mobile'] = "yes"
# Default values to 'email' and 'mobile
self.data['email'] = False
self.data['mobile'] = False
# Updating the fields of 'email' and 'mobile'
if int(self.data['email_mobile_status']) == 3 or int(self.data['email_mobile_status']) == 1:
self.data['email'] = True
if int(self.data['email_mobile_status']) == 3 or int(self.data['email_mobile_status']) == 2:
self.data['mobile'] = True

def decodeddata(self):
# Will return the personal data in a dictionary format
Expand All @@ -71,15 +74,11 @@ def signedData(self):

def isMobileNoRegistered(self):
# Will return True if mobile number is registered
if self.data['mobile'] == "yes":
return True
return False
return self.data['mobile']

def isEmailRegistered(self):
# Will return True if email id is registered
if self.data['email'] == "yes":
return True
return False
return self.data['email']

def sha256hashOfEMail(self):
# Will return the hash of the email id
Expand Down

0 comments on commit 55b2f2e

Please sign in to comment.