python - 随机字符串生成器_help_

时间:2015-10-08 14:23:59

标签: python function random printing

这是一个愚蠢的问题......老实说,我此刻并不是真的有线索,而且对于python来说我是新手......

我目前正在破解python脚本以生成随机密码。我从{Omid Raha'{/ 3>的here例子中找到了一个良好的开端 关于revist的

编辑: ,这个例子对于看似有更简单的方法执行相同任务的事情来说非常复杂......

[i,j]

简单地返回:

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)

我不知道这意味着什么......或者我是否正确执行了脚本。

编辑:

谢谢阅读

2 个答案:

答案 0 :(得分:0)

绊脚石......发现了这个:

from OpenSSL import rand

p = rand.seed("lolNOmoreBADpasswds12")
print(p)

的是:

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))

和另一个:

from OpenSSL import rand

p = rand.bytes("12")
print(p)

玩这样的东西,那个,现在另一个...... :) 编辑: 上述所有方法都很糟糕

给出:xkcd rocks

让我们将此视为前进的最佳路径(对于随机密码生成器而言令人难忘) ,因为您有一个包含您想要包含的所有单词字符串的文件

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

答案 1 :(得分:0)

我知道的最简单的随机字符串生成器

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))

如果要更改可能的字符数,只需更改all_chars变量:
例如:

如果您需要数字和字符:

all_chars = string.ascii_lowercase + string.digits

对于数字以及大小写字符:

all_chars = string.ascii_letters + string.digits


Documentation

相关问题