Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python v3 ( Random password generator )

My question is: how to generate a random password with a length that varies from (8 to 12 characters) each time generated.

This is my code:

import string
import random

def randompassword():
    chars=string.ascii_uppercase + string.ascii_lowercase + string.digits
    size=8 
    return ''.join(random.choice(chars) for x in range(size,12))

print(randompassword())
like image 426
Alfie brown Avatar asked Oct 06 '13 19:10

Alfie brown


People also ask

How do you create a code generator in Python?

You can create a generator model with the YAKINDU Statechart generator model wizard by selecting File → New → Code generator model. The code generation is performed automatically whenever the statechart or the generator file is modified. See also chapter Running a generator for more information.


2 Answers

size = 8; range(size, 12) always returns array [8,9,10,11], so you always get a password of length 4. Instead, determine the size of this particular password using randint ahead of time:

import string
import random

def randompassword():
  chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
  size = random.randint(8, 12)
  return ''.join(random.choice(chars) for x in range(size))
like image 74
Denis Avatar answered Oct 23 '22 07:10

Denis


There are nuances to this question and certain solutions may cause password weakness problems. Consider the following nuances along with the solution alternative.

  • Some answers employ string.printable, but this isn't a good idea since that contains whitespace characters. While they are not strictly illegal for passwords, you cannot easily see them and therefore cannot differentiate, say, a tab from several spaces (and so on). Below I only employ lowercase letters & uppercase letters, digits and punctuation characters.
  • Randomly choosing from a set of characters based on element-position isn't random since the cardinality of each included character-class isn't uniformly distributed: 26 Lowercase letters; 26 Uppercase letters; 10 Digits; 32 Punctuation characters. Therefore generated passwords are likely to have more letters than punctuation characters and digits; and more punctuation characters than digits; (and so on). So if random.choices() is used (as in other answers), one should also employ it's weights= and cum_weights= options, to eliminate aforementioned biases and even-out the distribution.
  • That said, I encourage use of Python's secrets module rather that its random module for this use case. From their documentation on random:

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

Here is one functionally-oriented solution using Python-3. It uses secrets.choice() only. It doesn't totally solve the random problem (other nuances remain), but it does improve selection-distribution to reduces bias:

>>> import string, secrets

>>> char_classes = (string.ascii_lowercase,
                    string.ascii_uppercase,
                    string.digits,
                    string.punctuation)

>>> size = lambda: secrets.choice(range(8,13))                  # Chooses a password length.
>>> char = lambda: secrets.choice(secrets.choice(char_classes)) # Chooses one character, uniformly selected from each of the included character classes.
>>> pw   = lambda: ''.join([char() for _ in range(size())])     # Generates the variable-length password.

DEMO: Generate 10 variable-length password-strings using characters uniformly selected from each of our character classes:

>>> for i in range(1,11):
>>>    p = pw()
>>>    print('%i) %i chars :: %s' % (i,len(p),p))
 1) 11 chars :: IwWNEAUmnJt
 2) 10 chars :: ;N/'tO6RTv
 3)  8 chars :: l=5.2CDh
 4) 10 chars :: V0=I+A`t2Q
 5) 12 chars :: PQm8:f,#56"9
 6) 10 chars :: KOdx9~%r;F
 7) 11 chars :: <?67U8}3>F{
 8) 11 chars :: G$5y~3fE7o*
 9) 10 chars :: 70,|=Rexwn
10)  8 chars :: &31P^@cU

Finally, while we used the secrets module here, something similar could be done using numpy and numpy.random. I hope this helps!

like image 37
NYCeyes Avatar answered Oct 23 '22 08:10

NYCeyes