I have a sensitive data field that I want to have as shortest life cycle as possible in memory. Following the post here I have the following implementation:
class UserData:
def __init__(data):
self.user_data = data #sensitive data
def get_user_data(self):
return UserData.decrypt(self.user_data)
@staticmethod
def decrypt(data):
...
Following the post here, it seems the best I can do is to use del and make the string available for GC. I have the following implementation:
class UserData:
def __init__(data):
self.user_data = data #sensitive data
self._decrypted_user_data = None
@contextmanager
def get_user_data(self):
self._decrypted_user_data = UserData.decrypt(self.user_data)
yield
del self._decrypted_user_data
@staticmethod
def decrypt(data):
...
I have two questions:
Does the del on instance variable make the variable available for GC (i.e. is the reference count 0 on the instance variable). The reason I am asking is because I know dict returns the attributes. If not is there a way to mark the instance ready for GC instead of the whole object?
Is it safer than doing self._decrypted_user_data = None?