Completed
Push — master ( 8055c8...3e2656 )
by Tony Karavasilev (Тони
16:45
created

SecretKeyTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 16
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSecretKey() 0 3 1
A setSecretKey() 0 18 4
1
<?php
2
3
/**
4
 * Trait implementation of the secret key capabilities for symmetric encryption algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageEncryption;
8
9
use \CryptoManana\Core\Interfaces\MessageEncryption\SecretKeyInterface as SecretKeySpecification;
10
11
/**
12
 * Trait SecretKeyTrait - Reusable implementation of `SecretKeyInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\SecretKeyInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageEncryption
17
 *
18
 * @property string $key The encryption/decryption secret key property storage.
19
 *
20
 * @mixin SecretKeySpecification
21
 */
22
trait SecretKeyTrait
23
{
24
    /**
25
     * Setter for the secret key string property.
26
     *
27
     * @param string $key The encryption key string.
28
     *
29
     * @return $this The encryption algorithm object.
30
     * @throws \Exception Validation errors.
31
     */
32
    public function setSecretKey($key)
33
    {
34
        if (!is_string($key)) {
1 ignored issue
show
introduced by
The condition is_string($key) is always true.
Loading history...
35
            throw new \InvalidArgumentException('The secret key must be a string or a binary string.');
36
        }
37
38
        /**
39
         * {@internal The encryption standard is 8-bit wise (don not use StringBuilder) and utilizes performance. }}
40
         */
41
        if (strlen($key) > static::KEY_SIZE) {
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...ecretKeyTrait::KEY_SIZE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42
            $key = hash_hkdf('sha256', $key, static::KEY_SIZE, 'CryptoMañana', '');
43
        } elseif (strlen($key) < static::KEY_SIZE) {
44
            $key = str_pad($key, static::KEY_SIZE, "\x0", STR_PAD_RIGHT);
45
        }
46
47
        $this->key = $key;
48
49
        return $this;
50
    }
51
52
    /**
53
     * Getter for the secret key string property.
54
     *
55
     * @return string The encryption key string.
56
     */
57
    public function getSecretKey()
58
    {
59
        return $this->key;
60
    }
61
}
62