]> git.phdru.name Git - m_lib.git/blob - m_lib/mcrypt.py
Remove wrong copyright lines, fix module docstrings
[m_lib.git] / m_lib / mcrypt.py
1 #! /usr/bin/env python
2
3 #
4 # useful function(s) for module crypt
5 #
6
7
8 import random, crypt
9
10
11 saltchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz0123456789./"
12 len_salt = len(saltchars)
13
14 def gen_salt():
15    """
16       There are some difference among modern unicies. BSD/OS, for example,
17       uses MD5 hash, and ignores salt completely. FreeBSD uses 3 different
18       versions of crypt() - with standard salt, with extended 9-byte salt,
19       and MD5 (again, ignoring salt at all).
20       This function generates salt for standard "Broken DES"-based crypt().
21    """
22    r1 = random.randint(0, len_salt-1)
23    r2 = random.randint(0, len_salt-1)
24    return "%s%s" % (saltchars[r1], saltchars[r2])
25
26
27 def test():
28    passwd = raw_input("Enter password: ")
29    salt = gen_salt()
30    encrypted = crypt.crypt(passwd, salt)
31
32    pwd_file = open("test.pwd", 'w')
33    pwd_file.write("%s:%s" % ("user", encrypted))
34    pwd_file.close()
35    print "Password file written"
36
37    import string
38    pwd_file = open("test.pwd", 'r')
39    username, encrypted = string.split(pwd_file.readline()[:-1], ':')
40    pwd_file.close()
41
42    if crypt.crypt(encrypted, encrypted):
43       print "Password verified Ok"
44    else:
45       print "BAD password"
46
47 if __name__ == "__main__":
48    test()