-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash.php
More file actions
267 lines (207 loc) · 7.38 KB
/
hash.php
File metadata and controls
267 lines (207 loc) · 7.38 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
class generateHash {
/*
* Informacinės saugos pagrindai - Hashing
* Only for learning the Concepts of Hashing.
*/
public $study_book_no = '000000000';
private $pepper = array();
private $options;
public function __construct()
{
echo "From the command line and http(s)." . PHP_EOL;
/* Application specific hash security measure - the Pepper */
$this->pepper[] = array('first+128+letters+long+text+value+or+so+aaaaAaaaaA+aaaAaaaaAa+aaAaaaaAaa+aAaaaaAaaa+AaaaaAaaaa+aaaaAaaaaA+aaaAaaaaAa+aaAaaaaAaa+', '2021-01-01 00:00:00');
$this->pepper[] = array('second+128+letters+long+text+value+or+so+bbbBbbbbbB+bbbbBBbbbb+BbbbbbBbbb+bBbbbbbBbb+bbBbbbbbBb+bbbBbbbbbB+bbbbBBbbbb+BbbbbbBbbb', '2022-01-01 00:00:00');
$this->pepper[] = array('third+128+letters+long+text+value+or+so+ccCccccCcc+cccCccccCc+ccccCccccC+cCccccCccc+CccccCcccc+cccCccCccc+ccccCCcccc+CccccccccC+', '2023-01-01 00:00:00');
if((int)$this->study_book_no > 0)
{
$this->generateHash($this->study_book_no);
}
else
{
echo "Error: Invalid value of the variable study_book_no .";
echo $this->PHP_EOL();
echo "Tip. Edit the file and change the variable \$study_book_no .";
echo $this->PHP_EOL();
echo "For later: ";
echo $this->showOpenSSLMethods();
echo $this->PHP_EOL();
}
}
protected function generateHash($number = 0)
{
$i = 0;
$j = 0;
if(isset($this->study_book_no) and (int)$number > 0)
{
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a hash? */
print('Hint: empty; Hash: '. $number);
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a hash? */
print('Hint: base64; Hash: '. base64_encode($number));
if (function_exists('md5')) {
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a good hash? */
print('Hint: MD5; Hash: '. md5($number));
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a better hash? */
/* Let's make assumption that there are hardcoded repetitions of the same algorithm. */
print('Hint: MD5(MD5); Hash: '. md5(md5(md5($number))));
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is even better hash? */
/* Let's make assumption that there are hardcoded join of the password with the fixed value of the salt. */
print('Hint: MD5+Salt(1); Hash: '. md5('security'.$number));
}
if (function_exists('crypt')) {
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strong hash? */
/* Let's make assumption that some kind of library for hashing is in use, which accepts various (i.e. fixed) parameters. */
print('Hint: MD5+Salt(2); Hash: '. crypt($number, '$1$security$'));
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strong hash? */
/* Let's make assumption that some kind of library for hashing is in use, which accepts various (i.e. random) parameters. */
/* Does it is good idea to join the salt of bytes with the value of non-bytes? */
print('Hint: MD5+Salt(3); Hash: '. crypt($number, '$1$'.$this->basicSalt(12).'$'));
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strong hash? */
/* Let's make assumption that some kind of library for hashing is in use, which accepts various (i.e. random) parameters. */
/* Does it is good idea to join the salt of bytes with the value of non-bytes? */
/* Does by having pepper we would need to collect additional data for usability of the Hash? */
print('Hint: MD5+Salt(3)+Pepper; Hash: '. crypt($this->returnPepper().$number, '$1$'.$this->basicSalt(12).'$'));
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strong hash? */
/* Does by having pepper we would need to collect additional data for usability of the Hash? */
print('Hint: SHA1+Pepper; Hash: '. sha1($this->returnPepper().$number));
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strong hash */
/* Let's make assumption that some kind of library for hashing is in use with passed additional hashing controls. */
print('Hint: SHA256; Hash: '. crypt($this->returnPepper().$number,'$5$rounds=1000$additionalsalts.'));
}
if(defined("PASSWORD_DEFAULT")) {
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strongest hash */
print('Hint: BCRYPT; Hash: '. password_hash($number, PASSWORD_DEFAULT));
}
if(defined("PASSWORD_BCRYPT")) {
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strongest hash */
$this->options = array("cost" => 12,);
print('Hint: BCRYPT; Hash: '. password_hash($number, PASSWORD_BCRYPT, $this->options));
// Does password_hash has something with crypt_blowfish?
}
if(defined("PASSWORD_ARGON2I")) {
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strongest hash */
foreach ($this->options as $key => $value) {
unset($this->options[$key]);
}
$this->options = array("memory_cost" => 1<<10, "time_cost" => 1, "threads" => 1);
print('Hint: ARGON2; Hash: '. password_hash($number, PASSWORD_ARGON2I, $this->options));
// Does this hash algorithm is a winner of Password Hashing Competition?
$i = $i + 1;
echo $i .".".$this->PHP_EOL();
$j++;
/* Does it is a strongest hash */
print('Hint: ARGON2; Hash: '. password_hash($number, PASSWORD_ARGON2I));
// Does this hash algorithm is a winner of Password Hashing Competition?
}
echo "__.".$this->PHP_EOL();
echo "Generated " .$i." out of ". $j.".".$this->PHP_EOL();
echo $this->PHP_EOL();
}
}
protected function PHP_EOL()
{
print("\n");
}
protected function basicSalt($length = 32) {
if (function_exists('random_bytes')) {
return random_bytes($length);
}
if (function_exists('mcrypt_create_iv')) {
return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
}
if (function_exists('openssl_random_pseudo_bytes')) {
return openssl_random_pseudo_bytes($length);
}
$min = '1';
$max = '9';
for($i = 1; $i < $length; $i++) {
$min = $min.'0';
$max = $max.'9';
}
settype($min, "integer");
settype($max, "integer");
return rand($min, $max);
}
protected function returnPepper() {
$pepper = '';
$time_today = date("Y-m-d H:i:s");
$time_limit = date("Y-m-d H:i:s", strtotime("+1 years"));
foreach ($this->pepper as $key => $value) {
if ($value[1] > $time_today and $value[1] < $time_limit) {
$pepper .= $value[0];
}
}
return $pepper;
}
public function showOpenSSLMethods()
{
if(extension_loaded("openssl")) {
echo "OpenSSL Message Digest Algo:";
echo $this->PHP_EOL();
print_r(openssl_get_md_methods());
echo $this->PHP_EOL();
echo "OpenSSL Ciphers:";
echo $this->PHP_EOL();
print_r(openssl_get_cipher_methods());
}
elseif(extension_loaded("mcrypt")) {
echo "Mcrypt - available.";
//for encryption & decryption only
//i.e.
//$encrypted_data = mcrypt_ecb (MCRYPT_3DES, 'symmetric_cryptography_key', 'Plaintext message', MCRYPT_ENCRYPT);
//$encrypted_data = mcrypt_ecb (MCRYPT_3DES, 'symmetric_cryptography_key', 'Encrypted message', MCRYPT_DECRYPT);
//mcrypt_list_algorithms()
//mcrypt_list_modes()
echo $this->PHP_EOL();
}
else {
echo "OpenSSL - not available.";
echo $this->PHP_EOL();
}
}
public function __destruct()
{
unset($this->study_book_no);
}
}
$gH = new generateHash;