[英]python - random string generator _help_
this is a stupid question ... honestly i don't really have a clue at the moment and am pretty novice to python... 这是一个愚蠢的问题...说实话,我目前还没有任何线索,并且是python的新手...
I'm currently wrecking my head on a python script to generate a random password. 我目前正在用python脚本破坏头以生成随机密码。 I found a good start from here example by 'Omid Raha'
我发现从一个良好的开端这里例如通过“奥米德拉哈”
edit: on revist, this example is far to complex for something that appears to have much simpler ways of performing the same task... 编辑: 在revist上,此示例对于似乎具有执行相同任务的更简单方法的某些事情而言,已远远不复杂...
import random
import hashlib
import time
"""
This script is adapted from an example found here:https://stackoverflow.com/questions/18319101/whats-the-best-way-to-generate-random-strings-of-a-specific-length-in-python ; originally provided by user 'Omid Raha'
"""
SECRET_KEY = 'ffdsat9asdf5o5u9HKHvurtiasdf1tg1V36jyasdfSv8Ppin9O'
try:
random = random.SystemRandom()
using_sysrandom = True
except NotImplementedError:
import warnings
warnings.warn('A secure pseudo-random number generator is not available '
'on your system. Falling back to Mersenne Twister.')
using_sysrandom = False
def get_random_string(length=12,
allowed_chars='abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
'%*/-_@'):
"""
Returns a securely generated random string.
The default length of 12 with the a-z, A-Z, 0-9 character set returns
a 71-bit value. log_2((26+26+10)^12) =~ 71 bits
"""
if not using_sysrandom:
# This is ugly, and a hack, but it makes things better than
# the alternative of predictability. This re-seeds the PRNG
# using a value that is hard for an attacker to predict, every
# time a random string is required. This may change the
# properties of the chosen random sequence slightly, but this
# is better than absolute predictability.
random.seed(
hashlib.sha256(
("%s%s%s" % (
random.getstate(),
time.time(),
SECRET_KEY)).encode('utf-8')
).digest())
return ''.join(random.choice(allowed_chars) for i in range(length))
print (get_random_string)
simply returns: 只需返回:
<function get_random_string at 0x1034f7848>
I have no clue what this means... or if I'm even properly executing the script. 我不知道这意味着什么...或者我是否正确执行了脚本。
edit: 编辑:
thank you, for reading 谢谢你的阅读
stumbling along... found this: 跌跌撞撞...发现了这一点:
from OpenSSL import rand
p = rand.seed("lolNOmoreBADpasswds12")
print(p)
that: 那:
import os, random, string
length = 12
chars = string.ascii_letters + string.digits + '!@#$%^&*()'
random.seed = (os.urandom(1024))
print ''.join(random.choice(chars) for i in range(length))
and the other: 和另一个:
from OpenSSL import rand
p = rand.bytes("12")
print(p)
playing with things like this, that, and the other now... :)
edit: all the above methods suck 玩这样的事情,那个,和其他的...
:)
编辑: 以上所有方法都很烂
given: xkcd rocks 鉴于: xkcd岩石
lets consider this as a best path forward (for random password generator thats memorable) given you have a file containing all the word strings you'd like to include : 假设您有一个包含所有要包含的单词字符串的文件,那么可以考虑将其视为最佳的前进路径(对于令人难以忘怀的随机密码生成器) :
import random, string, os
word_file = "./wordlist"
words = open(word_file).read().splitlines()
part1 = random.choice(words)
part2 = random.choice(words)
part3 = random.choice(words)
part4 = random.choice(words)
phrase = part1.capitalize()+part2+part3.capitalize()+part4
print phrase
The simplest random string Generator i know: 我知道的最简单的随机字符串生成器:
import random, string;
all_chars = string.ascii_lowercase
def randstr(length):
result = str()
for i in range(length):
result += random.choice(all_chars)
return(result)
print(randstr(15))
if you want to change the amount of possible characters just change the all_chars variable: 如果要更改可能的字符数,只需更改all_chars变量:
For example: 例如:
If you want digits and characters: 如果需要数字和字符:
all_chars = string.ascii_lowercase + string.digits
For digits and uppercase and lowercase characters: 对于数字以及大小写字符:
all_chars = string.ascii_letters + string.digits
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.