Advertisement
Guest User

passwordgen

a guest
Sep 4th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import random # https://docs.python.org/3.6/library/random.html
  2. import sqlite3 # https://docs.python.org/3.6/library/sqlite3.html
  3. import string # https://docs.python.org/3.6/library/string.html
  4.  
  5.  
  6. def get_rand(
  7. str_list:str , choice:int):
  8. ''' This function chooses a random letter from
  9. a string and returns it in upper or lower
  10. case depending upon the choice'''
  11. pass
  12. if choice == 0:
  13. return random.choice(str_list)
  14. else:
  15. return random.choice(str_list).upper()
  16.  
  17.  
  18. def get_char(rule: int, posi: int):
  19. ''' This function returns a character depending
  20. on the rule and the position in the list
  21. The below are the characters considered
  22. in the password generation process
  23. letters = 'qwertyuiopasdfghjklzxcvbnm'
  24. numbers = '1234561789'
  25. punc = ':,β€”!-().?"; '''
  26.  
  27. letters = 'qwertyuiopasdfghjklzxcvbnm'
  28. numbers = '1234561789'
  29. punc = """ ':,β€”!-().?";"""
  30.  
  31. if rule >=4:
  32. pass
  33. temp = punc if posi <=4 else numbers+letters+punc
  34. return get_rand(temp,random.randrange(0,2))
  35. elif rule >=3:
  36. pass
  37. temp = letters.upper() if posi <=3 else numbers+letters
  38. return get_rand(temp,random.randrange(0,2))
  39. elif rule >=2:
  40. pass
  41. temp = numbers if posi <=2 else numbers+letters
  42. return get_rand(temp,0)
  43. else:
  44. pass
  45. return get_rand(letters,0)
  46.  
  47.  
  48. def generate_password(length: int, complexity: int) -> str:
  49. """Generate a random password with given length and complexity
  50.  
  51. Complexity levels:
  52. Complexity == 1: return a password with only lowercase chars
  53. Complexity == 2: Previous level plus at least 1 digit
  54. Complexity == 3: Previous levels plus at least 1 uppercase char
  55. Complexity == 4: Previous levels plus at least 1 punctuation char
  56.  
  57. :param length: number of characters
  58. :param complexity: complexity level
  59. :returns: generated password
  60. """
  61. pass
  62. pwd_list = []
  63. """ check if length is greater than complexity if not
  64. return the password with the minimum required comlexity
  65. length
  66. """
  67. if length < complexity:
  68. length = complexity
  69. """ generate the password"""
  70. temp_rule = 1
  71. for i in range(0,length):
  72. temp_val = temp_rule if temp_rule <= complexity else random.randrange(1,complexity+1)
  73. pwd_list.append(get_char(temp_val,i))
  74. temp_rule = temp_rule +1
  75. random.shuffle(pwd_list)
  76. return "".join(pwd_list)
  77.  
  78.  
  79. print(generate_password(10,4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement