forked from defuse/crackstation-hashdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoreHashes.php
171 lines (147 loc) · 4.35 KB
/
MoreHashes.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
interface HashAlgorithm
{
public function hash($input, $raw);
public function getAlgorithmName();
}
class StandardHashAlgorithm implements HashAlgorithm
{
private $algorithm;
function __construct($algorithm)
{
if (!in_array($algorithm, hash_algos())) {
throw new Exception("Not a standard algorithm.");
}
$this->algorithm = $algorithm;
}
public function hash($input, $raw)
{
return hash($this->algorithm, $input, $raw);
}
public function getAlgorithmName()
{
return $this->algorithm;
}
}
class LMHashAlgorithm implements HashAlgorithm
{
public function hash($input, $raw)
{
$hash = $this->LMHash($input);
if (!$raw) {
$hash = bin2hex($hash);
}
return $hash;
}
private function LMhash($string)
{
$string = strtoupper(substr($string,0,14));
$string .= str_repeat("\x00", 14 - strlen($string));
$p1 = $this->LMhash_DESencrypt(substr($string, 0, 7));
$p2 = $this->LMhash_DESencrypt(substr($string, 7, 7));
return $p1.$p2;
}
private function LMhash_DESencrypt($string)
{
$key = "\x00\x00\x00\x00\x00\x00\x00\x00";
$key[0] = chr(ord($string[0]) & 254);
$key[1] = chr((ord($string[0]) << 7) | (ord($string[1]) >> 1));
$key[2] = chr((ord($string[1]) << 6) | (ord($string[2]) >> 2));
$key[3] = chr((ord($string[2]) << 5) | (ord($string[3]) >> 3));
$key[4] = chr((ord($string[3]) << 4) | (ord($string[4]) >> 4));
$key[5] = chr((ord($string[4]) << 3) | (ord($string[5]) >> 5));
$key[6] = chr((ord($string[5]) << 2) | (ord($string[6]) >> 6));
$key[7] = chr(ord($string[6]) << 1);
$crypt = openssl_encrypt("KGS!@#$%", "des-ecb", $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
return $crypt;
}
public function getAlgorithmName()
{
return "LM";
}
}
class NTLMHashAlgorithm implements HashAlgorithm
{
public function hash($input, $raw)
{
// Convert the password from UTF8 to UTF16 (little endian)
$input=@iconv('UTF-8','UTF-16LE',$input);
if ($input === false) {
return false;
}
$MD4Hash=hash('md4',$input, $raw);
return $MD4Hash;
}
public function getAlgorithmName()
{
return "NTLM";
}
}
class MD5MD5HexHashAlgorithm implements HashAlgorithm
{
public function hash($input, $raw)
{
return hash("md5", hash("md5", $input, false), $raw);
}
public function getAlgorithmName()
{
return "md5(md5)";
}
}
class MySQL41HashAlgorithm implements HashAlgorithm
{
public function hash($input, $raw)
{
return hash("sha1", hash("sha1", $input, true), $raw);
}
public function getAlgorithmName()
{
return "MySQL4.1+";
}
}
class QubesV31BackupDefaultsHashAlgorithm implements HashAlgorithm
{
public function hash($input, $raw)
{
$default_backup_header = "version=3\nhmac-algorithm=SHA512\ncrypto-algorithm=aes-256-cbc\nencrypted=True\ncompressed=False\n";
return hash_hmac("sha512", $default_backup_header, $input, $raw);
}
public function getAlgorithmName()
{
return "QubesV31DefaultBackup";
}
}
class MoreHashAlgorithms
{
public static function GetHashAlgoNames()
{
$extra_algos = array('LM', 'NTLM', 'md5(md5)', 'MySQL4.1+', 'QubesV3.1BackupDefaults');
return array_merge(hash_algos(), $extra_algos);
}
public static function GetHashFunction($algorithm)
{
if (in_array($algorithm, hash_algos())) {
return new StandardHashAlgorithm($algorithm);
} else {
switch ($algorithm) {
case "LM":
return new LMHashAlgorithm();
break;
case "NTLM":
return new NTLMHashAlgorithm();
break;
case "md5(md5)":
return new MD5MD5HexHashAlgorithm();
break;
case "MySQL4.1+":
return new MySQL41HashAlgorithm();
break;
case "QubesV3.1BackupDefaults":
return new QubesV31BackupDefaultsHashAlgorithm();
break;
default:
throw new Exception("Unknown algorithm name.");
}
}
}
}