3 namespace Org\Mxchange\CoreFramework\Utils\Crypto;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use \InvalidArgumentException;
14 * Crypto utilities class
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
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.
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.
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/>.
35 final class CryptoUtils extends BaseFrameworkSystem {
37 * Length of output from hash()
39 private static $hashLength = NULL;
42 * Hash function, will be overwritten, so don't set it here!
44 private static $hashFunction = '';
47 * Private constructor, no instance needed. If PHP would have a static initializer ...
51 private function __construct () {
52 // Call parent constructor
53 parent::__construct(__CLASS__);
57 * Since PHP doesn't have static initializers, this method needs to be
58 * invoked by each public method here.
60 private static function staticInitializer () {
61 // Is $hashFunction set?
62 if (empty(self::$hashFunction)) {
64 $dummyInstance = new CryptoUtils();
66 // Set hash function from configuration
67 self::$hashFunction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('crypto_hash_function_name');
72 * Hashes a given string with a simple but stronger hash function (no salt)
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
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));
85 throw new InvalidArgumentException('Parameter "str" is empty');
86 } elseif (!extension_loaded('hash')) {
88 throw new LogicException('Extension ext-hash not loaded');
91 // Invoke static initializer
92 self::staticInitializer();
94 // Hash given string with (better secure) hasher
95 $hash = hash(self::$hashFunction, $str, true);
98 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash{}=0x%s - EXIT!', bin2hex($hash)));
103 * "Getter" for length of hash() output. This will be "cached" to speed up
106 * @return $length Length of hash() output
108 public static final function getHashLength () {
109 // Invoke static initializer
110 self::staticInitializer();
113 if (is_null(self::$hashLength)) {
114 // No, then hash a string and save its length.
115 self::$hashLength = strlen(self::hash('abc123'));
119 return self::$hashLength;