3 namespace Org\Mxchange\CoreFramework\Utils\Crypto;
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;
11 use \InvalidArgumentException;
15 * Crypto utilities class
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
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.
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.
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/>.
36 final class CryptoUtils extends BaseFrameworkSystem {
38 * Length of output from hash()
40 private static $hashLength = NULL;
43 * Hash function, will be overwritten, so don't set it here!
45 private static $hashFunction = '';
48 * Private constructor, no instance needed. If PHP would have a static initializer ...
52 private function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
58 * Since PHP doesn't have static initializers, this method needs to be
59 * invoked by each public method here.
61 private static function staticInitializer () {
62 // Is $hashFunction set?
63 if (empty(self::$hashFunction)) {
65 $dummyInstance = new CryptoUtils();
67 // Set hash function from configuration
68 self::$hashFunction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('crypto_hash_function_name');
73 * Hashes a given string with a simple but stronger hash function (no salt)
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
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));
86 throw new InvalidArgumentException('Parameter "str" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
87 } elseif (!extension_loaded('hash')) {
89 throw new LogicException('Extension ext-hash not loaded', FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
92 // Invoke static initializer
93 self::staticInitializer();
95 // Hash given string with (better secure) hasher
96 $hash = hash(self::$hashFunction, $str, true);
99 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash{}=0x%s - EXIT!', bin2hex($hash)));
104 * "Getter" for length of hash() output. This will be "cached" to speed up
107 * @return $length Length of hash() output
109 public static final function getHashLength () {
110 // Invoke static initializer
111 self::staticInitializer();
114 if (is_null(self::$hashLength)) {
115 // No, then hash a string and save its length.
116 self::$hashLength = strlen(self::hash('abc123'));
120 return self::$hashLength;