Welcome, guest | Sign In | My Account | Store | Cart

A password generator that uses OS facilities to generate none pseudo random numbers. the SystemRandom uses CryptGenRandom in Windows and /dev/random in linux and it is so better to use this to create random numbers in cryptography or any other security areas.

Python, 40 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
a secure password generator
"""
import random,string,sqlite3
def shuffle(cont):  # we can use random.shuffle but i prefer to use this
    "cont:container"
    lim=len(cont)-1
    c=list(cont)
    for _ in "."*(lim/2):  
        a=random.randint(0,lim)
        b=random.randint(0,lim)
        t=c[a]
        c[a]=c[b]
        c[b]=t
    return "".join(c)

def Gen_Pass(name):
    n=random.randint(7,10)  #Length of password 
    #--------------------Data Base
    conn=sqlite3.connect("pass.db")
    cur=conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS passwords(user TEXT UNIQUE,pass TEXT UNIQUE)")
    #--------------------
    rand=random.SystemRandom()
    lst=list(string.letters[26:52]+string.digits[1:])#lst=list(string.letters[:52])
    word=""
    for i in range(n):
        word+=lst[rand.randint(0,len(lst)-1)]
    word=shuffle(word)
    try:
        cur.execute("INSERT OR IGNORE INTO passwords(user,pass) VALUES (?,?)",(name,word))
        conn.commit()
        print "the pass is ",word
        print "ok , now it is inserted"
    except:
        print "there are some problems"
    cur.close()
    conn.close()
if __name__=="__main__":
    Gen_Pass("UserName")

1 comment

Alan Plum 12 years, 9 months ago  # | flag

Or just use this:

from string import printable
from random import choice

chars = printable[:87] + '_' + printable[90:95]

def make_password(length=16):
    return ''.join(choice(chars) for i in range(length))

This will use all ASCII lowercase and uppercase letters, all digits, as well as most ASCII symbols (excluding diacritics and special whitespace).