-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEncryptionMd5.php
More file actions
87 lines (73 loc) · 2.11 KB
/
Copy pathEncryptionMd5.php
File metadata and controls
87 lines (73 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* Based on the encryption written by mikey_nich(at)hotmail.com
* @see: http://php.net/manual/de/function.crypt.php#73619
*/
class EncryptionMd5 implements EncryptionInterface {
/**
* @param string $plainPassword
* @param string $encryptedPassword
* @return bool|int
*/
public function validateEncryptedString($plainPassword, $encryptedPassword) {
$passwordParts = explode('$', $encryptedPassword);
if (count($passwordParts) !== 4)
return false;
list(, $key, $salt) = $passwordParts;
if ($key !== 'apr1')
return 0;
return $this->_encryptString($plainPassword, $salt) === $encryptedPassword;
}
/**
* @param string $plainPassword
* @return string
*/
public function encryptString($plainPassword) {
$this->_encryptString($plainPassword, $this->_generateSalt());
}
/**
* @return string
*/
private function _generateSalt() {
return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8);
}
/**
* @param string $plainPassword
* @param string $salt
* @return string
*/
private function _encryptString($plainPassword, $salt) {
$len = strlen($plainPassword);
$text = $plainPassword . '$apr1$' . $salt;
$bin = pack("H32", md5($plainPassword . $salt . $plainPassword));
for ($i = $len; $i > 0; $i -= 16) {
$text .= substr($bin, 0, min(16, $i));
}
for ($i = $len; $i > 0; $i >>= 1) {
$text .= ($i & 1) ? chr(0) : $plainPassword{0};
}
$bin = pack("H32", md5($text));
for ($i = 0; $i < 1000; $i++) {
$new = ($i & 1) ? $plainPassword : $bin;
if ($i % 3)
$new .= $salt;
if ($i % 7)
$new .= $plainPassword;
$new .= ($i & 1) ? $bin : $plainPassword;
$bin = pack("H32", md5($new));
}
$tmp = "";
for ($i = 0; $i < 5; $i++) {
$k = $i + 6;
$j = $i + 12;
if ($j == 16)
$j = 5;
$tmp = $bin[$i] . $bin[$k] . $bin[$j] . $tmp;
}
$tmp = chr(0) . chr(0) . $bin[11] . $tmp;
$tmp = strtr(strrev(substr(base64_encode($tmp), 2)),
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
return "$" . "apr1" . "$" . $salt . "$" . $tmp;
}
}