[Python] How to store password and access it during runtime of program securely [duplicate]

Discussão em 'Python' iniciado por Stack, Dezembro 9, 2024.

  1. Stack

    Stack Membro Participativo

    I have an application which is invoked by user entering a password. It processes some data and occasionally needs to re-use the password.

    What is the recommend way to save the password yet avoid user access via name mangling?

    1 - Naive approach

    class C:
    def __init__(self, password: str,) -> None:
    self.__pw = password

    c=C("hide-me-well")
    print(c._C__pw)


    2 - Fernet approach

    from cryptography.fernet import Fernet

    class C:
    def __init__(self, password: str,) -> None:
    self.__key = Fernet.generate_key()
    self.__cipher = Fernet(self.__key)
    self.__encrypted_pw = self.__cipher.encrypt(password.encode('utf-8'))

    c=C("hide-me-well")
    print(c._C__cipher.decrypt(c._C__encrypted_pw).decode('utf-8'))

    Continue reading...

Compartilhe esta Página