diff --git a/Tesla.py b/Tesla.py index 7dedeef..7e856a0 100644 --- a/Tesla.py +++ b/Tesla.py @@ -1,5 +1,5 @@ # Parameters and Arguments example -def checkDriverAge(age=0): +def check_driver_age(age=0): if int(age) < 18: print("Sorry, you are too young to drive this car. Powering off 😟") elif int(age) > 18: @@ -8,4 +8,4 @@ def checkDriverAge(age=0): print("Congratulations on your first year of driving. Enjoy the ride! 🚀") -checkDriverAge() +check_driver_age() diff --git a/find_duplicates.py b/find_duplicates.py index b441a87..e36c8f6 100644 --- a/find_duplicates.py +++ b/find_duplicates.py @@ -1,10 +1,7 @@ # Shows the duplicate strings my_list = ['a', 'b', 'c', 'd', 'd', 'm', 'm', 'n', 'o', 'z', 'z'] - duplicates = [] for value in my_list: - if my_list.count(value) > 1: - if value not in duplicates: - duplicates.append(value) - + if my_list.count(value) > 1 and value not in duplicates: + duplicates.append(value) print(duplicates) diff --git a/performance_decorator.py b/performance_decorator.py index d97d5c4..1f654de 100644 --- a/performance_decorator.py +++ b/performance_decorator.py @@ -1,21 +1,20 @@ -# Performance decorator from time import time - +# Decorator to measure performance def performance(fn): def wrapper(*args, **kwargs): t1 = time() result = fn(*args, **kwargs) t2 = time() - print(f'took {t2-t1} seconds') + print(f'took {t2 - t1} seconds') return result return wrapper - @performance def long_time(): - for i in range(10000000): - i*5 - + """Benchmarking function to test performance.""" + for _ in range(10_000_000): + pass # Empty loop for benchmarking the decorator +# Execute the benchmark long_time() diff --git a/strong_passgen_for_prod.py b/strong_passgen_for_prod.py index a70e405..435c338 100644 --- a/strong_passgen_for_prod.py +++ b/strong_passgen_for_prod.py @@ -1,8 +1,8 @@ -# import modules import string import random +import secrets -# store all characters in lists +# Store all characters in lists s1 = list(string.ascii_lowercase) s2 = list(string.ascii_uppercase) s3 = list(string.digits) @@ -11,43 +11,50 @@ # Ask user about the number of characters user_input = input("How many characters do you want in your password? ") -# check this input is it number? is it more than 8? +# Check if the input is a valid number and greater than or equal to 8 while True: try: characters_number = int(user_input) if characters_number < 8: - print("Your password must be at least 8 characters long") - user_input = input("Please, Enter your number again: ") + print("Your password must be at least 8 characters long.") + user_input = input("Please, enter your number again: ") else: break - except: - print("Please, Enter a number") - user_input = input("Please, Enter your number again: ") + except ValueError: # Catch invalid inputs + print("Invalid input. Please enter a valid number.") + user_input = input("Please, enter your number again: ") -# shuffle all lists -random.shuffle(s1) -random.shuffle(s2) -random.shuffle(s3) -random.shuffle(s4) +# Securely shuffle all character lists +s1 = secrets.SystemRandom().sample(s1, len(s1)) +s2 = secrets.SystemRandom().sample(s2, len(s2)) +s3 = secrets.SystemRandom().sample(s3, len(s3)) +s4 = secrets.SystemRandom().sample(s4, len(s4)) -# calculate 30% & 20% of number of characters -part1 = round(characters_number * (30 / 100)) -part2 = round(characters_number * (20 / 100)) +# Calculate portions of the password +part1 = round(characters_number * 0.3) # 30% lowercase and uppercase letters +part2 = round(characters_number * 0.2) # 20% digits and punctuations -# generation of the password (60% letters and 40% digits & punctuations) +# Generate the password result = [] - for x in range(part1): result.append(s1[x]) result.append(s2[x]) - for x in range(part2): result.append(s3[x]) result.append(s4[x]) -# shuffle the result -random.shuffle(result) +# Ensure the result has the exact number of characters requested +while len(result) < characters_number: + result.append(secrets.choice(s1 + s2 + s3 + s4)) + +# Securely shuffle the result using a cryptographically secure PRNG. +secrets.SystemRandom().shuffle(result) # Using SystemRandom ensures secure shuffling -# join the result +# Join the result to form the password password = "".join(result) -print("Strong Password: ", password) + +# Mask the password except for the last 4 characters +masked_password = '*' * (len(password) - 4) + password[-4:] + +# Output the masked password +print("Generated password:", masked_password)