Passed
Push — master ( e9ba59...c2c890 )
by Tony Karavasilev (Тони
19:20
created

SymmetricKeyAuthentication   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 21
c 1
b 0
f 0
dl 0
loc 89
ccs 0
cts 17
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A __clone() 0 4 1
A __destruct() 0 3 1
1
<?php
2
3
/**
4
 * Cryptographic protocol for symmetric key authentication.
5
 */
6
7
namespace CryptoManana\CryptographicProtocol;
8
9
use \CryptoManana\Core\Abstractions\Containers\AbstractCryptographicProtocol as CryptographicProtocol;
10
use \CryptoManana\Core\Abstractions\Randomness\AbstractRandomness as RandomnessSource;
11
use \CryptoManana\Core\Abstractions\Randomness\AbstractGenerator as RandomnessGenerator;
12
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractBlockCipherAlgorithm as SymmetricBlockCipher;
13
use \CryptoManana\Core\Interfaces\MessageEncryption\DataEncryptionInterface as DataEncryption;
14
use \CryptoManana\Core\Interfaces\Containers\EntityIdentificationInterface as IdentifyEntities;
15
use \CryptoManana\Core\Interfaces\Containers\EntityAuthenticationInterface as AuthenticateEntities;
16
use \CryptoManana\Core\Interfaces\Containers\TokenTransformationInterface as GenerateAuthenticationTokens;
17
use \CryptoManana\Core\Interfaces\Containers\RandomnessInjectableInterface as RandomGeneratorSetter;
18
use \CryptoManana\Core\Interfaces\Containers\SymmetricEncryptionInjectableInterface as SymmetricCipherSetter;
19
use \CryptoManana\Core\Traits\Containers\RandomnessInjectableTrait as RandomGeneratorSetterImplementation;
20
use \CryptoManana\Core\Traits\Containers\SymmetricEncryptionInjectableTrait as SymmetricCipherSetterImplementation;
21
use \CryptoManana\Core\Traits\Containers\EntityIdentificationTrait as EntityIdentificationProcess;
22
use \CryptoManana\Core\Traits\Containers\EntityAuthenticationViaTokenTrait as EntityAuthenticationProcess;
23
use \CryptoManana\Core\Traits\Containers\TokenSymmetricTransformationTrait as AuthenticationTokenTransformation;
24
use \CryptoManana\Randomness\CryptoRandom as DefaultRandomnessSource;
25
26
/**
27
 * Class SymmetricKeyAuthentication - The symmetric key authentication protocol object.
28
 *
29
 * @package CryptoManana\CryptographicProtocol
30
 *
31
 * @mixin RandomGeneratorSetterImplementation
32
 * @mixin SymmetricCipherSetterImplementation
33
 * @mixin EntityIdentificationProcess
34
 * @mixin EntityAuthenticationProcess
35
 * @mixin AuthenticationTokenTransformation
36
 */
37
class SymmetricKeyAuthentication extends CryptographicProtocol implements
38
    RandomGeneratorSetter,
39
    SymmetricCipherSetter,
40
    IdentifyEntities,
41
    AuthenticateEntities,
42
    GenerateAuthenticationTokens
43
{
44
    /**
45
     * Dependency injection via a setter method implementation.
46
     *
47
     * {@internal Reusable implementation of `RandomnessInjectableInterface`. }}
48
     */
49
    use RandomGeneratorSetterImplementation;
50
51
    /**
52
     * The message symmetric encryption service dependency injection via a setter method implementation.
53
     *
54
     * {@internal Reusable implementation of `SymmetricEncryptionInjectableInterface`. }}
55
     */
56
    use SymmetricCipherSetterImplementation;
57
58
    /**
59
     * The entity identification capabilities.
60
     *
61
     * {@internal Reusable implementation of `EntityIdentificationInterface`. }}
62
     */
63
    use EntityIdentificationProcess;
64
65
    /**
66
     * The entity authentication capabilities.
67
     *
68
     * {@internal Reusable implementation of `EntityAuthenticationInterface`. }}
69
     */
70
    use EntityAuthenticationProcess;
71
72
    /**
73
     * The generation and transformation capabilities of authentication tokens.
74
     *
75
     * {@internal Reusable implementation of `TokenTransformationInterface`. }}
76
     */
77
    use AuthenticationTokenTransformation;
78
79
    /**
80
     * The pseudo-random generator service property storage.
81
     *
82
     * @var RandomnessSource|RandomnessGenerator|null The pseudo-random generator service.
83
     */
84
    protected $randomnessSource = null;
85
86
    /**
87
     * The message symmetric encryption algorithm service property storage.
88
     *
89
     * @var SymmetricBlockCipher|DataEncryption|null The message symmetric encryption service.
90
     */
91
    protected $symmetricCipherSource = null;
92
93
    /**
94
     * Container constructor.
95
     *
96
     * @param SymmetricBlockCipher $cipher The message encryption service.
97
     *
98
     * @throws \Exception Initialization validation.
99
     */
100
    public function __construct(SymmetricBlockCipher $cipher = null)
101
    {
102
        if ($cipher instanceof DataEncryption) {
103
            $this->symmetricCipherSource = $cipher;
104
        } else {
105
            throw new \RuntimeException('No symmetric encryption service has been set.');
106
        }
107
108
        $this->randomnessSource = new DefaultRandomnessSource();
109
    }
110
111
    /**
112
     * Container destructor.
113
     */
114
    public function __destruct()
115
    {
116
        unset($this->randomnessSource, $this->symmetricCipherSource);
117
    }
118
119
    /**
120
     * Container cloning via deep copy.
121
     */
122
    public function __clone()
123
    {
124
        $this->randomnessSource = clone $this->randomnessSource;
125
        $this->symmetricCipherSource = clone $this->symmetricCipherSource;
126
    }
127
}
128