Skip to content

Commit ab95ee1

Browse files
committed
functions: phpbb_check_hash complete
1 parent 4a12316 commit ab95ee1

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

phpbb/functions.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,84 @@
2020
# along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
2121
#
2222

23+
from hashlib import md5
2324

25+
ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
26+
27+
def raw_md5(*args):
28+
m = md5()
29+
for i in args:
30+
m.update(i)
31+
return m.digest()
32+
33+
def hex_md5(*args):
34+
m = md5()
35+
for i in args:
36+
m.update(i)
37+
return m.hexdigest()
38+
39+
def phpbb_check_hash(password, password_hash):
40+
if len(password_hash) == 34:
41+
return hash_crypt_private(password, password_hash) == password_hash
42+
43+
return hex_md5(password) == password_hash
44+
45+
def hash_encode64(raw_hash, count, itoa64=ITOA64):
46+
output = ''
47+
i = 0
48+
49+
while True:
50+
value = ord(raw_hash[i])
51+
i += 1
52+
53+
output += itoa64[value & 0x3f]
54+
55+
if i < count:
56+
value |= ord(raw_hash[i]) << 8
57+
58+
output += itoa64[(value >> 6) & 0x3f]
59+
60+
i += 1
61+
if i >= count:
62+
break
63+
64+
if i < count:
65+
value |= ord(raw_hash[i]) << 16
66+
67+
output += itoa64[(value >> 12) & 0x3f]
68+
69+
i += 1
70+
if i >= count:
71+
break
72+
73+
output += itoa64[(value >> 18) & 0x3f]
74+
75+
if not i < count:
76+
break
77+
78+
return output
79+
80+
def hash_crypt_private(password, setting, itoa64=ITOA64):
81+
output = '*'
82+
83+
if setting[0:0+3] != '$H$':
84+
return output
85+
86+
count_log2 = itoa64.find(setting[3])
87+
if count_log2 < 7 or count_log2 > 30:
88+
return output
89+
90+
count = 1 << count_log2
91+
salt = setting[4:4+8]
92+
93+
if len(salt) != 8:
94+
return output
95+
96+
raw_hash = raw_md5(salt, password)
97+
for i in xrange(count):
98+
raw_hash = raw_md5(raw_hash, password)
99+
100+
output = setting[0:0+12]
101+
output += hash_encode64(raw_hash, 16, itoa64)
102+
103+
return output

0 commit comments

Comments
 (0)