Skip to content

Commit

Permalink
Update encryptor.py
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergandhi authored Oct 11, 2024
1 parent 62949ab commit 4c726ea
Showing 1 changed file with 3 additions and 31 deletions.
34 changes: 3 additions & 31 deletions main/encryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,10 @@

sys.dont_write_bytecode = True

def generate_key(password: str) -> bytes:
def generate_key(password: str, salt: bytes) -> bytes:
"""
Generates an encryption key based on a password using PBKDF2HMAC.
"""
salt = os.urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password.encode()))
return key, salt

def load_key(password: str, salt: bytes) -> bytes:
"""
Loads the encryption key using the provided password and salt.
"""
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
Expand Down Expand Up @@ -68,22 +53,9 @@ def validate_password(password: str) -> bool:
print("Password should contain at least one uppercase letter.")
return False
if not re.search("[0-9]", password):
print("Password should contain at least one number.")
print("Password should contain at least one digit.")
return False
if not re.search("[!@#$%^&*(),.?\":{}|<>]", password):
if not re.search("[@#$%^&+=]", password):
print("Password should contain at least one special character.")
return False
if re.search(r'(.)\1\1', password):
print("Password should not contain sequences of three or more repeating characters.")
return False
return True

def update_encryption_key(old_key: bytes, new_key: bytes, passwords: dict) -> dict:
"""
Re-encrypts all passwords with the new key.
"""
updated_passwords = {}
for service, encrypted_password in passwords.items():
decrypted_password = decrypt_message(encrypted_password.encode(), old_key)
updated_passwords[service] = encrypt_message(decrypted_password, new_key).decode()
return updated_passwords

0 comments on commit 4c726ea

Please sign in to comment.