* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ final class CryptoUtils extends BaseFrameworkSystem { /** * Length of output from hash() */ private static $hashLength = NULL; /** * Hash function, will be overwritten, so don't set it here! */ private static $hashFunction = ''; /** * Private constructor, no instance needed. If PHP would have a static initializer ... * * @return void */ private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Since PHP doesn't have static initializers, this method needs to be * invoked by each public method here. */ private static function staticInitializer () { // Is $hashFunction set? if (empty(self::$hashFunction)) { // Get instance $dummyInstance = new CryptoUtils(); // Set hash function from configuration self::$hashFunction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('crypto_hash_function_name'); } } /** * Hashes a given string with a simple but stronger hash function (no salt) * and hex-encode it. * * @param $str The string to be hashed * @return $hash The hash from string $str * @throws InvalidArgumentException If a parameter is not valid * @throws LogicException If proper extension hash is not loaded */ public static final function hash (string $str) { // Validate parameter/mhash extension //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: str=%s - CALLED!', $str)); if (empty($str)) { // Throw IAE throw new InvalidArgumentException('Parameter "str" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (!extension_loaded('hash')) { // Should be there throw new LogicException('Extension ext-hash not loaded', FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION); } // Invoke static initializer self::staticInitializer(); // Hash given string with (better secure) hasher $hash = hash(self::$hashFunction, $str, true); // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CRYPTO-UTILS: hash{}=0x%s - EXIT!', bin2hex($hash))); return $hash; } /** * "Getter" for length of hash() output. This will be "cached" to speed up * things. * * @return $length Length of hash() output */ public static final function getHashLength () { // Invoke static initializer self::staticInitializer(); // Is it cashed? if (is_null(self::$hashLength)) { // No, then hash a string and save its length. self::$hashLength = strlen(self::hash('abc123')); } // Return it return self::$hashLength; } }