Advertisement
am_dot_com

FP 20211102

Nov 2nd, 2021 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. #passgen.py - a random password generator
  2. import random
  3.  
  4. #random.randint(1, 44) #38, 1, 44
  5. #random.random() #[0, 1000[ 999-0+1
  6. #pseudo-random
  7.  
  8. """
  9. we want to control:
  10. - the length of the password (the #symbols)
  11. -- random length
  12. -- ask the user
  13. - support capital letters A-Z
  14. - support small letters a-z
  15. - support decimal degital 0-9
  16. - support "special symbols", e.g. "*", ".",...
  17. """
  18. #4 sequences / collections : list, tuple, dict, set
  19. DEFAULT_SPECIAL_SYMBOLS = ["*", "-", "/", "~", "@", "รง", "?"]
  20. DEFAULT_PASS_LENGTH = 8
  21. DEFAULT_B_CAPITAL_L = True
  22. DEFAULT_B_SMALL_L = True
  23. DEFAULT_B_DIGITS = True
  24. DEFAULT_B_SPECIALS = True
  25.  
  26. TOOL_AZ = 1
  27. TOOL_az = 2
  28. TOOL_D = 3
  29. TOOL_SS = 4
  30. def randomPassword(
  31.     piPassyLength = DEFAULT_PASS_LENGTH, #how many symbols?
  32.     pbCapitalLetters = DEFAULT_B_CAPITAL_L, #True to accept AZ
  33.     pbSmallLetters = DEFAULT_B_SMALL_L, #True to accept az
  34.     pbDigits = DEFAULT_B_DIGITS, #True to accept 0-9
  35.     pbSpecials = DEFAULT_B_SPECIALS, #True to accept ????
  36.     pListSS = DEFAULT_SPECIAL_SYMBOLS
  37. ):
  38.     bPasswordReady = False
  39.     strPassword = ""
  40.     while (not bPasswordReady):
  41.         iRandomTool = random.randint(1, 4) #pick a random tool
  42.         iRandomSymbol = None
  43.         if (iRandomTool==TOOL_AZ):
  44.             iRandomSymbol = randomAZ()
  45.         elif (iRandomTool==TOOL_az):
  46.             iRandomSymbol = random_az()
  47.         elif (iRandomTool==TOOL_D):
  48.             iRandomSymbol = randomDigit()
  49.         elif (iRandomTool==TOOL_SS):
  50.             iRandomSymbol = randomSpecialSymbol(pListSS)
  51.  
  52.         strPassword+=iRandomSymbol
  53.         bPasswordReady = len(strPassword)>=piPassyLength
  54.     #while
  55.     return strPassword
  56. #def randomPassword
  57.  
  58. def randomSymbolBetween(pStrLeft, pStrRight):
  59.     iCodeLeft = ord(pStrLeft)
  60.     iCodeRight = ord(pStrRight)
  61.     iRandomCode = random.randint(iCodeLeft, iCodeRight)
  62.     strRandomSymbol = chr(iRandomCode)
  63.     return strRandomSymbol
  64. #def randomSymbolBetween
  65.  
  66. def randomAZ():
  67.     return randomSymbolBetween('A', 'Z')
  68.  
  69. def random_az():
  70.     return randomSymbolBetween('a', 'z')
  71.  
  72. def randomDigit():
  73.     return randomSymbolBetween('0', '9')
  74.  
  75. def randomSpecialSymbol(
  76.     pListSpecialSymbols = DEFAULT_SPECIAL_SYMBOLS
  77. ):
  78.     iHowManyElements = len(pListSpecialSymbols) #e.g. 7
  79.     #["coisa1"@0, "coisa2"@1, ...,  "coisa7"@6]
  80.     iRandomListAddress = random.randint(0, iHowManyElements-1)
  81.     strRandomSymbol = pListSpecialSymbols[iRandomListAddress]
  82.     return strRandomSymbol
  83. #def randomSpecialSymbol
  84.  
  85. #alias (singular) aliases (plural)
  86. def digitoAoCalhas():
  87.     return randomDigit()
  88. #def digitoAoCalhas
  89.  
  90. for x in range(100):
  91.     #print(randomSpecialSymbol([":", "%"]))
  92.     #print(randomSpecialSymbol())
  93.     print(randomPassword())
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement