2015-10-08 53 views
1

這是一個愚蠢的問題......老實說,我真的沒有在目前的線索和我相當新手蟒蛇...蟒蛇 - 隨機字符串發生器_help_

我目前衝倒我的頭一個生成隨機密碼的python腳本。我發現了一個良好的開端從here例如通過「奧米德拉哈」

編輯:上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' 
           'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
           '%*/[email protected]'): 
    """ 
    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) 

簡單的返回:

<function get_random_string at 0x1034f7848>

我不知道這意味着什麼......或者如果我前夕n正確執行腳本。

編輯:

謝謝你,閱讀

+4

明明白白地說,你要打印的函數的定義,當你想調用的函數,並打印值。嘗試'print(get_random_string())' - 函數名稱信號後面的括號,以便實際運行該函數,並獲取其生成的值。 – Monkpit

+0

@monkey謝謝:)這對未來的工作非常有幫助 – knope

回答

0

一起磕磕絆絆......發現這一點:

from OpenSSL import rand 

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

說:

import os, random, string 
length = 12 
chars = string.ascii_letters + string.digits + '[email protected]#$%^&*()' 
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

讓我們考慮這是一個最好的路徑(對於隨機密碼生成器,這讓人難忘)給定y OU有一個包含所有你想包括詞串的文件:

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