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