bfinio
Expert
Posts: 377
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Science Buddies Staff

### Re: Password Security: How easily can your password be hacked?

Ok - then you should be fine to use the len() function in your code, and save the result as a new variable that you can use instead of hard-coding the number.

josh2021
Posts: 9
Joined: Mon Nov 27, 2017 6:44 pm
Occupation: Student

### Re: Password Security: How easily can your password be hacked?

Ok, I'm a bit confused as I don't have a whole lot of prior programming experience.
I'm assuming I need to put len(wheel) under the line the defines the "wheel" variable, but I'm not exactly sure what I need to do after that.

bfinio
Expert
Posts: 377
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Science Buddies Staff

### Re: Password Security: How easily can your password be hacked?

Josh - if you don't have a lot of prior programming experience, it might be good to take a step back and do some more basic Python tutorials that show you how to use variables and loops etc. This is kind of an advanced project if you haven't done much programming before. I can give you a really brief explanation here (but won't tell you exactly what changes to make to the code).

Here's a simple example. Let's say you want to write a program that adds two numbers and store them in a variable called "sum." If the sum is bigger than 10, then you print "Yay!". That program might look like this:

sum = 2 + 3
if sum > 10:
print("Yay!")

(note that the spacing gets messed up in the forums, that "print" statement should be indented 4 spaces).

In this program, the numbers 2, 3, and 10 are all hard-coded. That's bad if I'm going to be re-using those numbers in the rest of the program, because I'll have to change them in multiple places. I could do that using variables instead, like this:

x = 2
y = 3
z = 10
sum = x + y
if sum > z
print("Yay!")

Now - pretend that I have a longer program that uses 'z' in multiple locations - I only need to change the value of z once at the beginning.

So, given all that information - can you figure out how to save len(wheel) in a variable, and then use that variable in the if-statement instead of a hard-coded number?

HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer

### Re: Password Security: How easily can your password be hacked?

Ben (bfinio) was correct when he pointed out where you change that count, but he didn't explain why the code used a number in the first place. Python is an interpreted language, meaning that every single time the computer executes a line of your code it has to figure out what you mean The more functions you put in each line, the longer it takes. Part of the goal is to find as many passwords as you can but also to find them as fast as possible.

You can use the length 84 in one of three ways:
1) Just use the number
2) Set the number into 'wheel_length' and use that
3) Use the len() function.

Consider the following hunk of code:

Code: Select all

import time

starttime = time.time()
for i in range(1,1000000):
pass
endtime = time.time()
emptylooptime = 1000*(endtime - starttime)
print("One million empty loops took ",emptylooptime," milliseconds")

starttime = time.time()
for i in range(1,1000000):
a = 84
endtime = time.time()
time1 = 1000*(endtime - starttime) - emptylooptime
print("One million operations took ",time1," milliseconds")

starttime = time.time()
wheel = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234"
wheel_length = len(wheel)
for i in range(1,1000000):
a = wheel_length
endtime = time.time()
time2 = 1000*(endtime - starttime) - emptylooptime
print("Using a variable, one million operations took ",time2," milliseconds")

starttime = time.time()
wheel = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234"
for i in range(1,1000000):
a = len(wheel)
endtime = time.time()
time3 = 1000*(endtime - starttime) - emptylooptime
print("Using len(), one million operations took ",time3," milliseconds")
print("Using the variable takes ",100*((time2/time1)-1),"% more time")
print("Using len() in the loop takes ",100*((time3/time1)-1),"% more time")

When you run it, you get something like this:

One million empty loops took 51.98812484741211 milliseconds

One million operations took 27.846813201904297 milliseconds
Using a variable, one million operations took 45.683860778808594 milliseconds
Using len(), one million operations took 99.29084777832031 milliseconds

Using the variable takes 64.05417900991455 % more time
Using len() in the loop takes 256.5609000154112 % more time

What this tells you is that it is measurably faster to simply replace the 62 with the new number. If you really don't want to, at least set it in a variable since that's less of a performance penalty than "if pass_wheel_array[i] > len(wheel):". Your testing time is going to be really long anyway with long passwords, so you might not want to make it worse than it has to be.