5 This script expects a mandatory plain passwd,
6 and an optional MD5-encoded passwd
8 If both are provided, we check that they match
9 Otherwise we return an encrypted passwd
12 __author__="Thierry Parmentelat, INRIA Sophia Antipolis"
19 from crypt import crypt
23 #################### md5 passwds syntax
25 re_magic='\$1\$' # $ needs \ in regular expression
29 print "Usage: %s plain [encrypted]"%sys.argv[0]
34 def getsalt(chars = string.letters + string.digits):
35 # generate a random 8-character 'salt'
36 return (random.choice(chars)
37 + random.choice(chars)
38 + random.choice(chars)
39 + random.choice(chars)
40 + random.choice(chars)
41 + random.choice(chars)
42 + random.choice(chars)
43 + random.choice(chars))
47 def compute_encrypted (plain):
48 return crypt(plain,magic+getsalt()+'$')
53 def check_encrypted (plain,passwd):
61 # print "re="+re_passwd
62 m_passwd=re.compile(re_passwd)
63 r=m_passwd.match(passwd)
66 print 'passwd wrong syntax %s'%passwd
70 checked=crypt(plain,magic+salt+'$')
71 ok = (checked==passwd)
77 (opts, argv) = getopt.getopt(sys.argv[1:], "h")
78 for (opt, optval) in opts:
87 encrypted=compute_encrypted(plain)
95 ok = check_encrypted (plain,passwd)
105 print "END should not occur"
108 if __name__ == '__main__':