python 建立密碼

raysuen發表於2018-05-06
使用:
    mkpass -l [password length] [-nu|-nl|-ns]
獲取幫助:
    mkpass -h



點選(此處)摺疊或開啟

  1. #!/usr/bin/env python3
  2. # _*_coding:utf-8_*_
  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())





來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28572479/viewspace-2153864/,如需轉載,請註明出處,否則將追究法律責任。

相關文章