Skip to content

Commit

Permalink
Minimize the memory footprint of sensitive data
Browse files Browse the repository at this point in the history
  • Loading branch information
zbalkan committed Sep 15, 2024
1 parent f2953ca commit 5cb1ebc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/PAN.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
import re


Expand All @@ -7,17 +8,26 @@ class PAN:
brand: str

__pan: str
__masked: str

def __init__(self, brand: str, pan: str) -> None:

self.brand, self.__pan = brand, pan
self.brand = brand
self.__masked = self.__mask_pan(pan)

def get_masked_pan(self) -> str:
# Clear the PAN from memory ASAP
del pan
gc.collect()

def __mask_pan(self, pan: str) -> str:
"""The first six and last four digits are the maximum number of digits that may be displayed"""
standardized = self.__pan.replace(' ', '').replace('-', '')
pan_out: str = standardized[0:6] + \
standardized = pan.replace(' ', '').replace('-', '')
masked: str = standardized[0:6] + \
re.sub(r'\d', '*', standardized[6:-4]) + standardized[-4:]
return f'{self.brand}:{pan_out}'
return masked

def get_masked_pan(self) -> str:
return f'{self.brand}:{self.__masked}'

@staticmethod
def is_valid_luhn_checksum(pan: str) -> bool:
Expand Down
4 changes: 4 additions & 0 deletions src/PanFinder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
from re import Pattern

from config import PANHuntConfiguration
Expand All @@ -16,6 +17,9 @@ def find(self, text: str) -> list[PAN]:
for pan in regex.findall(text):
if PAN.is_valid_luhn_checksum(pan=pan) and not config.is_excluded(pan=pan):
matches.append(PAN(brand=brand, pan=pan))
# Clear the PAN from memory ASAP
del pan
gc.collect()
break

return matches

0 comments on commit 5cb1ebc

Please sign in to comment.