]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/crypto/class_CryptoHelper.php
Support for cluster added to RNG, hashPassword() finished for better hashes
[shipsimu.git] / inc / classes / main / crypto / class_CryptoHelper.php
index 5652b89f07e3e48e240d369c3bb8aecba46b8b1a..d23645272f79cdb47b41cf32ea342fec6f15864e 100644 (file)
@@ -115,7 +115,41 @@ class CryptoHelper extends BaseFrameworkSystem {
                $length = $this->getConfigInstance()->readConfig('salt_length');
 
                // Keep only defined number of characters
-               $this->salt = substr($randomString, -$length, $length);
+               $this->salt = substr(sha1($randomString), -$length, $length);
+       }
+
+       /**
+        * Hashes a password with salt and returns the hash. If an old previous hash
+        * is supplied the method will use the first X chars of that hash for hashing
+        * the password. This is useful if you want to check if the password is
+        * identical for authorization purposes.
+        *
+        * @param       $plainPassword  The plain password to use
+        * @param       $oldHash                A previously hashed password
+        * @return      $hashed                 The hashed and salted password
+        */
+       public function hashPassword ($plainPassword, $oldHash = "") {
+               // Is the old password set?
+               if (empty($oldHash)) {
+                       // No, then use the current salt
+                       $salt = $this->salt;
+               } else {
+                       // Use the salt from hash, first get length
+                       $length = $this->getConfigInstance()->readConfig('salt_length');
+
+                       // Then extract the X first characters from the hash as our salt
+                       $salt = substr($oldHash, 0, $length);
+               }
+
+               // Hash the password with salt
+               $hashed = $salt . md5(sprintf($this->getConfigInstance()->readConfig('hash_mask'),
+                       $salt,
+                       $this->rngInstance->getExtraSalt(),
+                       $plainPassword
+               ));
+
+               // And return it
+               return $hashed;
        }
 }