|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Crypt; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2019 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Limoncello\Crypt\Exceptions\CryptException; |
|
22
|
|
|
use function openssl_error_string; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Limoncello\Crypt |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class BaseCrypt |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
12 |
|
protected function clearErrors(): void |
|
33
|
|
|
{ |
|
34
|
|
|
/** @noinspection PhpStatementHasEmptyBodyInspection */ |
|
35
|
12 |
|
while ($this->openSslErrorString() !== false) { |
|
36
|
|
|
// drop all accumulated error messages if any |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return null|string |
|
42
|
|
|
*/ |
|
43
|
4 |
|
protected function getErrorMessage(): ?string |
|
44
|
|
|
{ |
|
45
|
4 |
|
$message = null; |
|
46
|
4 |
|
while (($errorLine = $this->openSslErrorString()) !== false) { |
|
47
|
1 |
|
$message .= ($message === null ? $errorLine : PHP_EOL . $errorLine); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
return $message; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param CryptException $exception |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
4 |
|
protected function throwException(CryptException $exception): void |
|
59
|
|
|
{ |
|
60
|
4 |
|
throw $exception; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* We need this wrapper for testing purposes so we can mock system call to Open SSL. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string|false |
|
67
|
|
|
*/ |
|
68
|
11 |
|
protected function openSslErrorString() |
|
69
|
|
|
{ |
|
70
|
11 |
|
return openssl_error_string(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.