1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_auth\Mode; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_auth\AuthException; |
7
|
|
|
use kalanis\kw_auth\Interfaces\IKATranslations; |
8
|
|
|
use kalanis\kw_auth\Interfaces\IMode; |
9
|
|
|
use kalanis\kw_auth\TTranslate; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class KwOrig |
14
|
|
|
* @package kalanis\kw_auth\Mode |
15
|
|
|
* older kwcms style of password hashing |
16
|
|
|
*/ |
17
|
|
|
class KwOrig implements IMode |
18
|
|
|
{ |
19
|
|
|
use TTranslate; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $salt = ''; |
23
|
|
|
|
24
|
3 |
|
public function __construct(string $salt, ?IKATranslations $lang = null) |
25
|
|
|
{ |
26
|
3 |
|
$this->setLang($lang); |
27
|
3 |
|
$this->salt = $salt; |
28
|
3 |
|
} |
29
|
|
|
|
30
|
2 |
|
public function check(string $pass, string $hash): bool |
31
|
|
|
{ |
32
|
2 |
|
return $hash == $this->hashPassword($pass); |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function hash(string $pass, ?string $method = null): string |
36
|
|
|
{ |
37
|
2 |
|
return $this->hashPassword($pass); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $input |
42
|
|
|
* @throws AuthException |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
2 |
|
protected function hashPassword(string $input): string |
46
|
|
|
{ |
47
|
2 |
|
return base64_encode(bin2hex($this->makeHash($this->passSalt($input)))); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
private function passSalt(string $input): string |
51
|
|
|
{ |
52
|
2 |
|
$ln = strlen($input); |
53
|
|
|
# pass is too long and salt too short |
54
|
2 |
|
$salt = (strlen($this->salt) < ($ln*5)) |
55
|
2 |
|
? str_repeat($this->salt, 5) |
56
|
2 |
|
: $this->salt ; |
57
|
2 |
|
return substr($salt, $ln, $ln) |
58
|
2 |
|
. substr($input,0, (int) ($ln/2)) |
59
|
2 |
|
. substr($salt,$ln*2, $ln) |
60
|
2 |
|
. substr($input, (int) ($ln/2)) |
61
|
2 |
|
. substr($salt,$ln*3, $ln); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $word |
66
|
|
|
* @throws AuthException |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
2 |
|
private function makeHash(string $word): string |
70
|
|
|
{ |
71
|
2 |
|
if (function_exists('mhash')) { |
72
|
|
|
return (string) mhash(MHASH_SHA256, $word); |
73
|
|
|
} |
74
|
|
|
// @codeCoverageIgnoreStart |
75
|
2 |
|
if (function_exists('hash')) { |
76
|
2 |
|
return (string) hash('sha256', $word); |
77
|
|
|
} |
78
|
|
|
throw new AuthException($this->getLang()->kauHashFunctionNotFound()); |
79
|
|
|
// @codeCoverageIgnoreEnd |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|