使用Python生成随机密码的示例分享


Posted in Python onFebruary 18, 2016

生成随机密码这件事情用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
Python 相关文章推荐
Python多线程学习资料
Dec 19 Python
Python中返回字典键的值的values()方法使用
May 22 Python
Python脚本暴力破解栅栏密码
Oct 19 Python
Python 中urls.py:URL dispatcher(路由配置文件)详解
Mar 24 Python
python实现维吉尼亚算法
Mar 20 Python
Python实现查找字符串数组最长公共前缀示例
Mar 27 Python
PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上
Apr 01 Python
python图像处理入门(一)
Apr 04 Python
python 实现交换两个列表元素的位置示例
Jun 26 Python
python3的数据类型及数据类型转换实例详解
Aug 20 Python
python装饰器相当于函数的调用方式
Dec 27 Python
Opencv常见图像格式Data Type及代码实例
Nov 02 Python
使用Python的urllib2模块处理url和图片的技巧两则
Feb 18 #Python
讲解Python的Scrapy爬虫框架使用代理进行采集的方法
Feb 18 #Python
使用Python的PIL模块来进行图片对比
Feb 18 #Python
使用Python来编写HTTP服务器的超级指南
Feb 18 #Python
python装饰器与递归算法详解
Feb 18 #Python
Python利用Nagios增加微信报警通知的功能
Feb 18 #Python
Python多线程、异步+多进程爬虫实现代码
Feb 17 #Python
You might like
PHP 的几个配置文件函数
2006/12/21 PHP
php日历[测试通过]
2008/03/27 PHP
PHP strtr() 函数使用说明
2008/11/21 PHP
详解PHP的Yii框架中组件行为的属性注入和方法注入
2016/03/18 PHP
js函数排序的实例代码
2013/07/01 Javascript
javascript中的=等号个数问题两个跟三个有什么区别
2013/10/23 Javascript
JavaScript限定图片显示大小的方法
2015/03/11 Javascript
JavaScript基础篇(3)之Object、Function等引用类型
2015/11/30 Javascript
jQuery实现查找链接文字替换属性的方法
2016/06/27 Javascript
easyui form validate总是返回false的原因及解决方法
2016/11/07 Javascript
详解如何提高 webpack 构建 Vue 项目的速度
2017/07/03 Javascript
JavaScript插件Tab选项卡效果
2017/11/14 Javascript
JS实现为动态创建的元素添加事件操作示例
2018/03/17 Javascript
vue实现简单的MVVM框架
2018/08/05 Javascript
Element UI 自定义正则表达式验证方法
2018/09/04 Javascript
javascript将非数值转换为数值
2018/09/13 Javascript
vue通过style或者class改变样式的实例代码
2018/10/30 Javascript
jQuery中DOM常见操作实例小结
2019/08/01 jQuery
如何实现小程序与小程序之间的跳转
2020/11/04 Javascript
[08:47]2018国际邀请赛 OG战队举杯时刻
2018/08/29 DOTA
9种python web 程序的部署方式小结
2014/06/30 Python
详解Python2.x中对Unicode编码的使用
2015/04/03 Python
Python使用自带的ConfigParser模块读写ini配置文件
2016/06/26 Python
Tensorflow卷积神经网络实例进阶
2018/05/24 Python
Django中Middleware中的函数详解
2019/07/18 Python
python计算无向图节点度的实例代码
2019/11/22 Python
Python 测试框架unittest和pytest的优劣
2020/09/26 Python
Aerosoles爱柔仕官网:美国舒软女鞋品牌
2017/07/17 全球购物
zooplus意大利:在线宠物商店
2019/08/07 全球购物
请写一个C函数,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1
2015/07/16 面试题
会计系中文个人求职信
2013/12/24 职场文书
网上商城创业计划书范文
2014/01/31 职场文书
家长学校工作方案
2014/05/07 职场文书
政协调研汇报材料
2014/08/15 职场文书
CSS几步实现赛博朋克2077风格视觉效果
2021/06/16 HTML / CSS
postgresql之greenplum字符串去重拼接方式
2023/05/08 PostgreSQL