am_dot_com

FP 20211108

Nov 8th, 2021 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.28 KB | None | 0 0
  1. #my_random_module.py - tools for playing with randomness
  2. import random
  3.  
  4. def f2():
  5.     print("I am f2")
  6. #def f2
  7.  
  8. #random.randint(1, 44) #38, 1, 44
  9. #random.random() #[0, 1[
  10. #pseudo-random
  11.  
  12. """
  13. we want to control:
  14. - the length of the password (the #symbols)
  15. -- random length
  16. -- ask the user
  17. - support capital letters A-Z
  18. - support small letters a-z
  19. - support decimal digits 0-9
  20. - support "special symbols", e.g. "*", ".",...
  21. """
  22. #4 sequences / collections : list, tuple, dict, set
  23. DEFAULT_SPECIAL_SYMBOLS = ["*", "-", "/", "~", "@", "ç", "?"]
  24. #type(DEFAULT_SPECIAL_SYMBOLS) #list
  25. DEFAULT_PASS_LENGTH = 8
  26. #type(DEFAULT_PASS_LENGTH) #int
  27. DEFAULT_B_CAPITAL_L = True
  28. DEFAULT_B_SMALL_L = True
  29. DEFAULT_B_DIGITS = True
  30. DEFAULT_B_SPECIALS = True
  31.  
  32. TOOL_AZ = 1
  33. TOOL_az = 2
  34. TOOL_D = 3
  35. TOOL_SS = 4
  36.  
  37. def randomPassword(
  38.     piPassyLength = DEFAULT_PASS_LENGTH, #how many symbols?
  39.     pbCapitalLetters = DEFAULT_B_CAPITAL_L, #True to accept AZ
  40.     pbSmallLetters = DEFAULT_B_SMALL_L, #True to accept az
  41.     pbDigits = DEFAULT_B_DIGITS, #True to accept 0-9
  42.     pbSpecials = DEFAULT_B_SPECIALS, #True to accept ????
  43.     pListSS = DEFAULT_SPECIAL_SYMBOLS
  44. ):
  45.     strPassword = ""  # empty string
  46.  
  47.     bLegitCall = pbCapitalLetters or \
  48.                 pbSmallLetters or \
  49.                 pbDigits or \
  50.                 (pbSpecials and len(pListSS)>0)
  51.  
  52.     if (bLegitCall):
  53.         bPasswordReady = False
  54.         while (not bPasswordReady):
  55.             iRandomTool = random.randint(1, 4) #pick a random tool
  56.             strRandomSymbol = ""
  57.  
  58.             if (iRandomTool==TOOL_AZ and pbCapitalLetters):
  59.                 strRandomSymbol = randomAZ()
  60.             elif (iRandomTool==TOOL_az and pbSmallLetters):
  61.                 strRandomSymbol = random_az()
  62.             elif (iRandomTool==TOOL_D and pbDigits):
  63.                 strRandomSymbol = randomDigit()
  64.             elif (iRandomTool==TOOL_SS and pbSpecials):
  65.                 strRandomSymbol = randomSpecialSymbol(pListSS)
  66.  
  67.             #blindly
  68.             strPassword+=strRandomSymbol #MUST CHANGE
  69.  
  70.             bPasswordReady = len(strPassword)==piPassyLength
  71.         #while
  72.     return strPassword
  73. #def randomPassword
  74.  
  75. def randomSymbolBetween(pStrLeft, pStrRight):
  76.     iCodeLeft = ord(pStrLeft) #code for the letter pStrLeft
  77.     iCodeRight = ord(pStrRight) #code for the letter pStrRight
  78.     iRandomCode = random.randint(iCodeLeft, iCodeRight)
  79.     strRandomSymbol = chr(iRandomCode) #symbol for the code
  80.     return strRandomSymbol #random symbol
  81. #def randomSymbolBetween
  82.  
  83. def randomAZ():
  84.     return randomSymbolBetween('A', 'Z')
  85.  
  86. def random_az():
  87.     return randomSymbolBetween('a', 'z')
  88.  
  89. def randomDigit():
  90.     return randomSymbolBetween('0', '9')
  91.  
  92. def randomSpecialSymbol(
  93.     pListSpecialSymbols = DEFAULT_SPECIAL_SYMBOLS
  94. ):
  95.     iHowManyElements = len(pListSpecialSymbols) #e.g. 7
  96.     #["coisa1"@0, "coisa2"@1, ...,  "coisa7"@6]
  97.     iRandomListAddress = random.randint(0, iHowManyElements-1)
  98.     strRandomSpecialSymbol = pListSpecialSymbols[iRandomListAddress]
  99.     return strRandomSpecialSymbol
  100. #def randomSpecialSymbol
  101.  
  102. #alias (singular) aliases (plural)
  103. def digitoAoCalhas():
  104.     return randomDigit()
  105. #def digitoAoCalhas
  106.  
  107.  
  108. *******************************************
  109. import random
  110.  
  111. from my_random_module import *
  112.  
  113. FILE_FOR_PASS = "PASSWORDS.TXT"
  114. def genFileWithNPasswords(pStrN:str)->str:
  115.     iHowManyTimes = int(pStrN)
  116.     bEnoughTimes = False
  117.     iCounter = 0
  118.     #file writer
  119.     fw = open(
  120.         FILE_FOR_PASS,
  121.         #'wt' #write text
  122.         'at' #append text
  123.     )
  124.     while (not bEnoughTimes):
  125.         strPass = randomPassword(random.randint(3,33))
  126.         iCounter+=1
  127.         #write to file via fw
  128.         fw.write(strPass+"\n")
  129.         bEnoughTimes = iCounter==iHowManyTimes
  130.     #while
  131.     fw.close()
  132.     return FILE_FOR_PASS
  133. #def
  134.  
  135. def totallyRandomPass()->str:
  136.     return randomPassword(random.randint(8, 32))
  137.  
  138. def passwordSized(pStrUserSize:str)->str:
  139.     return randomPassword(int(pStrUserSize))
  140.  
  141. strMenu = \
  142. """
  143. 1 - Gerar uma só password, completamente aleatória
  144. 2 - Gerar uma só password, de tamanho à escolha
  145. 3 - Gerar um ficheiros com quantidade n de passwords
  146. 0 - Terminar o programa
  147. """
  148.  
  149. print (strMenu)
  150.  
  151. bEndOfProgram = False
  152. while (not bEndOfProgram):
  153.     strUserChoice = input("A sua escolha? ")
  154.     print ("Escolheu a opção {}".format(strUserChoice))
  155.     if(strUserChoice=="1"):
  156.         print ("Eis a tua password totalmente aleatória: {}"\
  157.                 .format(totallyRandomPass()))
  158.     elif (strUserChoice=="2"):
  159.         strUserSize = input("Tamanho da password? ")
  160.         print ("Eis a tua password de tamanho {}: {}"\
  161.                 .format(
  162.                     strUserSize,
  163.                     #len(passwordSized(strUserSize)), #ugly!
  164.                     passwordSized(strUserSize)
  165.                 ))
  166.     elif (strUserChoice=="3"):
  167.         strHowManyPass = input("How many passwords? ")
  168.         print("{} password(s) were added to file {}"\
  169.               .format(
  170.                     strHowManyPass,
  171.                     genFileWithNPasswords(strHowManyPass)
  172.                 ))
  173.     elif (strUserChoice=="0"):
  174.         print ("Bye bye")
  175.         exit(0) #end of program, code 0
  176.     else:
  177.         print ("Escolha inválida")
  178.  
  179.     print(strMenu)
  180. #while
  181.  
Add Comment
Please, Sign In to add comment