Completed
Push — master ( b8fcd2...ffbcb5 )
by Tony Karavasilev (Тони
17:13
created

AsymmetricPaddingTrait::enableChunkProcessing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 32
    public function setPaddingStandard($padding)
34
    {
35
        $validPadding = [
36 32
            self::PKCS1_PADDING,
37 32
            self::OAEP_PADDING,
38
        ];
39
40 32
        $padding = filter_var(
41 32
            $padding,
42 32
            FILTER_VALIDATE_INT,
43
            [
44
                "options" => [
45 32
                    "min_range" => 1,
46 32
                    "max_range" => PHP_INT_MAX,
47
                ],
48
            ]
49
        );
50
51 32
        if ($padding === false || !in_array($padding, $validPadding, true)) {
52 8
            throw new \InvalidArgumentException(
53 8
                'The digestion algorithm standard must be a valid integer bigger than 0.'
54
            );
55
        }
56
57 24
        $this->padding = $padding;
58
59 24
        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 24
    public function getPaddingStandard()
68
    {
69 24
        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 24
    public function enableChunkProcessing()
80
    {
81 24
        $this->useChunks = true;
82
83 24
        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 32
    public function disableChunkProcessing()
93
    {
94 32
        $this->useChunks = false;
95
96
97 32
        return $this;
98
    }
99
}
100