python 创建密码

使用:

    mkpass -l [password length] [-nu|-nl|-ns]

获取帮助:

    mkpass -h

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # _*_coding:utf8_*_
  3. # Auth by raysuen
  4. import sys,string,re
  5. import random
  6. class MakePass(object):
  7.     rPassInfo={
  8.         “Length”:8,
  9.         “NoUpper”:False,
  10.         “NoLower”:False,
  11.         “NoSysbol”:False
  12.            }
  13.     def __init__(self,passlen=8,nu=False,nl=False,ns=False):
  14.         if passlen != 8:
  15.             self.rPassInfo[“Length”] = passlen
  16.         if nu == True:
  17.             self.rPassInfo[“NoUpper”] = True
  18.         if nl == True:
  19.             self.rPassInfo[“NoLower”] = True
  20.         if ns == True:
  21.             self.rPassInfo[“NoSysbol”] = True
  22.     def MkPass(self):
  23.         rPassStr={
  24.             “passStr”:,
  25.             “AllChar”:[],
  26.             “pwIndex”:[],
  27.             “num”:0
  28.         }
  29.         if self.rPassInfo[“NoUpper”] == False:
  30.             rPassStr[“AllChar”].append(random.choice(string.ascii_uppercase))
  31.             rPassStr[“passStr”] = rPassStr[“passStr”]+.join(string.ascii_lowercase)
  32.             rPassStr[“num”] += 1
  33.         if self.rPassInfo[“NoLower”] == False:
  34.             rPassStr[“AllChar”].append(random.choice(string.ascii_lowercase))
  35.             rPassStr[“passStr”] = rPassStr[“passStr”] + .join(string.ascii_lowercase)
  36.             rPassStr[“num”] += 1
  37.         if self.rPassInfo[“NoSysbol”] == False:
  38.             rPassStr[“AllChar”].append(random.choice(“!@#$%&*()+.,”))
  39.             rPassStr[“passStr”] = rPassStr[“passStr”] + “!@#$%&*()+.,”
  40.             rPassStr[“num”] += 1
  41.         rPassStr[“passStr”] = rPassStr[“passStr”] + .join(string.digits)
  42.         rPassStr[“AllChar”].append(random.choice(string.digits))
  43.         rPassStr[“num”] += 1
  44.         rPassStr[“pwIndex”].extend(random.sample(range(1,self.rPassInfo[“Length”]),rPassStr[“num”]))
  45.         rPassStr[“pwIndex”].sort()
  46.         n = 0
  47.         i = 0
  48.         res =
  49.         while i < self.rPassInfo[“Length”]:
  50.             isMatch = False
  51.             for j in rPassStr[“pwIndex”]:
  52.                 if i == j:
  53.                     res = res + .join(rPassStr[“AllChar”][n])
  54.                     n += 1
  55.                     i += 1
  56.                     isMatch = True
  57.                     break
  58.             if isMatch == False:
  59.                 res = res + .join(random.choice(rPassStr[“passStr”]))
  60.                 i += 1
  61.         return res
  62. def func_help():
  63.     print(“”
  64.         Name:
  65.             mkpass –make password
  66.         
  67.         Synopsis:
  68.             mkpass -l [password length] [-nu|-nl|-ns]
  69.             
  70.         Description:
  71.             -l Specify a number for the length of password,defaul 8
  72.             -nu Don’t use upper
  73.             -nl Don’t use lower
  74.             -ns Don’t use special character
  75.             -h Get help
  76.     ““”)
  77. if __name__==‘__main__’:
  78.     i = 1
  79.     mp = MakePass()
  80.     while i < len(sys.argv):
  81.         if sys.argv[i] == ‘-l’: #l means length
  82.             if i+1 > len(sys.argv):
  83.                 print(“The value of -l must be specified!!!”)
  84.                 exit(1)
  85.             elif re.match(“^-“, sys.argv[i+1]) != None: #判断下个参数是否以开头
  86.                 print(“The value of -l must be specified!!!”)
  87.                 exit(1)
  88.             elif sys.argv[i+1].isdecimal() == False: #判断l下一个参数是否为数字
  89.                 print(“The value of -l must be digits”)
  90.                 exit(1)
  91.             i += 1
  92.             mp.rPassInfo[“Length”] = int(sys.argv[i]) #获取l参数的value
  93.         elif sys.argv[i] == ‘-nu’: #nu 指定不适用大写字符
  94.             mp.rPassInfo[“NoUpper”] = True
  95.         elif sys.argv[i] == ‘-nl’: #nu 指定不适用小写字符
  96.             mp.rPassInfo[“NoLower”] = True
  97.         elif sys.argv[i] == ‘-ns’: #nu 指定不适用特殊字符
  98.             mp.rPassInfo[“NoSysbol”] = True
  99.         elif sys.argv[i] == ‘-h’:
  100.             func_help()
  101.             exit(0)
  102.         i += 1
  103.     print(mp.MkPass())
    原文作者:raysuen
    原文地址: http://blog.itpub.net/28572479/viewspace-2153864/
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞