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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python

# Written by Daniel Schmidt
# CGI password generator I threw together when I couldn't find anything better.  GPLv3 or greater

# Import the CGI module
import cgi, sys, crypt, os, random, string

# Required header that tells the browser how to render the HTML.
print "Content-Type: text/html\n\n"

# Define function to generate HTML form.
def generate_form():
    print "<HTML>\n"
    print "<HEAD>\n"
    print "\t<TITLE>Generate Password Hash</TITLE>\n"
    print "</HEAD>\n"
    print "<BODY BGCOLOR = white>\n"
    print "\t<H3>Generate Password Hash</H3>\n"
    print "\t<TABLE BORDER = 0>\n"
    print "\t\t<FORM METHOD = post ACTION = \
    \"gen_pass.cgi\">\n"
    print "\t\t<TR><TH>Enter password:</TH><TD><INPUT type = password \
    name = \"the_pass\" value = \"\"></TD><TR>\n"
    print "\t\t<TR><TH>Enter again :</TH><TD><INPUT type = password \
    name = \"the_pass2\" value = \"\"></TD><TR>\n"
    print "\t</TABLE>\n"
    print "\t<INPUT TYPE = hidden NAME = \"action\" VALUE = \
    \"display\">\n"
    print "\t<INPUT TYPE = submit VALUE = \"Enter\">\n"
    print "\t</FORM>\n"
    print "</BODY>\n"
    print "</HTML>\n"

# Quick random seed generator
# (Thanks to tzotzioy)
def random_md5like_hash():
    available_chars= string.hexdigits[:16]
    return ''.join(
        random.choice(available_chars)
        for dummy in xrange(8))

# call crypt with a random seed
def hash_it(the_pass):
    return(crypt.crypt(the_pass, '$6$%s$' % random_md5like_hash()))

# Print the hash result
def display_hash(the_pass):
    print "<HTML>\n"
    print "<HEAD>\n"
    print "\t<TITLE>Info Form</TITLE>\n"
    print "</HEAD>\n"
    print "<BODY BGCOLOR = white>\n"
    print "Hashed Password:"
    print "<pre>"
    print hash_it(the_pass)
    print "</pre>"
    print "</BODY>\n"
    print "</HTML>\n"

# Define main function.
def main():
    form = cgi.FieldStorage()
    if (form.has_key("the_pass")):
        if (form["the_pass"].value == form["the_pass2"].value):
            display_hash(form["the_pass"].value)
        else:
            print "Passwords do not match."
    else:
        generate_form()
    

# Call main function.
main()