Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.67% |
11 / 12 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Encryption | |
91.67% |
11 / 12 |
|
80.00% |
4 / 5 |
8.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getInstance | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| encrypt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| error | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| setSalt | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Provides methods for password encryption. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Lars Tiedemann <php@larstiedemann.de> |
| 12 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 13 | * @copyright 2005-2026 phpMyFAQ Team |
| 14 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 15 | * @link https://www.phpmyfaq.de |
| 16 | * @since 2005-09-18 |
| 17 | */ |
| 18 | |
| 19 | declare(strict_types=1); |
| 20 | |
| 21 | namespace phpMyFAQ; |
| 22 | |
| 23 | use SensitiveParameter; |
| 24 | |
| 25 | class Encryption |
| 26 | { |
| 27 | /** |
| 28 | * Error constant. |
| 29 | */ |
| 30 | private const string PMF_ERROR_USER_NO_ENCTYPE = 'EncryptionTypes method could not be found.'; |
| 31 | |
| 32 | /** |
| 33 | * Public array that contains error messages. |
| 34 | * |
| 35 | * @var string[] |
| 36 | */ |
| 37 | public array $errors = []; |
| 38 | |
| 39 | /** |
| 40 | * Salt. |
| 41 | */ |
| 42 | protected string $salt = ''; |
| 43 | |
| 44 | /** |
| 45 | * Constructor. |
| 46 | */ |
| 47 | private function __construct( |
| 48 | protected Configuration $configuration, |
| 49 | ) { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * This method is called statically. The parameter encType specifies the |
| 54 | * type of encryption method for the encryption object. Supported |
| 55 | * are 'bcrypt', 'hash', and 'none'. |
| 56 | * $enc = EncryptionTypes::getInstance('hash'); |
| 57 | * $enc is an instance of the class EncryptionTypes\Hash. |
| 58 | * If the given encryption-type is not supported, getInstance() will return an |
| 59 | * object without database access and with an error message. See the |
| 60 | * documentation of the error() method for further details. |
| 61 | */ |
| 62 | public static function getInstance(string $encType, Configuration $configuration): Encryption |
| 63 | { |
| 64 | $self = new self($configuration); |
| 65 | $encType = ucfirst(strtolower($encType)); |
| 66 | |
| 67 | $encClass = 'phpMyFAQ\\EncryptionTypes\\' . $encType; |
| 68 | if (!class_exists($encClass) || !is_subclass_of($encClass, self::class)) { |
| 69 | $self->errors[] = self::PMF_ERROR_USER_NO_ENCTYPE; |
| 70 | |
| 71 | return $self; |
| 72 | } |
| 73 | |
| 74 | return new $encClass($configuration); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Encrypts the given string and returns the result. |
| 79 | * |
| 80 | * @param string $password String |
| 81 | */ |
| 82 | public function encrypt(#[SensitiveParameter] string $password): string |
| 83 | { |
| 84 | return $password; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The string returned by error() contains messages for all errors that |
| 89 | * occurred during object processing. |
| 90 | * New lines separate messages. |
| 91 | */ |
| 92 | public function error(): string |
| 93 | { |
| 94 | return $this->errors !== [] ? implode(PHP_EOL, $this->errors) . PHP_EOL : ''; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Setter for salt. |
| 99 | */ |
| 100 | public function setSalt(string $login): Encryption |
| 101 | { |
| 102 | $this->salt = (string) $this->configuration->get(item: 'security.salt') . $login; |
| 103 | return $this; |
| 104 | } |
| 105 | } |