Completed
Push — master ( a44b29...b8fcd2 )
by Tony Karavasilev (Тони
16:57
created

AsymmetricPaddingTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
c 1
b 0
f 0
dl 0
loc 75
ccs 0
cts 37
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A disableChunkProcessing() 0 6 1
A getPaddingStandard() 0 3 1
A enableChunkProcessing() 0 5 1
A setPaddingStandard() 0 27 3
1
<?php
2
3
/**
4
 * Trait implementation of the data padding capabilities and actions for asymmetric encryption algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageEncryption;
8
9
use \CryptoManana\Core\Interfaces\MessageEncryption\AsymmetricPaddingInterface as AsymmetricPaddingSpecification;
10
11
/**
12
 * Trait AsymmetricPaddingTrait - Reusable implementation of `AsymmetricPaddingInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\AsymmetricPaddingInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageEncryption
17
 *
18
 * @property int $padding The asymmetric data padding operation property.
19
 * @property bool $useChunks Flag for enabling/disabling data processing via chunks.
20
 *
21
 * @mixin AsymmetricPaddingSpecification
22
 */
23
trait AsymmetricPaddingTrait
24
{
25
    /**
26
     * Setter for the asymmetric data padding operation property.
27
     *
28
     * @param int $padding The padding standard integer code value.
29
     *
30
     * @return $this The asymmetric encryption algorithm object.
31
     * @throws \Exception Validation errors.
32
     */
33
    public function setPaddingStandard($padding)
34
    {
35
        $validPadding = [
36
            self::PKCS1_PADDING,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...ingTrait::PKCS1_PADDING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
37
            self::OAEP_PADDING,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...dingTrait::OAEP_PADDING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
        ];
39
40
        $padding = filter_var(
41
            $padding,
42
            FILTER_VALIDATE_INT,
43
            [
44
                "options" => [
45
                    "min_range" => 1,
46
                    "max_range" => PHP_INT_MAX,
47
                ],
48
            ]
49
        );
50
51
        if ($padding === false || !in_array($padding, $validPadding, true)) {
52
            throw new \InvalidArgumentException(
53
                'The digestion algorithm standard must be a valid integer bigger than 0.'
54
            );
55
        }
56
57
        $this->padding = $padding;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Getter for the asymmetric data padding operation property.
64
     *
65
     * @return string The padding standard integer code value.
66
     */
67
    public function getPaddingStandard()
68
    {
69
        return $this->padding;
70
    }
71
72
    /**
73
     * Enable long data processing via small chunks.
74
     *
75
     * @return $this The asymmetric encryption algorithm object.
76
     *
77
     * @internal Using data chunks with asymmetric algorithms is discouraged.
78
     */
79
    public function enableChunkProcessing()
80
    {
81
        $this->useChunks = true;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Disable long data processing via small chunks.
88
     *
89
     * @return $this The asymmetric encryption algorithm object.
90
     * @internal Using data chunks with asymmetric algorithms is discouraged.
91
     */
92
    public function disableChunkProcessing()
93
    {
94
        $this->useChunks = false;
95
96
97
        return $this;
98
    }
99
}
100