Add 'Smf2' password algorithm
authorjoshuaruesweg <ruesweg@woltlab.com>
Wed, 30 Sep 2020 11:16:09 +0000 (13:16 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 30 Sep 2020 13:57:29 +0000 (15:57 +0200)
wcfsetup/install/files/lib/system/user/authentication/password/algorithm/Smf2.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/util/PasswordUtil.class.php

diff --git a/wcfsetup/install/files/lib/system/user/authentication/password/algorithm/Smf2.class.php b/wcfsetup/install/files/lib/system/user/authentication/password/algorithm/Smf2.class.php
new file mode 100644 (file)
index 0000000..89bdcf2
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+namespace wcf\system\user\authentication\password\algorithm;
+use wcf\system\user\authentication\password\IPasswordAlgorithm;
+
+/**
+ * Implementation of the password algorithm for Simple Machines Forums 2.x (smf2).
+ *
+ * @author     Joshua Ruesweg
+ * @copyright  2001-2020 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\User\Authentication\Password\Algorithm
+ * @since      5.4
+ */
+final class Smf2 implements IPasswordAlgorithm {
+       /**
+        * @inheritDoc
+        */
+       public function verify(string $password, string $hash): bool {
+               [$hash, $salt] = explode(':', $hash, 2);
+               
+               return \hash_equals($hash, $this->hashWithSalt($password, $salt));
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function hash(string $password): string {
+               $salt = \bin2hex(\random_bytes(20));
+               
+               return $this->hashWithSalt($password, $salt).':'.$salt;
+       }
+       
+       /**
+        * Returns the hashed password, hashed with a given salt.
+        */
+       private function hashWithSalt(string $password, string $salt): string {
+               return sha1($salt . $password);
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function needsRehash(string $hash): bool {
+               return false;
+       }
+}
index f0c6b4313b5c9754f67d91cd553f7e018a962410..30dfd209532abfa07a70db37190c09d3619e28a1 100644 (file)
@@ -368,13 +368,7 @@ final class PasswordUtil {
        }
        
        /**
-        * Validates the password hash for Simple Machines Forums 2.x (smf2).
-        * 
-        * @param       string          $username
-        * @param       string          $password
-        * @param       string          $salt
-        * @param       string          $dbHash
-        * @return      boolean
+        * @deprecated  5.4 - Use the new password algorithm framework in \wcf\system\user\authentication\password\*.
         */
        protected static function smf2($username, $password, $salt, $dbHash) {
                return self::smf1($username, $password, $salt, $dbHash);