使用Python產生隨機密碼的樣本分享

來源:互聯網
上載者:User
產生隨機密碼這件事情用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 stringdef 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 stringdef 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 stringclass 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 stringclass 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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.