1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Crypt\Package; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Application\ContainerConfiguratorInterface; |
22
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
23
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
24
|
|
|
use Limoncello\Crypt\Contracts\DecryptInterface; |
25
|
|
|
use Limoncello\Crypt\Contracts\EncryptInterface; |
26
|
|
|
use Limoncello\Crypt\SymmetricCrypt; |
27
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
28
|
|
|
use Limoncello\Crypt\Package\SymmetricCryptSettings as C; |
29
|
|
|
use function array_key_exists; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package Limoncello\Crypt |
33
|
|
|
*/ |
34
|
|
|
class SymmetricCryptContainerConfigurator implements ContainerConfiguratorInterface |
35
|
|
|
{ |
36
|
|
|
/** @var callable */ |
37
|
|
|
const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
1 |
|
public static function configureContainer(LimoncelloContainerInterface $container): void |
43
|
|
|
{ |
44
|
1 |
|
$crypt = null; |
45
|
|
|
|
46
|
|
|
$instanceFactory = function (PsrContainerInterface $container) use (&$crypt) { |
47
|
1 |
|
if ($crypt === null) { |
48
|
1 |
|
$settings = $container->get(SettingsProviderInterface::class)->get(C::class); |
49
|
1 |
|
$crypt = new SymmetricCrypt($settings[C::KEY_METHOD], $settings[C::KEY_PASSWORD]); |
50
|
|
|
|
51
|
1 |
|
$vector = $settings[C::KEY_IV] ?? ''; |
52
|
1 |
|
empty($vector) === true ?: $crypt->setIV($vector); |
53
|
|
|
|
54
|
1 |
|
$usePadding = $settings[C::KEY_USE_ZERO_PADDING] ?? false; |
55
|
1 |
|
$usePadding === true ? $crypt->withZeroPadding() : $crypt->withoutZeroPadding(); |
56
|
|
|
|
57
|
1 |
|
$useAuthentication = $settings[C::KEY_USE_AUTHENTICATION] ?? false; |
58
|
1 |
|
if ($useAuthentication === true) { |
59
|
1 |
|
$crypt->enableAuthentication(); |
60
|
1 |
|
if (array_key_exists(C::KEY_TAG_LENGTH, $settings) === true) { |
61
|
1 |
|
$tagLength = $settings[C::KEY_TAG_LENGTH]; |
62
|
1 |
|
$crypt->setTagLength($tagLength); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return $crypt; |
68
|
1 |
|
}; |
69
|
|
|
|
70
|
1 |
|
$container[EncryptInterface::class] = $instanceFactory; |
71
|
1 |
|
$container[DecryptInterface::class] = $instanceFactory; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|