使用Python生成随机密码的示例分享
作者:goldensun 时间:2023-05-23 11:32:10
生成随机密码这件事情用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,随机密码
0
投稿
猜你喜欢
django中只使用ModleForm的表单验证
2021-02-03 15:41:50
python实现简单石头剪刀布游戏
2023-03-08 03:23:44
asp.net连接数据库 增加,修改,删除,查询代码
2024-01-20 15:52:31
一个20行左右的强$代码
2009-12-25 15:22:00
asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)
2024-01-12 13:28:10
使用springboot通过spi机制加载mysql驱动的过程
2024-01-27 09:12:26
玩转markdown 分享几个需要用到的工具
2023-01-29 22:53:20
GO语言的控制语句详解包括GO语言的指针语法
2024-02-21 22:17:27
python使用matplotlib画柱状图、散点图
2023-05-18 11:50:06
Go语言实现互斥锁、随机数、time、List
2024-04-25 15:11:24
使用Visual Studio 2022开发前端的详细教程
2023-07-02 05:30:35
利用PyCharm Profile分析异步爬虫效率详解
2023-08-15 03:02:58
pyinstaller 3.6版本通过pip安装失败的解决办法(推荐)
2023-10-13 11:55:44
win10安装tensorflow-gpu1.8.0详细完整步骤
2023-01-22 08:44:33
javascript实现计算器功能详解流程
2024-04-23 09:27:00
Numpy 理解ndarray对象的示例代码
2023-07-17 00:56:20
简单的Python的curses库使用教程
2023-11-06 15:04:28
五种方法解决 Web2.0设计中的匹配度
2007-09-22 10:58:00
Spring Boot中使用Spring-data-jpa实现数据库增删查改
2024-01-28 09:41:28
django queryset相加和筛选教程
2022-09-10 11:04:43