1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Factory object encryption algorithm object instancing. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Factories; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\DesignPatterns\AbstractFactory as FactoryPattern; |
10
|
|
|
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractSymmetricEncryptionAlgorithm as EncryptionAlgorithm; |
11
|
|
|
use \CryptoManana\SymmetricEncryption\Aes128 as Aes128; |
12
|
|
|
use \CryptoManana\SymmetricEncryption\Aes192 as Aes192; |
13
|
|
|
use \CryptoManana\SymmetricEncryption\Aes256 as Aes256; |
14
|
|
|
use \CryptoManana\SymmetricEncryption\Camellia128 as Camellia128; |
15
|
|
|
use \CryptoManana\SymmetricEncryption\Camellia192 as Camellia192; |
16
|
|
|
use \CryptoManana\SymmetricEncryption\Camellia256 as Camellia256; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class SymmetricCipherFactory - Factory object encryption algorithm object instancing. |
20
|
|
|
* |
21
|
|
|
* @package CryptoManana\Factories |
22
|
|
|
*/ |
23
|
|
|
class SymmetricCipherFactory extends FactoryPattern |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The AES-128 type. |
27
|
|
|
*/ |
28
|
|
|
const AES_128 = Aes128::class; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The AES-192 type. |
32
|
|
|
*/ |
33
|
|
|
const AES_192 = Aes192::class; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The AES-256 type. |
37
|
|
|
*/ |
38
|
|
|
const AES_256 = Aes256::class; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The CAMELLIA-128 type. |
42
|
|
|
*/ |
43
|
|
|
const CAMELLIA_128 = Camellia128::class; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The CAMELLIA-192 type. |
47
|
|
|
*/ |
48
|
|
|
const CAMELLIA_192 = Camellia192::class; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The CAMELLIA-256 type. |
52
|
|
|
*/ |
53
|
|
|
const CAMELLIA_256 = Camellia256::class; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create an encryption algorithm object |
57
|
|
|
* |
58
|
|
|
* @param string|null $type The algorithm class name as type for creation. |
59
|
|
|
* |
60
|
|
|
* @return EncryptionAlgorithm|object|null An encryption algorithm object or null. |
61
|
|
|
*/ |
62
|
|
|
public function create($type) |
63
|
|
|
{ |
64
|
|
|
return self::createInstance($type); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create an encryption algorithm object |
69
|
|
|
* |
70
|
|
|
* @param string|null $type The algorithm class name as type for creation. |
71
|
|
|
* |
72
|
|
|
* @return EncryptionAlgorithm|object|null An encryption algorithm object or null. |
73
|
|
|
*/ |
74
|
|
|
public static function createInstance($type) |
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* Check if class exists and has a correct base class |
78
|
|
|
* |
79
|
|
|
* @var EncryptionAlgorithm|null $exception Object instance. |
80
|
|
|
*/ |
81
|
|
|
if (class_exists($type) && is_subclass_of($type, EncryptionAlgorithm::class)) { |
82
|
|
|
$exception = new $type(); |
83
|
|
|
} else { |
84
|
|
|
$exception = null; // Invalid type given |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $exception; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get debug information for the class instance. |
92
|
|
|
* |
93
|
|
|
* @return array Debug information. |
94
|
|
|
*/ |
95
|
|
|
public function __debugInfo() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
self::class . '::AES_128' => self::AES_128, |
99
|
|
|
self::class . '::AES_192' => self::AES_192, |
100
|
|
|
self::class . '::AES_256' => self::AES_256, |
101
|
|
|
self::class . '::CAMELLIA_128' => self::CAMELLIA_128, |
102
|
|
|
self::class . '::CAMELLIA_192' => self::CAMELLIA_192, |
103
|
|
|
self::class . '::CAMELLIA_256' => self::CAMELLIA_256, |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|