acclivity

pyGeneratePassword-v2

Jan 25th, 2021 (edited)
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. # Password Generator
  2. # Mike Kerry - 25-Jan-2021 - acclivity2@gmail.com
  3. # Modified April 2021
  4.  
  5. import string       # This module provides a convenient way of accessing the alphabet etc.
  6. import random       # This module is required for the "choices()" method we will use
  7.  
  8.  
  9. def make_password(length):
  10.     # Force "length" to be between 8 and 12 characters
  11.     # This is a neat Pythonic approach, rather than a naive if/else test
  12.     length = max(length, 8)
  13.     length = min(length, 12)
  14.  
  15.     # Now we will create our password, firstly as a list
  16.     # Start with 2 uppercase letters chosen at random using choices()
  17.     password = random.choices(string.ascii_uppercase, k=2)
  18.  
  19.     # Then add 2 random lowercase letters
  20.     password.extend(random.choices(string.ascii_lowercase, k=2))
  21.  
  22.     # Must include 2 digits
  23.     password.extend(random.choices(string.digits, k=2))
  24.  
  25.     # Must include 2 "punctuation" characters
  26.     password.extend(random.choices(string.punctuation, k=2))
  27.  
  28.     # The ppassword now is of length 8. If we want it longer we can extend it
  29.     # with any combination of letters digits and punctuation
  30.  
  31.     if length > 8:
  32.         mixture = string.ascii_letters + string.digits + string.punctuation
  33.         password.extend(random.choices(mixture, k=length-8))
  34.  
  35.     # Shuffle the list
  36.     random.shuffle(password)
  37.     # Convert the list to a string and return it
  38.     return "".join(password)
  39.  
  40.  
  41. # Generate some sample passwords of length 3 to 14, (these lengths will get constrained to 8 through 12)
  42. for x in range(12):
  43.     print(make_password(3 + x))
  44.  
  45.  
  46. # Results
  47. # X0F3rb|#
  48. # iLF2@?5f
  49. # ,0Q,rU5q
  50. # iD8G(m=9
  51. # 4p/9L}Yx
  52. # L+aw13P|
  53. # <k31BNn,#
  54. # 69a!4%OAfc
  55. # fH[61uXX!d'
  56. # A%TcN6a3Z;[Q
  57. # b!oACdeS[+88
  58. # H4xzA,V0\uE>
  59.  
Add Comment
Please, Sign In to add comment