author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 16 Jun 2010 09:21:49 +0200 | |
changeset 5754 | 51179e0bb250 |
parent 5424 | 8ecbcbff9777 |
child 5770 | 9e4f8db66962 |
permissions | -rw-r--r-- |
0 | 1 |
######################################################### |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
2 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
3 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
4 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
5 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
6 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
7 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
8 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
9 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
10 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
11 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
12 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
14 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
15 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
16 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
17 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3149
diff
changeset
|
18 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
19 |
""" |
3149
c6a85fafb155
note about licence, fix copyright, fix case of unicode argument
Aurelien Campeas
parents:
2172
diff
changeset
|
20 |
XXX clarify this header |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
21 |
""" |
0 | 22 |
# md5crypt.py |
23 |
# |
|
24 |
# 0423.2000 by michal wallace http://www.sabren.com/ |
|
25 |
# based on perl's Crypt::PasswdMD5 by Luis Munoz (lem@cantv.net) |
|
26 |
# based on /usr/src/libcrypt/crypt.c from FreeBSD 2.2.5-RELEASE |
|
27 |
# |
|
28 |
# MANY THANKS TO |
|
29 |
# |
|
30 |
# Carey Evans - http://home.clear.net.nz/pages/c.evans/ |
|
31 |
# Dennis Marti - http://users.starpower.net/marti1/ |
|
32 |
# |
|
33 |
# For the patches that got this thing working! |
|
34 |
# |
|
35 |
# modification by logilab: |
|
36 |
# * remove usage of the string module |
|
37 |
# * don't include the magic string in the output string |
|
38 |
# for true crypt.crypt compatibility |
|
39 |
######################################################### |
|
40 |
"""md5crypt.py - Provides interoperable MD5-based crypt() function |
|
41 |
||
42 |
SYNOPSIS |
|
43 |
||
44 |
import md5crypt.py |
|
45 |
||
46 |
cryptedpassword = md5crypt.md5crypt(password, salt); |
|
47 |
||
48 |
DESCRIPTION |
|
49 |
||
50 |
unix_md5_crypt() provides a crypt()-compatible interface to the |
|
51 |
rather new MD5-based crypt() function found in modern operating systems. |
|
52 |
It's based on the implementation found on FreeBSD 2.2.[56]-RELEASE and |
|
53 |
contains the following license in it: |
|
54 |
||
55 |
"THE BEER-WARE LICENSE" (Revision 42): |
|
56 |
<phk@login.dknet.dk> wrote this file. As long as you retain this notice you |
|
57 |
can do whatever you want with this stuff. If we meet some day, and you think |
|
58 |
this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
|
59 |
""" |
|
60 |
||
61 |
MAGIC = '$1$' # Magic string |
|
62 |
ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
|
63 |
||
64 |
import md5 |
|
65 |
||
66 |
def to64 (v, n): |
|
67 |
ret = '' |
|
68 |
while (n - 1 >= 0): |
|
69 |
n = n - 1 |
|
70 |
ret = ret + ITOA64[v & 0x3f] |
|
71 |
v = v >> 6 |
|
72 |
return ret |
|
73 |
||
74 |
def crypt(pw, salt, magic=None): |
|
3149
c6a85fafb155
note about licence, fix copyright, fix case of unicode argument
Aurelien Campeas
parents:
2172
diff
changeset
|
75 |
if isinstance(pw, unicode): |
c6a85fafb155
note about licence, fix copyright, fix case of unicode argument
Aurelien Campeas
parents:
2172
diff
changeset
|
76 |
pw = pw.encode('utf-8') |
1132 | 77 |
if magic is None: |
0 | 78 |
magic = MAGIC |
79 |
# Take care of the magic string if present |
|
80 |
if salt[:len(magic)] == magic: |
|
81 |
salt = salt[len(magic):] |
|
82 |
# salt can have up to 8 characters: |
|
83 |
salt = salt.split('$', 1)[0] |
|
84 |
salt = salt[:8] |
|
85 |
ctx = pw + magic + salt |
|
86 |
final = md5.md5(pw + salt + pw).digest() |
|
1132 | 87 |
for pl in xrange(len(pw), 0, -16): |
0 | 88 |
if pl > 16: |
89 |
ctx = ctx + final[:16] |
|
90 |
else: |
|
91 |
ctx = ctx + final[:pl] |
|
92 |
# Now the 'weird' xform (??) |
|
93 |
i = len(pw) |
|
94 |
while i: |
|
95 |
if i & 1: |
|
96 |
ctx = ctx + chr(0) #if ($i & 1) { $ctx->add(pack("C", 0)); } |
|
97 |
else: |
|
98 |
ctx = ctx + pw[0] |
|
99 |
i = i >> 1 |
|
100 |
final = md5.md5(ctx).digest() |
|
101 |
# The following is supposed to make |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
102 |
# things run slower. |
0 | 103 |
# my question: WTF??? |
1132 | 104 |
for i in xrange(1000): |
0 | 105 |
ctx1 = '' |
106 |
if i & 1: |
|
107 |
ctx1 = ctx1 + pw |
|
108 |
else: |
|
109 |
ctx1 = ctx1 + final[:16] |
|
110 |
if i % 3: |
|
111 |
ctx1 = ctx1 + salt |
|
112 |
if i % 7: |
|
113 |
ctx1 = ctx1 + pw |
|
114 |
if i & 1: |
|
115 |
ctx1 = ctx1 + final[:16] |
|
116 |
else: |
|
117 |
ctx1 = ctx1 + pw |
|
118 |
final = md5.md5(ctx1).digest() |
|
119 |
# Final xform |
|
120 |
passwd = '' |
|
121 |
passwd = passwd + to64((int(ord(final[0])) << 16) |
|
122 |
|(int(ord(final[6])) << 8) |
|
123 |
|(int(ord(final[12]))),4) |
|
124 |
passwd = passwd + to64((int(ord(final[1])) << 16) |
|
125 |
|(int(ord(final[7])) << 8) |
|
126 |
|(int(ord(final[13]))), 4) |
|
127 |
passwd = passwd + to64((int(ord(final[2])) << 16) |
|
128 |
|(int(ord(final[8])) << 8) |
|
129 |
|(int(ord(final[14]))), 4) |
|
130 |
passwd = passwd + to64((int(ord(final[3])) << 16) |
|
131 |
|(int(ord(final[9])) << 8) |
|
132 |
|(int(ord(final[15]))), 4) |
|
133 |
passwd = passwd + to64((int(ord(final[4])) << 16) |
|
134 |
|(int(ord(final[10])) << 8) |
|
135 |
|(int(ord(final[5]))), 4) |
|
136 |
passwd = passwd + to64((int(ord(final[11]))), 2) |
|
137 |
return salt + '$' + passwd |