使用Python生成隨機密碼的示例分享
更新時間:2016年02月18日 15:41:22 投稿:goldensun
這篇文章主要介紹了使用Python生成隨機密碼的示例分享,比如發(fā)生一些安全問題時為用戶先設(shè)置隨機密碼然后供用戶修改的情況下可以用到,需要的朋友可以參考下
生成隨機密碼這件事情用python來干確實相當(dāng)?shù)姆奖悖瑑?yōu)美的string方法加上choice簡直是絕配
make_password.py
###簡單幾行代碼執(zhí)行即可生成記不住的字符串### $ 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' ##有關(guān)生成器可參考: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' ##有關(guān)生成器可參考:http://www.ipython.me/python/python-generator.html##
生成一些人似乎能好記一些的密碼(Qs4Wm84q這種密碼似乎除了復(fù)制粘貼沒有別的選擇,話說前年我使用shell生成的類似的密碼給ldap做默認(rèn)密碼,我當(dāng)時公司就真有員工把這樣的密碼背下來了,現(xiàn)在想想真心是厲害~~~)。
##這樣看起來是比上面的好記一點了吧,但需要提供一個字典文件## $ 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 ##如果帶參數(shù)的話可以定義生成密碼的次數(shù),長度,追溯記錄## 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 ##如果帶參數(shù)的話可以定義生成密碼的次數(shù),長度,追溯記錄## 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
相關(guān)文章
Python實現(xiàn)計算長方形面積(帶參數(shù)函數(shù)demo)
今天小編就為大家分享一篇Python實現(xiàn)計算長方形面積(帶參數(shù)函數(shù)demo),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01matlab中imadjust函數(shù)的作用及應(yīng)用舉例
這篇文章主要介紹了matlab中imadjust函數(shù)的作用及應(yīng)用舉例,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02