import ctypes
import os
import sys
from motor.common.logger import get_logger
logger = get_logger(__name__)
def clear_passwd(password):
if not password:
return
password_len = len(password)
password_offset = sys.getsizeof(password) - password_len - 1
ctypes.memset(id(password) + password_offset, 0, password_len)
class PasswordDecryptor:
@staticmethod
def decrypt(password_file: str) -> str:
"""
Decrypt the password from the password file, and return the password as a string
Args:
password_file: The path to the password file
Returns:
The decrypted password as a string
"""
try:
if not os.path.exists(password_file):
logger.error(f"Password file does not exist: {password_file}")
return ""
with open(password_file, "r") as f:
password = f.read().strip()
return password
except Exception as e:
logger.error(f"Failed to decrypt password: {e}")
return ""