Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.44% |
17 / 18 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
AppCryptography | |
94.44% |
17 / 18 |
|
83.33% |
5 / 6 |
7.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
encrypt | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
decrypt | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
encryptToken | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
decryptToken | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
toMark | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models; |
6 | |
7 | use PeServer\Core\Cryptography; |
8 | use PeServer\Core\Throws\CryptoException; |
9 | use PeServer\App\Models\AppConfiguration; |
10 | use PeServer\Core\Binary; |
11 | use PeServer\Core\Text; |
12 | |
13 | final class AppCryptography |
14 | { |
15 | public function __construct( |
16 | private AppConfiguration $config |
17 | ) { |
18 | } |
19 | |
20 | #region function |
21 | |
22 | /** |
23 | * アプリケーション設定から文字列暗号化。 |
24 | * |
25 | * @param string $data |
26 | * @return string DB格納値を想定。 |
27 | */ |
28 | public function encrypt(string $data): string |
29 | { |
30 | $crypto = $this->config->setting->crypto; |
31 | return Cryptography::encrypt($crypto->algorithm, $data, $crypto->password); |
32 | } |
33 | |
34 | /** |
35 | * アプリケーション設定から文字列復号化。 |
36 | * |
37 | * @param string $data DB格納値を想定。 |
38 | * @return string アプリ使用文字列を想定。 |
39 | */ |
40 | public function decrypt(string $data): string |
41 | { |
42 | $crypto = $this->config->setting->crypto; |
43 | return Cryptography::decrypt($data, $crypto->password); |
44 | } |
45 | |
46 | public function encryptToken(string $data): string |
47 | { |
48 | $token = $this->config->setting->crypto->token; |
49 | $value = Cryptography::encrypt($token->algorithm, $data, $token->password); |
50 | return Text::split($value, Cryptography::SEPARATOR, 2)[1]; |
51 | } |
52 | |
53 | public function decryptToken(string $data): string |
54 | { |
55 | $token = $this->config->setting->crypto->token; |
56 | $value = $data; |
57 | return Cryptography::decrypt($token->algorithm . Cryptography::SEPARATOR . $value, $token->password); |
58 | } |
59 | |
60 | /** |
61 | * 検索用マーカー整数への変換。 |
62 | * |
63 | * 一意性はないので暗号化データの絞り込みに使用する想定。 |
64 | * |
65 | * @param string $data |
66 | * @return int |
67 | */ |
68 | public function toMark(string $data): int |
69 | { |
70 | $crypto = $this->config->setting->crypto; |
71 | $input = $data . $crypto->pepper; |
72 | |
73 | $binary = Cryptography::generateHashBinary('fnv132', new Binary($input)); |
74 | |
75 | $result = unpack('N', $binary->raw, 0); |
76 | if ($result === false) { |
77 | throw new CryptoException(); |
78 | } |
79 | |
80 | return $result[1]; |
81 | } |
82 | |
83 | #endregion |
84 | } |