AsymmetricPaddingTrait::getPaddingReservedSize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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 16
    public function setPaddingStandard($padding)
34
    {
35 16
        $validPadding = [
36 16
            self::PKCS1_PADDING,
37 16
            self::OAEP_PADDING,
38 16
        ];
39
40 16
        $padding = filter_var(
41 16
            $padding,
42 16
            FILTER_VALIDATE_INT,
43 16
            [
44 16
                "options" => [
45 16
                    "min_range" => 1,
46 16
                    "max_range" => PHP_INT_MAX,
47 16
                ],
48 16
            ]
49 16
        );
50
51 16
        if ($padding === false || !in_array($padding, $validPadding, true)) {
52 4
            throw new \InvalidArgumentException(
53 4
                'The digestion algorithm standard must be a valid integer bigger than 0.'
54 4
            );
55
        }
56
57 12
        $this->padding = $padding;
58
59 12
        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 12
    public function getPaddingStandard()
68
    {
69 12
        return $this->padding;
70
    }
71
72
    /**
73
     * Getter for the minimum size of the padding bytes that are required/reserved by the algorithm.
74
     *
75
     * @return int The minimum reserved size of the padding bytes.
76
     */
77 77
    public function getPaddingReservedSize()
78
    {
79
        /**
80
         * {@internal The reserved byte size for the PKCS1 standard is 11 and for the OAEP is 42. }}
81
         */
82 77
        return ($this->padding === OPENSSL_PKCS1_PADDING) ? 11 : 42;
83
    }
84
85
    /**
86
     * Enable long data processing via small chunks.
87
     *
88
     * @return $this The asymmetric encryption algorithm object.
89
     *
90
     * @note Using data chunks with asymmetric algorithms is discouraged.
91
     */
92 12
    public function enableChunkProcessing()
93
    {
94 12
        $this->useChunks = true;
95
96 12
        return $this;
97
    }
98
99
    /**
100
     * Disable long data processing via small chunks.
101
     *
102
     * @return $this The asymmetric encryption algorithm object.
103
     *
104
     * @note Using data chunks with asymmetric algorithms is discouraged.
105
     */
106 16
    public function disableChunkProcessing()
107
    {
108 16
        $this->useChunks = false;
109
110
111 16
        return $this;
112
    }
113
}
114