]> git.mxchange.org Git - core.git/blob - framework/main/classes/utils/crypto/class_CryptoUtils.php
Continued:
[core.git] / framework / main / classes / utils / crypto / class_CryptoUtils.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Utils\Crypto;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9
10 // Import SPL stuff
11 use \InvalidArgumentException;
12 use \LogicException;
13
14 /**
15  * Crypto utilities class
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 final class CryptoUtils extends BaseFrameworkSystem {
37         /**
38          * Length of output from hash()
39          */
40         private static $hashLength = NULL;
41
42         /**
43          * Hash function, will be overwritten, so don't set it here!
44          */
45         private static $hashFunction = '';
46
47         /**
48          * Private constructor, no instance needed. If PHP would have a static initializer ...
49          *
50          * @return      void
51          */
52         private function __construct () {
53                 // Call parent constructor
54                 parent::__construct(__CLASS__);
55         }
56
57         /**
58          * Since PHP doesn't have static initializers, this method needs to be
59          * invoked by each public method here.
60          */
61         private static function staticInitializer () {
62                 // Is $hashFunction set?
63                 if (empty(self::$hashFunction)) {
64                         // Get instance
65                         $dummyInstance = new CryptoUtils();
66
67                         // Set hash function from configuration
68                         self::$hashFunction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('crypto_hash_function_name');
69                 }
70         }
71
72         /**
73          * Hashes a given string with a simple but stronger hash function (no salt)
74          * and hex-encode it.
75          *
76          * @param       $str    The string to be hashed
77          * @return      $hash   The hash from string $str
78          * @throws      InvalidArgumentException        If a parameter is not valid
79          * @throws      LogicException  If proper extension hash is not loaded
80          */
81         public static final function hash (string $str) {
82                 // Validate parameter/mhash extension
83                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: str=%s - CALLED!', $str));
84                 if (empty($str)) {
85                         // Throw IAE
86                         throw new InvalidArgumentException('Parameter "str" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
87                 } elseif (!extension_loaded('hash')) {
88                         // Should be there
89                         throw new LogicException('Extension ext-hash not loaded', FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
90                 }
91
92                 // Invoke static initializer
93                 self::staticInitializer();
94
95                 // Hash given string with (better secure) hasher
96                 $hash = hash(self::$hashFunction, $str, true);
97
98                 // Return it
99                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash{}=0x%s - EXIT!', bin2hex($hash)));
100                 return $hash;
101         }
102
103         /**
104          * "Getter" for length of hash() output. This will be "cached" to speed up
105          * things.
106          *
107          * @return      $length         Length of hash() output
108          */
109         public static final function getHashLength () {
110                 // Invoke static initializer
111                 self::staticInitializer();
112
113                 // Is it cashed?
114                 if (is_null(self::$hashLength)) {
115                         // No, then hash a string and save its length.
116                         self::$hashLength = strlen(self::hash('abc123'));
117                 }
118
119                 // Return it
120                 return self::$hashLength;
121         }
122
123 }