Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #my_random_module.py - tools for playing with randomness
- import random
- def f2():
- print("I am f2")
- #def f2
- #random.randint(1, 44) #38, 1, 44
- #random.random() #[0, 1[
- #pseudo-random
- """
- we want to control:
- - the length of the password (the #symbols)
- -- random length
- -- ask the user
- - support capital letters A-Z
- - support small letters a-z
- - support decimal digits 0-9
- - support "special symbols", e.g. "*", ".",...
- """
- #4 sequences / collections : list, tuple, dict, set
- DEFAULT_SPECIAL_SYMBOLS = ["*", "-", "/", "~", "@", "ç", "?"]
- #type(DEFAULT_SPECIAL_SYMBOLS) #list
- DEFAULT_PASS_LENGTH = 8
- #type(DEFAULT_PASS_LENGTH) #int
- DEFAULT_B_CAPITAL_L = True
- DEFAULT_B_SMALL_L = True
- DEFAULT_B_DIGITS = True
- DEFAULT_B_SPECIALS = True
- TOOL_AZ = 1
- TOOL_az = 2
- TOOL_D = 3
- TOOL_SS = 4
- def randomPassword(
- piPassyLength = DEFAULT_PASS_LENGTH, #how many symbols?
- pbCapitalLetters = DEFAULT_B_CAPITAL_L, #True to accept AZ
- pbSmallLetters = DEFAULT_B_SMALL_L, #True to accept az
- pbDigits = DEFAULT_B_DIGITS, #True to accept 0-9
- pbSpecials = DEFAULT_B_SPECIALS, #True to accept ????
- pListSS = DEFAULT_SPECIAL_SYMBOLS
- ):
- strPassword = "" # empty string
- bLegitCall = pbCapitalLetters or \
- pbSmallLetters or \
- pbDigits or \
- (pbSpecials and len(pListSS)>0)
- if (bLegitCall):
- bPasswordReady = False
- while (not bPasswordReady):
- iRandomTool = random.randint(1, 4) #pick a random tool
- strRandomSymbol = ""
- if (iRandomTool==TOOL_AZ and pbCapitalLetters):
- strRandomSymbol = randomAZ()
- elif (iRandomTool==TOOL_az and pbSmallLetters):
- strRandomSymbol = random_az()
- elif (iRandomTool==TOOL_D and pbDigits):
- strRandomSymbol = randomDigit()
- elif (iRandomTool==TOOL_SS and pbSpecials):
- strRandomSymbol = randomSpecialSymbol(pListSS)
- #blindly
- strPassword+=strRandomSymbol #MUST CHANGE
- bPasswordReady = len(strPassword)==piPassyLength
- #while
- return strPassword
- #def randomPassword
- def randomSymbolBetween(pStrLeft, pStrRight):
- iCodeLeft = ord(pStrLeft) #code for the letter pStrLeft
- iCodeRight = ord(pStrRight) #code for the letter pStrRight
- iRandomCode = random.randint(iCodeLeft, iCodeRight)
- strRandomSymbol = chr(iRandomCode) #symbol for the code
- return strRandomSymbol #random symbol
- #def randomSymbolBetween
- def randomAZ():
- return randomSymbolBetween('A', 'Z')
- def random_az():
- return randomSymbolBetween('a', 'z')
- def randomDigit():
- return randomSymbolBetween('0', '9')
- def randomSpecialSymbol(
- pListSpecialSymbols = DEFAULT_SPECIAL_SYMBOLS
- ):
- iHowManyElements = len(pListSpecialSymbols) #e.g. 7
- #["coisa1"@0, "coisa2"@1, ..., "coisa7"@6]
- iRandomListAddress = random.randint(0, iHowManyElements-1)
- strRandomSpecialSymbol = pListSpecialSymbols[iRandomListAddress]
- return strRandomSpecialSymbol
- #def randomSpecialSymbol
- #alias (singular) aliases (plural)
- def digitoAoCalhas():
- return randomDigit()
- #def digitoAoCalhas
- *******************************************
- import random
- from my_random_module import *
- FILE_FOR_PASS = "PASSWORDS.TXT"
- def genFileWithNPasswords(pStrN:str)->str:
- iHowManyTimes = int(pStrN)
- bEnoughTimes = False
- iCounter = 0
- #file writer
- fw = open(
- FILE_FOR_PASS,
- #'wt' #write text
- 'at' #append text
- )
- while (not bEnoughTimes):
- strPass = randomPassword(random.randint(3,33))
- iCounter+=1
- #write to file via fw
- fw.write(strPass+"\n")
- bEnoughTimes = iCounter==iHowManyTimes
- #while
- fw.close()
- return FILE_FOR_PASS
- #def
- def totallyRandomPass()->str:
- return randomPassword(random.randint(8, 32))
- def passwordSized(pStrUserSize:str)->str:
- return randomPassword(int(pStrUserSize))
- strMenu = \
- """
- 1 - Gerar uma só password, completamente aleatória
- 2 - Gerar uma só password, de tamanho à escolha
- 3 - Gerar um ficheiros com quantidade n de passwords
- 0 - Terminar o programa
- """
- print (strMenu)
- bEndOfProgram = False
- while (not bEndOfProgram):
- strUserChoice = input("A sua escolha? ")
- print ("Escolheu a opção {}".format(strUserChoice))
- if(strUserChoice=="1"):
- print ("Eis a tua password totalmente aleatória: {}"\
- .format(totallyRandomPass()))
- elif (strUserChoice=="2"):
- strUserSize = input("Tamanho da password? ")
- print ("Eis a tua password de tamanho {}: {}"\
- .format(
- strUserSize,
- #len(passwordSized(strUserSize)), #ugly!
- passwordSized(strUserSize)
- ))
- elif (strUserChoice=="3"):
- strHowManyPass = input("How many passwords? ")
- print("{} password(s) were added to file {}"\
- .format(
- strHowManyPass,
- genFileWithNPasswords(strHowManyPass)
- ))
- elif (strUserChoice=="0"):
- print ("Bye bye")
- exit(0) #end of program, code 0
- else:
- print ("Escolha inválida")
- print(strMenu)
- #while
Add Comment
Please, Sign In to add comment