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