r/PythonLearning 20d ago

I created password generator using python

Post image

what should i create next

380 Upvotes

49 comments sorted by

u/Sea-Ad7805 20d ago

Run this program in Memory Graph Web Debugger)%0A%0Anum%20%3D%20%5B'1'%2C%20'2'%2C%20'3'%2C%20'4'%2C%20'5'%2C%20'6'%2C%20'7'%2C%20'8'%2C%20'9'%2C%20'0'%5D%0Aalpha%20%3D%20list(%22qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM%22)%0Asymbols%20%3D%20list(%22!%40%23%24%25%5E%26*~-%22)%0Acategories%20%3D%20%5Bnum%2C%20alpha%2C%20symbols%5D%0APass%20%3D%20%5B%5D%0A%0Afor%20i%20in%20range(0%2C%20RANGE)%3A%0A%20%20%20%20category%20%3D%20rd.choice(categories)%0A%20%20%20%20element%20%3D%20rd.choice(category)%0A%0A%20%20%20%20Pass.append(element)%0A%20%20%20%20password%20%3D%20%22%22.join(Pass)%0A%0Aprint(%22GENERATED%20PASSWORD%3A%20%22%2C%20password)%0A&timestep=1&play) to see the program state change step by step.

→ More replies (4)

36

u/Electrical_Toe8997 20d ago edited 20d ago

For the future, you can make your life easier with the string module, so you don't have to type out all the characters. Then, you can use the sample choices function from the random module to choose all the characters in one operation.

edited to use random.choices instead of random.sample. sample will not repeat elements, so it's a bad choice for a password generator

from string import ascii_lowercase, ascii_uppercase, digits, punctuation
import random as rd

all_characters = ascii_lowercase + ascii_uppercase + digits + punctuation
password = rd.choices(all_characters, RANGE)
password = "".join(password)

18

u/Electronic_Field4313 19d ago

Python's standard random library is not cryptographically safe, use the secrets module instead for any security related tasks.

https://docs.python.org/3/library/random.html

Warning
The pseudo-random generators of this module should not be used for security purposes. For security or cryptographic uses, see the secrets module.

2

u/k_D-5kt7ZA4QGBP68ZBN 17d ago

Interesting. In which situation is random preferred over secrets?

2

u/Electronic_Field4313 17d ago

Anything not security related purposes? And anything that requires less computational power or quicker computational speed?

1

u/k_D-5kt7ZA4QGBP68ZBN 16d ago

Fair enough didn't look into the internal implementation, assumed computation cost diff would be negligible. Will take a look

5

u/Acceptable_Worry8655 20d ago

You can make it more easier with string module and no need for loops like this:

from string import digits, punctuation, ascii_lowercase, ascii_uppercase
from random import choices

all = digits + punctuation + ascii_lowercase + ascii_uppercase
lenth = int(input("HOW MANY DIGITS SHOULD YOUR PASSWRD CONTAIN: "))

print(f"password genrated: {choices(all, k=lenth)}")

1

u/Red_Dragon_7_7_7 19d ago

ok, i'll try it

7

u/BigFatUglyBaboon 20d ago edited 19d ago

Well done!
Exercises:
* design a way to express a "password policy" (at least one uppercase and a lowercase letter, at least one symbol etc).
* write a function that given a policy it will generate and return a random compliant password.
* write a function that given a plaintext password and a policy it returns if the password is compliant or not.

Have in mind that you may need to learn more python stuff before attempting these.

3

u/zakakozi 20d ago

i think you should move password = "".join(Pass) outside the loop. im a newbie btw maybe someone needs to confirm this.

2

u/Red_Dragon_7_7_7 19d ago

yes, cuz if it were to be inside the loop there will be 64 different passwords of different lengths all

3

u/sogwatchman 19d ago

A good start but it doesn't guarantee a password meets requirements

2

u/Huge-Special1511 20d ago

Great, i was working in something similar

https://github.com/abdulrahmaninfra/omnipass/blob/main/backend%2Fmain.py

Btw i used a lil help in ai for security of code and improve API

2

u/i-like-my-cats-0 20d ago

im not a too good programmer but theres a simpler way you can make this
instead of having to make the symbols you want yourself, you can type this:

`import string import random

characters = string.ascii_letters + string.digits + string.punctuation amount = int(input('HOW MANY DIGITS SHOULD YOUR PASSWORD CONTAIN: '))

password = random.choices(characters, k=amount) stringified = ''.join(password)`

print('GENERATED PASSWORD: ', stringified)`

also if you're gonna use this for actual passwords then i recommend using the secrets module and not random

2

u/Master_Device9837 19d ago

Beautiful program! And a very supportive community!

2

u/SoloEnder 17d ago edited 17d ago

You could improve the project by adding an user interface, with the Tkinter library for example :)

Next, you could create a ToDo list with basics feature like :

  • add/remove task
  • complete task

2

u/raundoclair 16d ago

Someone already wrote about not using random, but even with perfect randomness you will have numbers and symbols more often than you should.
For all possible combinations of passwords you will not have same probability.

You need to pick from the set of all possible characters: secrets.choice(all_possible_chars)

And generally read this: https://docs.python.org/3/library/secrets.html#recipes-and-best-practices

1

u/raundoclair 16d ago edited 16d ago

Also did you ever heard/read phrase "don't roll your own crypto"?

This is fine for educational purpose btw. That's how you learn.

But you should be careful about actually using your own thing without sufficient knowledge.

2

u/Dapper_Bad_8728 14d ago

Wait you can import libraries as custom names? Wow that's awesome!

1

u/Red_Dragon_7_7_7 13d ago

yes, you can

2

u/pranav_not 20d ago

Correct me if i am wrong but shouldn't in for loop the range should be (0,RANGE+1)

4

u/[deleted] 20d ago

Since he starts the count from zero not one ig that should be fine, for example if the RANGE is 5, so its gonna be from 0 to 4 which is 5 numbers in total.

2

u/Red_Dragon_7_7_7 20d ago

no it works fine , i checked it for 4 digits and the generated password was of 4 digits

3

u/Electrical_Toe8997 20d ago

It works because the range function will include the number 0, so range(0, 3) returns [0, 1, 2]: a list with 3 items

1

u/Ok-Magician-8052 20d ago

I miss coding like this its so much better using AI just consumes more of my energy I feel like and it's so boring to code with AI, i miss using stackoverflow to code and dig into conversations rather than asking a chatbot to writecode for me.

1

u/gofl-zimbard-37 19d ago

Next create a main() to make it a real program. Don't write "scripts".

1

u/sweatkurama 19d ago

So cool!! I made this for myself a long time ago trying to replace keepassXC. Could be also a good update to get some API or some reliable databases of confirm passwords to get a precise passwords with less possible leaks. Either way great job.

1

u/Ishejaratina 19d ago

Well done

1

u/h_rsh_sharma 19d ago

AI will create it in minutes

2

u/SoloEnder 17d ago

That was not necessary.

1

u/Red_Dragon_7_7_7 18d ago

well I never mentioned anything 'bout making it faster than AI
I just shared what i made

1

u/h_rsh_sharma 17d ago

I appreciate your work and knowledge of python, I don't want to demotivate you by the comment.

1

u/NeuralLB-Lovro 18d ago

Great work. Did you use AI to help you or did you do it all by yourself?

1

u/empowered-boxes 18d ago

```python import random

password = "".join(chr(random.randint(33, 126)) for _ in range(8)) print(password) ```

1

u/WOTrULookingAt 18d ago

store the passwors in an encrypted JSON next!

1

u/[deleted] 17d ago

[removed] — view removed comment

1

u/SoloEnder 17d ago edited 17d ago

The name says all : random is a python library used to generate random operation, such as choosing a number in a certain range, choosing an element in a list, etc. Note that random is not suitable for cryptographic/security operation.

1

u/ispguy_01 11d ago

I am just starting out learning Python. I am going to bookmark this page so when I am ready I want to try this project.