My Crazy Password Generator Algorithm

My Crazy Password Generator Algorithm

ยท

4 min read

Introduction

It's been a while since I published an article, but here I am. Hopefully, I won't go off the radar again. In high school, I wanted to find a way to create passwords without memorising them. I was looking for a lazy way to be secure, so I devised an algorithm to do just that. I'll share its implementation in python as we go through each step.

How It Works

Step 1

First, we must write out all the available characters we want to use in our password. In our case, we will use the following:

  • lowercase letters
  • uppercase letters
  • digits
  • and symbols.

Python Implementation

Python has a built-in module called string. It holds variables that already have the characters we defined above, so there is no need to hardcode them.

code.png

  • string.ascii_letters holds both uppercase and lowercase letters.
  • string.digits holds numbers from 0 to 9.
  • string.punctuation holds all the punctuations in ASCII.
  • Add a space (" ") to cater for entries with spaces.
  • We stored each character as an element in a list.

Step 2

Next, we need to assign numbers to each character we wrote down in the first step.


๐Ÿ’ก This would be useful for encoding our string into a password.


Python Implementation

We can use the dictionary data structure to implement the second step in python. The keys will be the elements in the list, and their indices will be our values.

code1.png

  • We start by creating a dictionary called init_dict.
  • To get the value and key pairs, we use the enumerate function.

๐Ÿ“ The enumerate object yields pairs containing a count (from the start, which defaults to zero) and a value yielded by the iterable argument.


You might be wondering: "Why don't we just use lists ๐Ÿคท๐Ÿฝโ€โ™‚?" Well, I trust you'd figure it out in the successive steps. However, here's a hint: My algorithm was inspired by Caesar's cipher.

Onto the next...

Step 3

We need to make a function to determine the transformation of a single character. This is more like a key in cryptography. Instead of using a key, we replace it with a function (a math model).

Python Implementation

We need to define a function to handle the transformation of a character based on a specified mathematical function.

code2.png

  • We define a transformer function that takes a letter as an argument.
  • Using the dictionary we created earlier, we find the index of the letter and use it in our math model, new_x.
  • The definition of new_x can be customised to your heart's desire.

$$ \text{The function I used was: } f(x) = 7x - 5 $$

Notice that I computed the modulo of my function with respect to the length of our initial list. Can you guess why? Feel free to share in the comments below ๐Ÿ‘‡๐Ÿป

Step 4

After getting the transformed value, I used that as a key to find its corresponding character from our initial list. We continue this for the rest of the letters.

When I first implemented this, I found a way to reverse the algorithm such that I can use the function and the generated password to find the initial entry. There were lots of calculations, but its been over 5 years - I forgot what I did ๐Ÿซ 

Before we start drifting away... Let's finish up our algorithm

Python Implementation

For this part, we can define a function to handle the task.

code3.png

  • We define a function, transform, which returns our password.
  • It uses the value returned from our transformer function as an index in the initial list we defined.
  • It iterates over each letter and stores it in the password variable.

Making Use of Our Algorithm

code4.png

code5.png


๐ŸŽŠ๐ŸŽ‰ Congratulations, you just used my crazy algorithm. ๐Ÿ™‚


I'll see you in the next article. Keep learning!!!

The source code used in this article is available on GitHub: ๐Ÿ‘‰๐ŸปSee code

ย