3 * A helper class for cryptographical things like hashing passwords and so on
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
25 // Exception constants
26 const EXCEPTION_ENCRYPT_MISSING = 0x1f0;
27 const EXCEPTION_ENCRYPT_INVALID = 0x1f1;
30 * An instance of this own clas
32 private static $selfInstance = null;
35 * Instance of the random number generator
37 private $rngInstance = null;
40 * Instance of the crypto stream
42 private $cryptoStreamInstance = null;
45 * Salt for hashing operations
50 * Protected constructor
54 protected function __construct () {
55 // Call parent constructor
56 parent::__construct(__CLASS__);
60 * Creates an instance of this class
62 * @return $cryptoInstance An instance of this crypto helper class
64 public final static function createCryptoHelper () {
66 $cryptoInstance = new CryptoHelper();
68 // Initialize the hasher
69 $cryptoInstance->initHasher();
71 // Attach a crypto stream
72 $cryptoInstance->attachCryptoStream();
74 // Return the instance
75 return $cryptoInstance;
79 * Get a singleton instance of this class
81 * @return $selfInstance An instance of this crypto helper class
83 public final static function getInstance () {
84 // Is no instance there?
85 if (is_null(self::$selfInstance)) {
87 self::$selfInstance = self::createCryptoHelper();
90 // Return the instance
91 return self::$selfInstance;
95 * Attaches a crypto stream to this crypto helper by detecting loaded
100 protected function attachCryptoStream () {
101 // Do we have mcrypt loaded?
102 if ($this->isPhpExtensionLoaded('mcrypt')) {
104 $this->cryptoStreamInstance = ObjectFactory::createObjectByName('McryptStream', array($this->getRngInstance()));
106 // If nothing works ...
107 $this->cryptoStreamInstance = ObjectFactory::createObjectByName('NullCryptoStream');
112 * Initializes the hasher for different purposes.
116 protected function initHasher () {
117 // Initialize the random number generator which is required by some crypto methods
118 $this->setRngInstance(ObjectFactory::createObjectByConfiguredName('rng_class'));
120 // Generate a salt for the hasher
121 $this->generateSalt();
125 * Generates the salt based on configured length
129 private function generateSalt () {
130 // Get a random string from the RNG
131 $randomString = $this->getRngInstance()->randomString();
133 // Get config entry for salt length
134 $length = $this->getConfigInstance()->getConfigEntry('salt_length');
136 // Keep only defined number of characters
137 $this->salt = substr(sha1($randomString), -$length, $length);
141 * Hashes a string with salt and returns the hash. If an old previous hash
142 * is supplied the method will use the first X chars of that hash for hashing
143 * the password. This is useful if you want to check if password is identical
144 * for authorization purposes.
146 * @param $str Unhashed string
147 * @param $oldHash A hash from previous hashed string
148 * @return $hashed The hashed and salted string
150 public function hashString ($str, $oldHash = '') {
152 $str = (string) $str;
154 // Default is the default salt ;-)
157 // Is the old password set?
158 if (!empty($oldHash)) {
159 // Use the salt from hash, first get length
160 $length = $this->getConfigInstance()->getConfigEntry('salt_length');
162 // Then extract the X first characters from the hash as our salt
163 $salt = substr($oldHash, 0, $length);
166 // Hash the password with salt
167 //* DEBUG: */ echo "salt=".$salt."/plain=".$str."<br />\n";
168 $hashed = $salt . md5(sprintf($this->getConfigInstance()->getConfigEntry('hash_mask'),
170 $this->getRngInstance()->getFixedSalt(),
179 * Encrypt the string with fixed salt
181 * @param $str The unencrypted string
182 * @return $encrypted Encrypted string
184 public function encryptString ($str) {
185 // Encrypt the string through the stream
186 $encryted = $this->cryptoStreamInstance->encryptStream($str);
193 * Decrypt the string with fixed salt
195 * @param $encrypted Encrypted string
196 * @return $str The unencrypted string
198 public function decryptString ($encrypted) {
199 // Encrypt the string through the stream
200 $str = $this->cryptoStreamInstance->decryptStream($encrypted);