Completed
Push — master ( 604a9a...cf79ad )
by Tony Karavasilev (Тони
18:44
created

KeyPairSizeValidationTrait::validateKeyPairSize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.9332
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
 * Trait implementation of asymmetric key pair size in bits validation methods.
5
 */
6
7
namespace CryptoManana\Core\Traits\CommonValidations;
8
9
/**
10
 * Trait KeyPairSizeValidationTrait - Reusable implementation of asymmetric key pair size validations.
11
 *
12
 * @package CryptoManana\Core\Traits\CommonValidations
13
 */
14
trait KeyPairSizeValidationTrait
15
{
16
    /**
17
     * Internal method for asymmetric algorithm type validation.
18
     *
19
     * @param int $keySize The key size in bits.
20
     *
21
     * @throws \Exception Validation errors.
22
     */
23 24
    protected function validateKeyPairSize($keySize)
24
    {
25 24
        $keySize = filter_var(
26 24
            $keySize,
27 24
            FILTER_VALIDATE_INT,
28
            [
29
                "options" => [
30 24
                    "min_range" => 384,
31
                    "max_range" => 15360,
32
                ],
33
            ]
34
        );
35
36 24
        if ($keySize === false || $keySize % 128 !== 0) {
37 6
            throw new \InvalidArgumentException(
38
                'The key size must be between 384 (fastest but weakest) ' .
39 6
                'and 15360 (slowest but strongest) bits and be dividable by 128 bits.'
40
            );
41
        }
42 18
    }
43
}
44