Skip to content

Commit b72912e

Browse files
authored
Simplify md5crypt (hestiacp#2751)
* simplyfy md5crypt - magic argument was never used (idk what it was supposed to do either) - create_salt() was never defined, so the old implementation would crash if a salt wasn't given with the error "function create_salt not found" - fact is, md5 crypt() supports hasing with no salt at all! but the old md5crypt function did not support it, so to maintain api compatibility, i opted to not support it here either.. A long time ago, before PHP5.3, PHP's crypt() used the OS's native libc crypt(), and there was no guarantee that the OS-supplied libc crypt() actually had md5 support built in. But as of PHP5.3, PHP ships its own implementation of md5 crypt, so the original code was probably written with PHP<=5.2-compatibility in mind. That is no longer required. * remove now-unused function to64 it was used in the old version of md5crypt(), nowhere else, also today we have php's native base64_encode() function to do the same job :) (not sure when base64_encode was introduced, but i think it was around php4.0.0, i wonder if this function was written with PHP3 compatibility in mind?)
1 parent dcb3a85 commit b72912e

File tree

1 file changed

+17
-87
lines changed

1 file changed

+17
-87
lines changed

web/reset/mail/index.php

Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -23,97 +23,27 @@
2323
if ($ok==0) exit;
2424
if (isset($_SERVER['HTTP_X_REAL_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR'])) exit;
2525

26-
//
27-
// sourceforge.net/projects/postfixadmin/
28-
// md5crypt
29-
// Action: Creates MD5 encrypted password
30-
// Call: md5crypt (string cleartextpassword)
31-
//
3226

33-
function md5crypt ($pw, $salt="", $magic="")
27+
/**
28+
* md5 crypt() password
29+
*
30+
* @param string $password
31+
* @param string $salt
32+
*
33+
* @throws InvalidArgumentException if salt is emptystring
34+
* @throws InvalidArgumentException if salt is longer than 8 characters
35+
* @return string
36+
*/
37+
function md5crypt(string $pw, string $salt): string
3438
{
35-
$MAGIC = "$1$";
36-
37-
if ($magic == "") $magic = $MAGIC;
38-
if ($salt == "") $salt = create_salt ();
39-
$slist = explode ("$", $salt);
40-
if ($slist[0] == "1") $salt = $slist[1];
41-
42-
$salt = substr ($salt, 0, 8);
43-
$ctx = $pw . $magic . $salt;
44-
$final = hex2bin (md5 ($pw . $salt . $pw));
45-
46-
for ($i=strlen ($pw); $i>0; $i-=16)
47-
{
48-
if ($i > 16)
49-
{
50-
$ctx .= substr ($final,0,16);
51-
}
52-
else
53-
{
54-
$ctx .= substr ($final,0,$i);
55-
}
56-
}
57-
$i = strlen ($pw);
58-
59-
while ($i > 0)
60-
{
61-
if ($i & 1) $ctx .= chr (0);
62-
else $ctx .= $pw[0];
63-
$i = $i >> 1;
64-
}
65-
$final = hex2bin (md5 ($ctx));
66-
67-
for ($i=0;$i<1000;$i++)
68-
{
69-
$ctx1 = "";
70-
if ($i & 1)
71-
{
72-
$ctx1 .= $pw;
73-
}
74-
else
75-
{
76-
$ctx1 .= substr ($final,0,16);
77-
}
78-
if ($i % 3) $ctx1 .= $salt;
79-
if ($i % 7) $ctx1 .= $pw;
80-
if ($i & 1)
81-
{
82-
$ctx1 .= substr ($final,0,16);
83-
}
84-
else
85-
{
86-
$ctx1 .= $pw;
87-
}
88-
$final = hex2bin (md5 ($ctx1));
39+
if (strlen($salt) < 1) {
40+
// old implementation would crash with error "function generate_salt not defined", lets throw an exception instead
41+
throw new InvalidArgumentException('salt not given!');
8942
}
90-
$passwd = "";
91-
$passwd .= to64 (((ord ($final[0]) << 16) | (ord ($final[6]) << 8) | (ord ($final[12]))), 4);
92-
$passwd .= to64 (((ord ($final[1]) << 16) | (ord ($final[7]) << 8) | (ord ($final[13]))), 4);
93-
$passwd .= to64 (((ord ($final[2]) << 16) | (ord ($final[8]) << 8) | (ord ($final[14]))), 4);
94-
$passwd .= to64 (((ord ($final[3]) << 16) | (ord ($final[9]) << 8) | (ord ($final[15]))), 4);
95-
$passwd .= to64 (((ord ($final[4]) << 16) | (ord ($final[10]) << 8) | (ord ($final[5]))), 4);
96-
$passwd .= to64 (ord ($final[11]), 2);
97-
return "$magic$salt\$$passwd";
98-
}
99-
100-
101-
//
102-
// sourceforge.net/projects/postfixadmin/
103-
// to64
104-
//
105-
106-
function to64 ($v, $n)
107-
{
108-
$ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
109-
$ret = "";
110-
while (($n - 1) >= 0)
111-
{
112-
$n--;
113-
$ret .= $ITOA64[$v & 0x3f];
114-
$v = $v >> 6;
43+
if (strlen($salt) > 8) {
44+
throw new \InvalidArgumentException("maximum supported salt length for MD5 crypt is 8 characters!");
11545
}
116-
return $ret;
46+
return crypt($pw, '$1$' . $salt);
11747
}
11848

11949

0 commit comments

Comments
 (0)