High quality, simple random password generator

前端 未结 27 2468
渐次进展
渐次进展 2020-12-22 17:06

I\'m interested in creating a very simple, high (cryptographic) quality random password generator. Is there a better way to do this?

import os, random, strin         


        
27条回答
  •  無奈伤痛
    2020-12-22 17:20

    Another implemention of the XKCD method:

    #!/usr/bin/env python
    import random
    import re
    
    # apt-get install wbritish
    def randomWords(num, dictionary="/usr/share/dict/british-english"):
      r = random.SystemRandom() # i.e. preferably not pseudo-random
      f = open(dictionary, "r")
      count = 0
      chosen = []
      for i in range(num):
        chosen.append("")
      prog = re.compile("^[a-z]{5,9}$") # reasonable length, no proper nouns
      if(f):
        for word in f:
          if(prog.match(word)):
            for i in range(num): # generate all words in one pass thru file
              if(r.randint(0,count) == 0): 
                chosen[i] = word.strip()
            count += 1
      return(chosen)
    
    def genPassword(num=4):
      return(" ".join(randomWords(num)))
    
    if(__name__ == "__main__"):
      print genPassword()
    

    Sample output:

    $ ./randompassword.py
    affluent afford scarlets twines
    $ ./randompassword.py
    speedboat ellipse further staffer
    

提交回复
热议问题