annotate MoinMoin/support/md5crypt.py @ 5731:8692137f2ccc

md5crypt: fix python 2.4 compatibility, untested
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 26 Nov 2010 21:21:16 +0100
parents 69668ad0cae7
children
rev   line source
5720
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 #########################################################
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 # md5crypt.py
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 #
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4 # 0423.2000 by michal wallace http://www.sabren.com/
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 # based on perl's Crypt::PasswdMD5 by Luis Munoz (lem@cantv.net)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 # based on /usr/src/libcrypt/crypt.c from FreeBSD 2.2.5-RELEASE
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7 #
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8 # MANY THANKS TO
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 #
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10 # Carey Evans - http://home.clear.net.nz/pages/c.evans/
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 # Dennis Marti - http://users.starpower.net/marti1/
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 #
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 # For the patches that got this thing working!
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 #
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 #########################################################
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 """md5crypt.py - Provides interoperable MD5-based crypt() function
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 SYNOPSIS
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 import md5crypt.py
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 cryptedpassword = md5crypt.md5crypt(password, salt);
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 DESCRIPTION
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 unix_md5_crypt() provides a crypt()-compatible interface to the
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 rather new MD5-based crypt() function found in modern operating systems.
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 It's based on the implementation found on FreeBSD 2.2.[56]-RELEASE and
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 contains the following license in it:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 "THE BEER-WARE LICENSE" (Revision 42):
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 can do whatever you want with this stuff. If we meet some day, and you think
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 apache_md5_crypt() provides a function compatible with Apache's
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37 .htpasswd files. This was contributed by Bryan Hart <bryan@eai.com>.
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 """
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41 MAGIC = '$1$' # Magic string
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43
5731
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
44 try:
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
45 import hashlib
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
46 hash_md5 = hashlib.md5
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
47 except ImportError:
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
48 # maybe we have python < 2.5 (no hashlib)
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
49 import md5
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
50 hash_md5 = md5.new
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
51
5720
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 def to64 (v, n):
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 ret = ''
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 while (n - 1 >= 0):
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56 n = n - 1
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57 ret = ret + ITOA64[v & 0x3f]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 v = v >> 6
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 return ret
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 def apache_md5_crypt (pw, salt):
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 # change the Magic string to match the one used by Apache
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 return unix_md5_crypt(pw, salt, '$apr1$')
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 def unix_md5_crypt(pw, salt, magic=None):
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 if magic==None:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 magic = MAGIC
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 # Take care of the magic string if present
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 if salt[:len(magic)] == magic:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 salt = salt[len(magic):]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 # salt can have up to 8 characters:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 import string
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 salt = string.split(salt, '$', 1)[0]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 salt = salt[:8]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 ctx = pw + magic + salt
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83
5731
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
84 md5 = hash_md5()
5720
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85 md5.update(pw + salt + pw)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86 final = md5.digest()
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 for pl in range(len(pw),0,-16):
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 if pl > 16:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 ctx = ctx + final[:16]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91 else:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92 ctx = ctx + final[:pl]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 # Now the 'weird' xform (??)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 i = len(pw)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98 while i:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99 if i & 1:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
100 ctx = ctx + chr(0) #if ($i & 1) { $ctx->add(pack("C", 0)); }
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101 else:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102 ctx = ctx + pw[0]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 i = i >> 1
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104
5731
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
105 md5 = hash_md5()
5720
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
106 md5.update(ctx)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
107 final = md5.digest()
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 # The following is supposed to make
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 # things run slower.
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
111
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 # my question: WTF???
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114 for i in range(1000):
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115 ctx1 = ''
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
116 if i & 1:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
117 ctx1 = ctx1 + pw
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
118 else:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
119 ctx1 = ctx1 + final[:16]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
121 if i % 3:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
122 ctx1 = ctx1 + salt
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
123
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
124 if i % 7:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
125 ctx1 = ctx1 + pw
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
126
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
127 if i & 1:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
128 ctx1 = ctx1 + final[:16]
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
129 else:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
130 ctx1 = ctx1 + pw
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
131
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
132
5731
8692137f2ccc md5crypt: fix python 2.4 compatibility, untested
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5720
diff changeset
133 md5 = hash_md5()
5720
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
134 md5.update(ctx1)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
135 final = md5.digest()
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
136
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
137
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
138 # Final xform
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
139
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
140 passwd = ''
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
141
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
142 passwd = passwd + to64((int(ord(final[0])) << 16)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
143 |(int(ord(final[6])) << 8)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144 |(int(ord(final[12]))),4)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
145
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
146 passwd = passwd + to64((int(ord(final[1])) << 16)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 |(int(ord(final[7])) << 8)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
148 |(int(ord(final[13]))), 4)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
149
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
150 passwd = passwd + to64((int(ord(final[2])) << 16)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
151 |(int(ord(final[8])) << 8)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
152 |(int(ord(final[14]))), 4)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
153
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
154 passwd = passwd + to64((int(ord(final[3])) << 16)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
155 |(int(ord(final[9])) << 8)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
156 |(int(ord(final[15]))), 4)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
157
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
158 passwd = passwd + to64((int(ord(final[4])) << 16)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159 |(int(ord(final[10])) << 8)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160 |(int(ord(final[5]))), 4)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
162 passwd = passwd + to64((int(ord(final[11]))), 2)
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
163
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
164
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
165 return magic + salt + '$' + passwd
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
166
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
167
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
168 ## assign a wrapper function:
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
169 md5crypt = unix_md5_crypt
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
170
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
171 if __name__ == "__main__":
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
172 print unix_md5_crypt("cat", "hat")
69668ad0cae7 add support for other password hashes (thanks to Michael Foetsch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
173