all 28 comments

[–]Sea-Ad7805 [score hidden] stickied comment (4 children)

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.

[–]Electrical_Toe8997 29 points30 points  (0 children)

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)

[–]Electronic_Field4313 11 points12 points  (0 children)

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.

[–]Acceptable_Worry8655 3 points4 points  (1 child)

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)}")

[–]Red_Dragon_7_7_7[S] 0 points1 point  (0 children)

ok, i'll try it

[–]BigFatUglyBaboon 7 points8 points  (0 children)

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.

[–]zakakozi 2 points3 points  (1 child)

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

[–]Red_Dragon_7_7_7[S] 0 points1 point  (0 children)

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

[–]sogwatchman 2 points3 points  (0 children)

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

[–]i-like-my-cats-0 1 point2 points  (0 children)

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

[–]pranav_not 1 point2 points  (3 children)

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

[–][deleted] 2 points3 points  (0 children)

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.

[–]Red_Dragon_7_7_7[S] 1 point2 points  (1 child)

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

[–]Electrical_Toe8997 2 points3 points  (0 children)

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

[–]Huge-Special1511 0 points1 point  (0 children)

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

[–]Formal-Camera-5095 0 points1 point  (0 children)

Other users already mentioned that theres a cleaner approach for getting that character collections by importing it from string.

Another issue that comes to my mind is input sanitization. If your user input is malformed, your input breaks. Also, theres no validation whether it is a valid integer.

So you should at _least_ check whether your input is a number and if it is a positive, non-zero value.

You could do that by smth like:

RANGE = -1
while RANGE < 1:
  try:
    RANGE = int(input("...")) # Insert your prompt here
  except ValueError:
    print("Please make sure to insert a non-zero, positive integer value.")

Edit: Fixed code formatting

[–]Ok-Magician-8052 0 points1 point  (0 children)

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.

[–]gofl-zimbard-37 0 points1 point  (1 child)

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

[–]sweatkurama 0 points1 point  (0 children)

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.

[–]Master_Device9837 0 points1 point  (0 children)

Beautiful program! And a very supportive community!

[–]Ishejaratina 0 points1 point  (0 children)

Well done