md5crypt.php ➔ md5crypt()   F
last analyzed

Complexity

Conditions 14
Paths 1224

Size

Total Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 1224
nop 3
dl 0
loc 95
rs 1.789
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// md5crypt
4
// Action: Creates MD5 encrypted password
5
// Call: md5crypt (string cleartextpassword)
6
7
function md5crypt($pw, $salt = "", $magic = "")
8
{
9
    $MAGIC = "$1$";
10
11
    if ($magic == "")
12
	{
13
		$magic = $MAGIC;
14
	}
15
16
    if ($salt == "")
17
	{
18
		$salt = create_salt();
19
	}
20
21
    $slist = explode("$", $salt);
22
    if (isset($slist[0]) && $slist[0] == "1")
23
	{
24
		$salt = $slist[1];
25
	}
26
27
    $salt = substr($salt, 0, 8);
28
    $ctx = $pw.$magic.$salt;
29
    $final = hex2bin(md5($pw.$salt.$pw));
30
31
    for ($i = strlen($pw); $i > 0; $i -= 16)
32
    {
33
        if ($i > 16)
34
        {
35
            $ctx .= substr($final,0,16);
36
        }
37
        else
38
        {
39
            $ctx .= substr($final,0,$i);
40
        }
41
    }
42
43
    $i = strlen($pw);
44
45
    while ($i > 0)
46
    {
47
        if ($i & 1)
48
		{
49
			$ctx .= chr(0);
50
		}
51
        else
52
		{
53
			$ctx .= $pw[0];
54
		}
55
		
56
        $i = $i >> 1;
57
    }
58
59
    $final = hex2bin(md5($ctx));
60
61
    for ($i=0; $i<1000; $i++)
62
    {
63
        $ctx1 = "";
64
        if ($i & 1)
65
        {
66
            $ctx1 .= $pw;
67
        }
68
        else
69
        {
70
            $ctx1 .= substr($final,0,16);
71
        }
72
        if ($i % 3)
73
		{
74
			$ctx1 .= $salt;
75
		}
76
        if ($i % 7)
77
		{
78
			$ctx1 .= $pw;
79
		}
80
        if ($i & 1)
81
        {
82
            $ctx1 .= substr($final, 0, 16);
83
        }
84
        else
85
        {
86
            $ctx1 .= $pw;
87
        }
88
89
        $final = hex2bin(md5($ctx1));
90
    }
91
92
    $passwd = "";
93
    $passwd .= to64(((ord($final[0]) << 16) | (ord($final[6]) << 8) | (ord($final[12]))), 4);
94
    $passwd .= to64(((ord($final[1]) << 16) | (ord($final[7]) << 8) | (ord($final[13]))), 4);
95
    $passwd .= to64(((ord($final[2]) << 16) | (ord($final[8]) << 8) | (ord($final[14]))), 4);
96
    $passwd .= to64(((ord($final[3]) << 16) | (ord($final[9]) << 8) | (ord($final[15]))), 4);
97
    $passwd .= to64(((ord($final[4]) << 16) | (ord($final[10]) << 8) | (ord($final[5]))), 4);
98
    $passwd .= to64(ord($final[11]), 2);
99
100
    return $magic.$salt.'$'.$passwd;
101
}
102
103
function create_salt()
104
{
105
    srand((double) microtime() * 1000000);
106
    return substr(md5(rand(0,9999999)), 0, 8);
107
}
108
109
// PHP around 5.3.8 includes hex2bin as native function - http://php.net/hex2bin
110
if (!function_exists('hex2bin'))
111
{
112
	function hex2bin($str)
113
	{
114
		$len = strlen($str);
115
		$nstr = "";
116
		for ($i = 0; $i < $len; $i += 2)
117
		{
118
			$num = sscanf(substr($str, $i, 2), "%x");
119
			$nstr .= chr($num[0]);
120
		}
121
122
		return $nstr;
123
	}
124
}
125
126
function to64($v, $n)
127
{
128
    $ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
129
    $ret = "";
130
131
    while (($n - 1) >= 0)
132
    {
133
        $n--;
134
        $ret .= $ITOA64[$v & 0x3f];
135
        $v = $v >> 6;
136
    }
137
138
    return $ret;
139
}