Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #passgen.py - a random password generator
- import random
- #random.randint(1, 44) #38, 1, 44
- #random.random() #[0, 1000[ 999-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 degital 0-9
- - support "special symbols", e.g. "*", ".",...
- """
- #4 sequences / collections : list, tuple, dict, set
- DEFAULT_SPECIAL_SYMBOLS = ["*", "-", "/", "~", "@", "รง", "?"]
- DEFAULT_PASS_LENGTH = 8
- 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
- ):
- bPasswordReady = False
- strPassword = ""
- while (not bPasswordReady):
- iRandomTool = random.randint(1, 4) #pick a random tool
- iRandomSymbol = None
- if (iRandomTool==TOOL_AZ):
- iRandomSymbol = randomAZ()
- elif (iRandomTool==TOOL_az):
- iRandomSymbol = random_az()
- elif (iRandomTool==TOOL_D):
- iRandomSymbol = randomDigit()
- elif (iRandomTool==TOOL_SS):
- iRandomSymbol = randomSpecialSymbol(pListSS)
- strPassword+=iRandomSymbol
- bPasswordReady = len(strPassword)>=piPassyLength
- #while
- return strPassword
- #def randomPassword
- def randomSymbolBetween(pStrLeft, pStrRight):
- iCodeLeft = ord(pStrLeft)
- iCodeRight = ord(pStrRight)
- iRandomCode = random.randint(iCodeLeft, iCodeRight)
- strRandomSymbol = chr(iRandomCode)
- return strRandomSymbol
- #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)
- strRandomSymbol = pListSpecialSymbols[iRandomListAddress]
- return strRandomSymbol
- #def randomSpecialSymbol
- #alias (singular) aliases (plural)
- def digitoAoCalhas():
- return randomDigit()
- #def digitoAoCalhas
- for x in range(100):
- #print(randomSpecialSymbol([":", "%"]))
- #print(randomSpecialSymbol())
- print(randomPassword())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement