3 namespace Org\Mxchange\CoreFramework\Helper\Crypto;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Crypto\Cryptable;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 * A helper class for cryptographical things like hashing passwords and so on
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 <<<<<<< HEAD:framework/main/classes/crypto/class_CryptoHelper.php
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
19 >>>>>>> Some updates::inc/main/classes/crypto/class_CryptoHelper.php
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 class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
37 // Exception constants
38 const EXCEPTION_ENCRYPT_MISSING = 0x1f0;
39 const EXCEPTION_ENCRYPT_INVALID = 0x1f1;
42 * An instance of this own clas
44 private static $selfInstance = NULL;
47 * Instance of the crypto stream
49 private $cryptoStreamInstance = NULL;
52 * Salt for hashing operations
57 * Protected constructor
61 protected function __construct () {
62 // Call parent constructor
63 parent::__construct(__CLASS__);
67 * Creates an instance of this class
69 * @return $cryptoInstance An instance of this crypto helper class
71 public static final function createCryptoHelper () {
73 $cryptoInstance = new CryptoHelper();
75 // Initialize the hasher
76 $cryptoInstance->initHasher();
78 // Attach a crypto stream
79 $cryptoInstance->attachCryptoStream();
81 // Return the instance
82 return $cryptoInstance;
86 * Get a singleton instance of this class
88 * @return $selfInstance An instance of this crypto helper class
90 public static final function getSelfInstance () {
91 // Is no instance there?
92 if (is_null(self::$selfInstance)) {
94 self::$selfInstance = self::createCryptoHelper();
97 // Return the instance
98 return self::$selfInstance;
102 * Attaches a crypto stream to this crypto helper by detecting loaded
107 protected function attachCryptoStream () {
108 // @TODO Maybe rewrite this with DirectoryIterator, similar to Compressor thing?
109 // Do we have openssl/mcrypt loaded?
110 if ($this->isPhpExtensionLoaded('mcrypt')) {
112 $this->cryptoStreamInstance = ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Stream\Crypto\McryptStream', array($this->getRngInstance()));
113 } elseif ($this->isPhpExtensionLoaded('openssl')) {
115 $this->cryptoStreamInstance = ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Stream\Crypto\OpenSslStream', array($this->getRngInstance()));
117 // If nothing works ...
118 $this->cryptoStreamInstance = ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Stream\Crypto\NullCryptoStream');
123 * Initializes the hasher for different purposes.
127 protected function initHasher () {
128 // Initialize the random number generator which is required by some crypto methods
129 $this->setRngInstance(ObjectFactory::createObjectByConfiguredName('rng_class'));
131 // Generate a salt for the hasher
132 $this->generateSalt();
136 * Generates the salt based on configured length
140 private function generateSalt () {
141 // Get a random string from the RNG
142 $randomString = $this->getRngInstance()->randomString() . $this->createUuid();
144 // Get config entry for salt length
145 $length = $this->getConfigInstance()->getConfigEntry('salt_length');
147 // Keep only defined number of characters
148 $this->salt = substr(sha1($randomString), -$length, $length);
152 * Returns a UUID (Universal Unique IDentifier) if PECL extension uuid was
153 * found or an empty string it not.
155 * @return $uuid UUID with leading dash or empty string
157 public function createUuid () {
161 // Is the UUID extension loaded and enabled? (see pecl)
162 if ($this->getConfigInstance()->getConfigEntry('extension_uuid_loaded') === true) {
163 // Then add it as well
164 $uuid = uuid_create();
172 * Hashes a string with salt and returns the hash. If an old previous hash
173 * is supplied the method will use the first X chars of that hash for hashing
174 * the password. This is useful if you want to check if password is identical
175 * for authorization purposes.
177 * @param $str Unhashed string
178 * @param $oldHash A hash from previous hashed string
179 * @param $withFixed Whether to include a fixed salt (not recommended in p2p applications)
180 * @return $hashed The hashed and salted string
182 public function hashString ($str, $oldHash = '', $withFixed = true) {
184 $str = (string) $str;
186 // Default is the default salt ;-)
189 // Is the old password set?
190 if (!empty($oldHash)) {
191 // Use the salt from hash, first get length
192 $length = $this->getConfigInstance()->getConfigEntry('salt_length');
194 // Then extract the X first characters from the hash as our salt
195 $salt = substr($oldHash, 0, $length);
198 // Hash the password with salt
199 //* DEBUG: */ echo "salt=".$salt."/plain=".$str."<br />\n";
200 if ($withFixed === true) {
201 // Use additional fixed salt
202 $hashed = $salt . md5(sprintf($this->getConfigInstance()->getConfigEntry('hash_extra_mask'),
204 $this->getRngInstance()->getFixedSalt(),
208 // Use salt+string to hash
209 $hashed = $salt . md5(sprintf($this->getConfigInstance()->getConfigEntry('hash_normal_mask'),
220 * Encrypt the string with fixed salt
222 * @param $str The unencrypted string
223 * @param $key Optional key, if none provided, a random key will be generated
224 * @return $encrypted Encrypted string
226 public function encryptString ($str, $key = NULL) {
227 // Encrypt the string through the stream
228 $encrypted = $this->cryptoStreamInstance->encryptStream($str, $key);
235 * Decrypt the string with fixed salt
237 * @param $encrypted Encrypted string
238 * @return $str The unencrypted string
240 public function decryptString ($encrypted) {
241 // Encrypt the string through the stream
242 $str = $this->cryptoStreamInstance->decryptStream($encrypted);