生成隨機密碼這件事情用python來干確實相當的方便,優美的string方法加上choice簡直是絕配
make_password.py
###簡單幾行代碼執行即可生成記不住的字符串### $ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ...
$ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ...
代碼如下――注釋比代碼長
#!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_passwd # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- from random import choice import string def Makepass(length=8, chars=string.letters+string.digits): return ''.join([choice(chars) for i in range(length)]) if __name__ == '__main__': for i in range(10): print Makepass() ##下例基本上就是這個小腳本的所有工作核心了,使用random模塊的choice方法取string模塊生成的字符串## >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' >>> choice(string.digits) '4' >>> choice(string.letters) 'T' ##有關生成器可參考:http://www.ipython.me/python/python-generator.html## #!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_passwd # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- from random import choice import string def Makepass(length=8, chars=string.letters+string.digits): return ''.join([choice(chars) for i in range(length)]) if __name__ == '__main__': for i in range(10): print Makepass() ##下例基本上就是這個小腳本的所有工作核心了,使用random模塊的choice方法取string模塊生成的字符串## >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' >>> choice(string.digits) '4' >>> choice(string.letters) 'T' ##有關生成器可參考:http://www.ipython.me/python/python-generator.html##
?
生成一些人似乎能好記一些的密碼(Qs4Wm84q這種密碼似乎除了復制粘貼沒有別的選擇,話說前年我使用shell生成的類似的密碼給ldap做默認密碼,我當時公司就真有員工把這樣的密碼背下來了,現在想想真心是厲害~~~)。
##這樣看起來是比上面的好記一點了吧,但需要提供一個字典文件## $ python make_dictpass.py 1 8 1 ipythosd $ python make_dictpass.py nahontchen chenyibfeo ipythoniue coreostche ... $ python make_dictpass.py 1 8 1 ipythosd $ python make_dictpass.py nahontchen chenyibfeo ipythoniue coreostche ...
代碼如下
#!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_dictpass # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- import random import string class passwd(): data = open('./word.txt').read().lower() def renew(self, n, maxmem=3): self.chars = [] for i in range(n): randspot = random.randrange(len(self.data)) self.data = self.data[randspot:] + self.data[:randspot] where = -1 locate = ''.join(self.chars[-maxmem:]) while where < 0 and locate: where = self.data.find(locate) locate = locate[1:] c = self.data[where+len(locate)+1] if not c.islower(): c = random.choice(string.lowercase) self.chars.append(c) def __str__(self): return ''.join(self.chars) if __name__ == '__main__': import sys ##如果帶參數的話可以定義生成密碼的次數,長度,追溯記錄## if len(sys.argv) > 1: dopass = int(sys.argv[1]) else: dopass = 8 if len(sys.argv) > 2: length = int(sys.argv[2]) else: length = 10 if len(sys.argv) > 3: memory = int(sys.argv[3]) else: memory = 3 onepass = passwd() for i in range(dopass): onepass.renew(length,memory) print onepass
##字典文件(可以是各種單詞的組合)## $ cat word.txt chenyi itchenyi python ipython coreos coreos.me ipython.me
#!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_dictpass # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- import random import string class passwd(): data = open('./word.txt').read().lower() def renew(self, n, maxmem=3): self.chars = [] for i in range(n): randspot = random.randrange(len(self.data)) self.data = self.data[randspot:] + self.data[:randspot] where = -1 locate = ''.join(self.chars[-maxmem:]) while where < 0 and locate: where = self.data.find(locate) locate = locate[1:] c = self.data[where+len(locate)+1] if not c.islower(): c = random.choice(string.lowercase) self.chars.append(c) def __str__(self): return ''.join(self.chars) if __name__ == '__main__': import sys ##如果帶參數的話可以定義生成密碼的次數,長度,追溯記錄## if len(sys.argv) > 1: dopass = int(sys.argv[1]) else: dopass = 8 if len(sys.argv) > 2: length = int(sys.argv[2]) else: length = 10 if len(sys.argv) > 3: memory = int(sys.argv[3]) else: memory = 3 onepass = passwd() for i in range(dopass): onepass.renew(length,memory) print onepass
##字典文件(可以是各種單詞的組合)## $ cat word.txt chenyi itchenyi python ipython coreos coreos.me ipython.me
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
