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, this is free software
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 * Salt for hashing operations
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
53 // Set part description
54 $this->setObjectDescription("Cryptographical helper");
56 // Create unique ID number
57 $this->generateUniqueId();
60 $this->removeNumberFormaters();
61 $this->removeSystemArray();
65 * Creates an instance of this class
67 * @return $cryptoInstance An instance of this crypto helper class
69 public final static function createCryptoHelper () {
71 $cryptoInstance = new CryptoHelper();
73 // Initialize the hasher
74 $cryptoInstance->initHasher();
76 // Return the instance
77 return $cryptoInstance;
81 * Get a singleton instance of this class
83 * @return $selfInstance An instance of this crypto helper class
85 public final static function getInstance () {
86 // Is no instance there?
87 if (is_null(self::$selfInstance)) {
89 self::$selfInstance = self::createCryptoHelper();
92 // Return the instance
93 return self::$selfInstance;
97 * Initializes the hasher for different purposes.
101 protected function initHasher () {
102 // Initialize the random number generator which is required by some crypto methods
103 $this->rngInstance = ObjectFactory::createObjectByConfiguredName('rng_class');
105 // Generate a salt for the hasher
106 $this->generateSalt();
110 * Generates the salt based on configured length
114 private function generateSalt () {
115 // Get a random string from the RNG
116 $randomString = $this->rngInstance->randomString();
118 // Get config entry for salt length
119 $length = $this->getConfigInstance()->readConfig('salt_length');
121 // Keep only defined number of characters
122 $this->salt = substr(sha1($randomString), -$length, $length);
126 * Hashes a string with salt and returns the hash. If an old previous hash
127 * is supplied the method will use the first X chars of that hash for hashing
128 * the password. This is useful if you want to check if the password is
129 * identical for authorization purposes.
131 * @param $str Unhashed string
132 * @param $oldHash A hash from previous hashed string
133 * @return $hashed The hashed and salted string
135 public function hashString ($str, $oldHash = "") {
137 $str = (string) $str;
139 // Is the old password set?
140 if (empty($oldHash)) {
141 // No, then use the current salt
144 // Use the salt from hash, first get length
145 $length = $this->getConfigInstance()->readConfig('salt_length');
147 // Then extract the X first characters from the hash as our salt
148 $salt = substr($oldHash, 0, $length);
151 // Hash the password with salt
152 //* DEBUG: */ echo "salt=".$salt."/plain=".$str."<br />\n";
153 $hashed = $salt . md5(sprintf($this->getConfigInstance()->readConfig('hash_mask'),
155 $this->rngInstance->getFixedSalt(),
164 * Encrypt the string with fixed salt
166 * @param $str The unencrypted string
167 * @return $encrypted Encrypted string
169 public function encryptString ($str) {
170 // Init crypto module
171 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
172 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
175 if ($this->getConfigInstance()->readConfig('crypt_fixed_salt') == "Y") {
176 $key = md5($this->rngInstance->getFixedSalt());
178 $key = md5($this->rngInstance->getExtraSalt());
181 // Encrypt the string
182 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv);
189 * Decrypt the string with fixed salt
191 * @param $encrypted Encrypted string
192 * @return $str The unencrypted string
194 public function decryptString ($encrypted) {
195 // Init crypto module
196 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
197 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
200 if ($this->getConfigInstance()->readConfig('crypt_fixed_salt') == "Y") {
201 $key = md5($this->rngInstance->getFixedSalt());
203 $key = md5($this->rngInstance->getExtraSalt());
206 // Encrypt the string
207 $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
209 // Trim trailing nulls away
210 $str = rtrim($str, "\0");