#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    31
    Rep Power
    2

    Suggestions/Help on Password Generator


    Today I had to set up security on the wireless router and remember seeing awhile ago something about a random password generator written in Python. I wanted to write a script that would create psuedo-random passwords and pulled out the Python 2.7.3 reference and made one that was sufficient for me. My main goal was creating a totally random password for the router. The initial script only created a string of 10 alpha and numeric characters.

    In my boredom tonight I elaborated on it some more just for the heck of it. Here is the code:

    Code:
    import random
    import android
    
    droid = android.Android()
    
    
    def new5display():
      title = 'Here it is'
      message = '{0}       {1}       {2}       {3}       {4}'.format(new5[0], new5[1], new5[2], new5[3], new5[4])
      droid.dialogCreateAlert(title,message)
      droid.dialogSetPositiveButtonText('Ok')
      droid.dialogShow()
      response = droid.dialogGetResponse().result
      return response['which'] == 'positive' 
    
    
    def new10display():
      title = 'Here it is'
      message = '{0}       {1}       {2}       {3}       {4}       {5}       {6}       {7}       {8}       {9}'.format(new10[0], new10[1], new10[2], new10[3], new10[4], new10[5], new10[6], new10[7], new10[8], new10[9])
      droid.dialogCreateAlert(title,message)
      droid.dialogSetPositiveButtonText('Ok')
      droid.dialogShow()
      response = droid.dialogGetResponse().result
      return response['which'] == 'positive' 
     
    
    
     
    def intro():
      title = 'Random Password Generator'
      message = 'By: 16410'
      droid.dialogCreateAlert(title,message)
      droid.dialogSetPositiveButtonText('Ok')
      droid.dialogShow()
      response = droid.dialogGetResponse().result
      return response['which'] == 'positive'
    
    
    what = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    
    who = [1,2,3,4,5,6,7,8,9]
    
    random.shuffle(what)
    random.shuffle(who)
    
    
    intro()
    
    
    comt = 1
    
    while comt == 1:
    
      new5  = []
      new10 = []
    
      title = 'Which kind of password?'
      droid.dialogCreateAlert(title)
      droid.dialogSetSingleChoiceItems(['Random Alphanumeric 5-Characters', 'Random Alphanumeric 10-Characters', '5 Random Letters', '10 Random Letters'])
      droid.dialogSetPositiveButtonText('Ok')
      droid.dialogShow()
      result = droid.dialogGetResponse().result
      ok = droid.dialogGetSelectedItems().result
    
    
      for item in ok: 
     
        if item == 0:
          rock = random.randint(1,5)
          if rock == 1:
            new5.append(what[0])
            new5.append(who[0])
            new5.append(what[1])
            new5.append(who[1])
            new5.append(what[2])
          elif rock == 2:
            new5.append(who[0])
            new5.append(what[0])
            new5.append(who[1])
            new5.append(what[1])
            new5.append(who[2])
          elif rock == 3:
            new5.append(who[0])
            new5.append(who[1])
            new5.append(what[0])
            new5.append(who[2])
            new5.append(what[1])
          elif rock == 4:
            new5.append(what[0])
            new5.append(who[0])
            new5.append(who[1])
            new5.append(what[1])
            new5.append(what[2])  
          elif rock == 5:
            new5.append(what[0])
            new5.append(what[1])
            new5.append(who[0])
            new5.append(what[2])
            new5.append(who[1])
        
    
          new5display()
    
    
          title = 'Another Go?'
          droid.dialogCreateAlert(title)
          droid.dialogSetPositiveButtonText('Yes')
          droid.dialogSetNegativeButtonText('No')
          droid.dialogShow()
          response = droid.dialogGetResponse().result
          another = response['which'] == 'positive'
    
          if another:
            random.shuffle(what)
            random.shuffle(who)
          else:
            comt +=1
          
    
        elif item == 1:
        
          rot = random.randint(1,5)
    
          if rot == 1:
            new10.append(what[0])
            new10.append(who[0])
            new10.append(what[1])
            new10.append(who[1])
            new10.append(what[2])
            new10.append(who[2])
            new10.append(what[3])
            new10.append(who[3])
            new10.append(what[4])
            new10.append(who[4])
    
          elif rot == 2:
            new10.append(who[0])
            new10.append(who[1])
            new10.append(what[0])
            new10.append(who[2])
            new10.append(what[1])
            new10.append(what[2])
            new10.append(who[3])
            new10.append(who[4])
            new10.append(what[3])
            new10.append(what[4])
    
          elif rot == 3:
            new10.append(what[0])
            new10.append(what[1])
            new10.append(who[0])
            new10.append(who[1])
            new10.append(what[2])
            new10.append(who[2])
            new10.append(who[3])
            new10.append(what[3])
            new10.append(what[4])
            new10.append(who[4])
    
          elif rot == 4:
            new10.append(who[0])
            new10.append(who[1])
            new10.append(what[0])
            new10.append(who[2])
            new10.append(what[1])
            new10.append(what[2])
            new10.append(who[3])
            new10.append(who[4])
            new10.append(what[3])
            new10.append(who[5])
    
          elif rot == 5:
            new10.append(what[0])
            new10.append(who[0])
            new10.append(who[1])
            new10.append(what[1])
            new10.append(what[2])
            new10.append(who[2])
            new10.append(who[3])
            new10.append(what[3])
            new10.append(what[4])
            new10.append(who[4]) 
    
    
          new10display()  
    
          title = 'Another Go?'
          droid.dialogCreateAlert(title)
          droid.dialogSetPositiveButtonText('Yes')
          droid.dialogSetNegativeButtonText('No')
          droid.dialogShow()
          response = droid.dialogGetResponse().result
          another1 = response['which'] == 'positive' 
         
          if another1:
            random.shuffle(what)
            random.shuffle(who)
          else:
            comt +=1      
        
    
        elif item == 2:
          new5.append(what[0])
          new5.append(what[1])
          new5.append(what[2])
          new5.append(what[3])
          new5.append(what[4])
    
          new5display() 
    
          title = 'Another Go?'
          droid.dialogCreateAlert(title)
          droid.dialogSetPositiveButtonText('Yes')
          droid.dialogSetNegativeButtonText('No')
          droid.dialogShow()
          response = droid.dialogGetResponse().result
          another2 = response['which'] == 'positive' 
     
          if another2:
            random.shuffle(what)
            random.shuffle(who)
          else:
            comt +=1
    
        elif item == 3:
          new10.append(what[0])
          new10.append(what[1])
          new10.append(what[2])
          new10.append(what[3])
          new10.append(what[4])
          new10.append(what[5])
          new10.append(what[6])
          new10.append(what[7])
          new10.append(what[8])
          new10.append(what[9])
          
          new10display()
       
          title = 'Another Go?'
          droid.dialogCreateAlert(title)
          droid.dialogSetPositiveButtonText('Yes')
          droid.dialogSetNegativeButtonText('No')
          droid.dialogShow()
          response = droid.dialogGetResponse().result
          another3 = response['which'] == 'positive' 
    
          if another3:
            random.shuffle(what)
            random.shuffle(who)
          else:
            comt +=1
    Sorry for the length of this.....I was hoping someone could give some constructive criticism on this. There must be a more efficient way to achieve the same results. Any help or comments will be appreciated. Thanks for looking.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Yes, there are definately more efficient ways of doing this. I haven't looked, but there might be some methods in the random class to do just this.

    Anyway, I made a shorter version of what (I think) you're trying to achieve:

    Code:
    import random
    
    alphabetic_values = 'abcdefghijklmnopqrstuvwxyz'
    numeric_values = '1234567890'
    
    def get_password(length, numeric=True, alphabetic=True):
        password = ''
        alphabet = []
        if numeric:
            alphabet.extend(list(numeric_values))
        if alphabetic:
            alphabet.extend(list(alphabetic_values))
    
        for _ in xrange(length):
            password += str(random.choice(alphabet))
        return password
    
    print get_password(5, alphabetic=False)
    print get_password(10)
    print get_password(10, numeric=False)

    Comments on this post

    • Guitarman2010 agrees
    Last edited by partoj; April 10th, 2013 at 02:51 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    31
    Rep Power
    2
    Thanks for the reply......I appreciate all the help I can get

IMN logo majestic logo threadwatch logo seochat tools logo