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