Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
weakpassword.php
1<?php
2
11
13
15{
16 public const MIN_PASSWORD_LENGTH = 6;
17
25 public static function createIndex(string $dataFile, string $path): bool
26 {
27 $file = new IO\File($dataFile);
28
29 $passwords = $file->getContents();
30
31 $passwords = str_replace(["\r\n", "\r"], "\n", $passwords);
32 $passwords = explode("\n", $passwords);
33
34 $hashedPasswords = [];
35 foreach ($passwords as $password)
36 {
37 if (strlen($password) >= self::MIN_PASSWORD_LENGTH)
38 {
39 $hash = md5($password);
40 $name = $hash[0] . $hash[1]; // 256 possible keys
41 $hashedPasswords[$name][] = (string)$password;
42 }
43 }
44 unset($passwords);
45
46 foreach ($hashedPasswords as $name => $value)
47 {
48 // we need the first and the last \n as a search pattern separator
49 $content = "\n" . implode("\n", $value) . "\n";
50
51 $indexFile = new IO\File($path . '/' . $name . '.txt');
52
53 if ($indexFile->putContents($content) === false)
54 {
55 return false;
56 }
57 }
58
59 return true;
60 }
61
69 public static function exists(string $password, string $path): bool
70 {
71 $hash = md5($password);
72 $name = $hash[0] . $hash[1];
73
74 $indexFile = new IO\File($path . '/' . $name . '.txt');
75
76 if (!$indexFile->isExists())
77 {
78 return false;
79 }
80
81 $passwords = $indexFile->getContents();
82
83 return (strpos($passwords, "\n" . $password . "\n") !== false);
84 }
85}
static exists(string $password, string $path)
static createIndex(string $dataFile, string $path)