-
Notifications
You must be signed in to change notification settings - Fork 0
/
brute_force_algorithm.py
59 lines (50 loc) · 2.27 KB
/
brute_force_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import string
import time
import random
# User inputs a password to crack
password = input("Write down your password: ")
print()
print("Thank you! Your password's security is being checked...")
print()
guess = "" # Variable will contain cracked password
length = len(password) # The length of the input
characters = string.ascii_letters + string.digits + string.punctuation # Strings generated by string module
character_list = list(characters) # Sll possible password characters
start_time = time.time() # Loop start time
timeout = time.time() + 60 # Loop termination time
attempts = 0 # Number of randomly generated passwords by brute force loop
"""
This while loop is repeating a block of code which guesses the password.
The loop continues while "guess" is not equal to user input "password"
and loop termination time (60sec) did not pass.
The while loop:
- adds up attempt number with each loop
- generates random guesses in length of input
- adds result to "guess" variable
- compares "guess" variable with "password" variable
"""
while (guess != password) and timeout > time.time():
attempts = attempts + 1
# random.choices method generates random results from specific variable
# par1 is variable, par2 = k is length of returned list
guess = random.choices(character_list, k=length)
guess = "".join(guess)
# Notify user if password is cracked wuthin 60 sec. Prints time it took and attempts number.
if guess == password:
print("Your password has been cracked! You need more secure password than \"" + guess + "\".")
print()
print("It took %s seconds to hack it!" % (time.time() - start_time))
print()
print(f"I guessed it in {attempts:,} attempts.")
print()
print("For the sake of safety, let's improve your password!")
print()
# Notify user when loop terminated after 60sec and program was unable to crack password on time.
if timeout <= time.time():
print("I couldn't hack your password in less than 60sec!")
print()
print("Unfortunately hackers might have faster computer, I think we should check how secure it is!")
print()
# Print dividers and empty lines for readability
print("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *")
print()