G+_Derek Souter Posted December 13, 2015 Share Posted December 13, 2015 so far up to episode 15, and thought I would show a quick and dirty little python script I did this morning - just to create a random character password # create a random password #import the required modules import random import binascii import os #set the variable and list Characters = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"] # valid characters in a HEX code password = "" # set the password to blank desiredpasswordlength = 16 # set the password length to the default loopcounter = 0 # this is how many times it has processed the loop to create the password firstmessage = "How many characters do you want in the generated password? (pressing enter will use the default password length of", str(desiredpasswordlength),"characters)" # i had to use this, due to python limitations on the raw_input method #clear the screen os.system('cls') # choose how many characters you want the password to be - uses the firstmessage variable due to limits on the raw_input method userpasswordlength = raw_input(firstmessage) if userpasswordlength.isdigit(): # check that the user has entered a number if int(userpasswordlength) < 10: # verify that the number is not too small - i don't like small passwords #clear the screen os.system('cls') print "Sorry, but this would generate a weak password of only", str(userpasswordlength),"characters, my programmer does not allow that, I will now use the default length of", str(desiredpasswordlength) else: if int(userpasswordlength) > 60: # make sure they are not being stupid - i don't want to try and type in this length of password! #clear the screen os.system('cls') print "Sorry, you must be mad to try and create a password of", str(userpasswordlength),"characters, my programmer does not allow that, I will now use the default length of", str(desiredpasswordlength) else: desiredpasswordlength = userpasswordlength # set the desiredpasswordlength variable to the userpasswordlength, assuming it is over 10 and less than 60 characters else: # clear screen os.system('cls') # tell the user that they didn't enter a number print "Sorry, but ", str(userpasswordlength), "is not a number, I will now use the default length of", str(desiredpasswordlength) print print "creating a password of", str(desiredpasswordlength),"characters." # tell the user that i will now create a password of the required number of characters print # blank line #my processing loop - run once for each character while loopcounter < int(desiredpasswordlength): random.seed() # initialise the random number randomcharacter1 = random.randint(0,(len(Characters)-1)) # choose a first random Octet randomcharacter2 = random.randint(0,(len(Characters)-1)) # choose a second random Octet AsciiCharacter = Characters[randomcharacter1] + Characters[randomcharacter2] # concatenate both Octets together to make a HEX code if AsciiCharacter > "20" and AsciiCharacter < "7F": # ensure that the character is one you can actually type in! password = password + binascii.unhexlify(AsciiCharacter) # add the new character to the current password # print the character code and the ASCII character to the screen (uncomment the next line to show the process on the screen) #print AsciiCharacter, "equates to", binascii.unhexlify(AsciiCharacter)," current password is:",password loopcounter = loopcounter + 1 print print "the generated password of", str(desiredpasswordlength),"characters is:", password # output the current password to the screen print print raw_input("Press Enter to quit") # stop the terminal from closing - need to add a menu and allow you to create multiple passwords..... Link to comment Share on other sites More sharing options...
G+_L I Posted March 16, 2016 Share Posted March 16, 2016 Nice. One note - os.system('cls') only works on windows. To work on UNIX you'd have to use os.system('clear') Link to comment Share on other sites More sharing options...
G+_L I Posted March 16, 2016 Share Posted March 16, 2016 There's also this method for both: os.system('cls' if os.name == 'nt' else 'clear') which I got from here: http://stackoverflow.com/questions/2084508/clear-terminal-in-python There are several more complex ways to do it in there Link to comment Share on other sites More sharing options...
Recommended Posts