Total Complexity | 5 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 | 48 | public function setSecretKey($key) |
|
33 | { |
||
34 | 48 | if (!is_string($key)) { |
|
35 | 12 | 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 | 36 | if (strlen($key) > static::KEY_SIZE) { |
|
42 | 12 | $key = hash_hkdf('sha256', $key, static::KEY_SIZE, 'CryptoMañana', ''); |
|
43 | 36 | } elseif (strlen($key) < static::KEY_SIZE) { |
|
44 | 12 | $key = str_pad($key, static::KEY_SIZE, "\x0", STR_PAD_RIGHT); |
|
45 | } |
||
46 | |||
47 | 36 | $this->key = $key; |
|
48 | |||
49 | 36 | return $this; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Getter for the secret key string property. |
||
54 | * |
||
55 | * @return string The encryption key string. |
||
56 | */ |
||
57 | 36 | public function getSecretKey() |
|
60 | } |
||
61 | } |
||
62 |