'lib' rewritten.
This commit is contained in:
77
lib/Crypto/AbstractAlgorithm.php
Normal file
77
lib/Crypto/AbstractAlgorithm.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* The abstract password algorithm class.
|
||||
* Each algorithm should extend this class, as it provides very base
|
||||
* functionality which seems to be necessary for every implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
abstract class AbstractAlgorithm implements IPasswordAlgorithm
|
||||
{
|
||||
/**
|
||||
* @var IL10N The localization service.
|
||||
*/
|
||||
private $localization;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
$this->localization = $localization;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getVisibleName()
|
||||
{
|
||||
return $this->localization->t($this->getAlgorithmName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the algorithm name.
|
||||
*
|
||||
* @return string The algorithm name.
|
||||
*/
|
||||
protected abstract function getAlgorithmName();
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkPassword($password, $dbHash)
|
||||
{
|
||||
return hash_equals($dbHash, $this->getPasswordHash($password));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public abstract function getPasswordHash($password);
|
||||
}
|
||||
63
lib/Crypto/AbstractCrypt.php
Normal file
63
lib/Crypto/AbstractCrypt.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
/**
|
||||
* Abstract Unix Crypt hashing implementation.
|
||||
* The hashing algorithm depends on the chosen salt.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
abstract class AbstractCrypt extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The chars used in the salt.
|
||||
*/
|
||||
const SALT_ALPHABET = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkPassword($password, $dbHash)
|
||||
{
|
||||
return hash_equals($dbHash, crypt($password, $dbHash));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return crypt($password, self::getSalt());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a salt string for the hashing algorithm.
|
||||
*
|
||||
* @return string The salt string.
|
||||
*/
|
||||
protected function getSalt()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/Cleartext.php
Normal file
58
lib/Crypto/Cleartext.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Cleartext password implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class Cleartext extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return $password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Cleartext";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/CourierMD5.php
Normal file
58
lib/Crypto/CourierMD5.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Courier MD5 hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CourierMD5 extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return '{MD5}' . Utils::hexToBase64(md5($password));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Courier base64-encoded MD5";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/CourierMD5Raw.php
Normal file
58
lib/Crypto/CourierMD5Raw.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Courier MD5 RAW hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CourierMD5Raw extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return '{MD5RAW}' . md5($password);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Courier hexadecimal MD5";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/CourierSHA1.php
Normal file
58
lib/Crypto/CourierSHA1.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Courier SHA1 hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CourierSHA1 extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return '{SHA}' . Utils::hexToBase64(sha1($password));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Courier base64-encoded SHA1";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/CourierSHA256.php
Normal file
58
lib/Crypto/CourierSHA256.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Courier SHA256 hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CourierSHA256 extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return '{SHA256}' . Utils::hexToBase64(hash('sha256', $password));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Courier base64-encoded SHA256";
|
||||
}
|
||||
}
|
||||
59
lib/Crypto/Crypt.php
Normal file
59
lib/Crypto/Crypt.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Unix Crypt hashing implementation.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class Crypt extends AbstractCrypt
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Unix (Crypt)";
|
||||
}
|
||||
}
|
||||
97
lib/Crypto/CryptArgon2.php
Normal file
97
lib/Crypto/CryptArgon2.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Argon2 Crypt hashing implementation.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CryptArgon2 extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* @var int Maximum memory (in bytes) that may be used to compute.
|
||||
*/
|
||||
private $memoryCost;
|
||||
/**
|
||||
* @var int Maximum amount of time it may take to compute.
|
||||
*/
|
||||
private $timeCost;
|
||||
/**
|
||||
* @var int Number of threads to use for computing.
|
||||
*/
|
||||
private $threads;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
* @param int $memoryCost Maximum memory (in bytes) that may be used
|
||||
* to compute.
|
||||
* @param int $timeCost Maximum amount of time it may take to compute.
|
||||
* @param int $threads Number of threads to use for computing.
|
||||
*/
|
||||
public function __construct(
|
||||
IL10N $localization,
|
||||
$memoryCost = PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
|
||||
$timeCost = PASSWORD_ARGON2_DEFAULT_TIME_COST,
|
||||
$threads = PASSWORD_ARGON2_DEFAULT_THREADS
|
||||
) {
|
||||
parent::__construct($localization);
|
||||
$this->memoryCost = $memoryCost;
|
||||
$this->timeCost = $timeCost;
|
||||
$this->threads = $threads;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkPassword($password, $dbHash)
|
||||
{
|
||||
return password_verify($password, $dbHash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return password_hash(
|
||||
$password, PASSWORD_ARGON2I, [
|
||||
"memory_cost" => $this->memoryCost,
|
||||
"time_cost" => $this->timeCost,
|
||||
"threads" => $this->threads
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Argon2 (Crypt)";
|
||||
}
|
||||
}
|
||||
79
lib/Crypto/CryptBlowfish.php
Normal file
79
lib/Crypto/CryptBlowfish.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Blowfish Crypt hashing implementation.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CryptBlowfish extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* @var int Denotes the algorithmic cost that should be used.
|
||||
*/
|
||||
private $cost;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
* @param int $cost Denotes the algorithmic cost that should
|
||||
* be used.
|
||||
*/
|
||||
public function __construct(IL10N $localization, $cost = 10)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
$this->cost = $cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkPassword($password, $dbHash)
|
||||
{
|
||||
return password_verify($password, $dbHash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return password_hash(
|
||||
$password, PASSWORD_BCRYPT, ["cost" => $this->cost]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the algorithm name.
|
||||
*
|
||||
* @return string The algorithm name.
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Blowfish (Crypt)";
|
||||
}
|
||||
}
|
||||
92
lib/Crypto/CryptExtendedDES.php
Normal file
92
lib/Crypto/CryptExtendedDES.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Extended DES Crypt hashing implementation.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CryptExtendedDES extends AbstractCrypt
|
||||
{
|
||||
/**
|
||||
* @var int The number of iterations.
|
||||
*/
|
||||
private $iterationCount;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
* @param int $iterationCount The number of iterations.
|
||||
*/
|
||||
public function __construct(IL10N $localization, $iterationCount = 1000)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
$this->iterationCount = $iterationCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSalt()
|
||||
{
|
||||
return self::encodeIterationCount($this->iterationCount)
|
||||
. Utils::randomString(4, self::SALT_ALPHABET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of iterations as describe below.
|
||||
* The 4 bytes of iteration count are encoded as printable characters,
|
||||
* 6 bits per character, least significant character first.
|
||||
* The values 0 to 63 are encoded as "./0-9A-Za-z".
|
||||
*
|
||||
* @param int $number The number of iterations.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function encodeIterationCount($number)
|
||||
{
|
||||
$alphabet = str_split(self::SALT_ALPHABET);
|
||||
$chars = array();
|
||||
$base = sizeof($alphabet);
|
||||
|
||||
while ($number) {
|
||||
$rem = $number % $base;
|
||||
$number = (int)($number / $base);
|
||||
$arr[] = $alphabet[$rem];
|
||||
}
|
||||
|
||||
return str_pad(implode($chars), 4, ".", STR_PAD_RIGHT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Extended DES (Crypt)";
|
||||
}
|
||||
}
|
||||
59
lib/Crypto/CryptMD5.php
Normal file
59
lib/Crypto/CryptMD5.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* MD5 Crypt hashing implementation.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CryptMD5 extends AbstractCrypt
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSalt()
|
||||
{
|
||||
return "$1$" . Utils::randomString(8, self::SALT_ALPHABET) . "$";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "MD5 (Crypt)";
|
||||
}
|
||||
}
|
||||
69
lib/Crypto/CryptSHA256.php
Normal file
69
lib/Crypto/CryptSHA256.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* SHA256 Crypt hashing implementation.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CryptSHA256 extends AbstractCrypt
|
||||
{
|
||||
/**
|
||||
* @var int The number of rounds.
|
||||
*/
|
||||
private $rounds;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
* @param int $rounds The number of rounds.
|
||||
* This value must be between 1000 and 999999999.
|
||||
*/
|
||||
public function __construct(IL10N $localization, $rounds = 5000)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
$this->rounds = $rounds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSalt()
|
||||
{
|
||||
return "$5\$rounds=" . $this->rounds . "$" . Utils::randomString(
|
||||
16, self::SALT_ALPHABET
|
||||
) . "$";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "SHA256 (Crypt)";
|
||||
}
|
||||
}
|
||||
69
lib/Crypto/CryptSHA512.php
Normal file
69
lib/Crypto/CryptSHA512.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* SHA512 Crypt hashing implementation.
|
||||
*
|
||||
* @see crypt()
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CryptSHA512 extends AbstractCrypt
|
||||
{
|
||||
/**
|
||||
* @var int The number of rounds.
|
||||
*/
|
||||
private $rounds;
|
||||
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
* @param int $rounds The number of rounds.
|
||||
* This value must be between 1000 and 999999999.
|
||||
*/
|
||||
public function __construct(IL10N $localization, $rounds = 5000)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
$this->rounds = $rounds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSalt()
|
||||
{
|
||||
return "$6\$rounds=" . $this->rounds . "$" . Utils::randomString(
|
||||
16, self::SALT_ALPHABET
|
||||
) . "$";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "SHA512 (Crypt)";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/CryptStandardDES.php
Normal file
58
lib/Crypto/CryptStandardDES.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Standard DES Crypt hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class CryptStandardDES extends AbstractCrypt
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getSalt()
|
||||
{
|
||||
return Utils::randomString(2, self::SALT_ALPHABET);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Standard DES (Crypt)";
|
||||
}
|
||||
}
|
||||
59
lib/Crypto/IPasswordAlgorithm.php
Normal file
59
lib/Crypto/IPasswordAlgorithm.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
/**
|
||||
* Interface which defines all function required by a hash algorithm.
|
||||
* Please note that this interface must be implemented by every hash function supported in this app.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
interface IPasswordAlgorithm
|
||||
{
|
||||
/**
|
||||
* Get the hash algorithm name.
|
||||
* This name is visible in the admin panel.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVisibleName();
|
||||
|
||||
/**
|
||||
* Hash given password.
|
||||
* This value is stored in the database, when the password is changed.
|
||||
*
|
||||
* @param String $password The new password.
|
||||
*
|
||||
* @return boolean True if the password was hashed successfully, false otherwise.
|
||||
*/
|
||||
public function getPasswordHash($password);
|
||||
|
||||
/**
|
||||
* Check password given by the user against hash stored in the database.
|
||||
*
|
||||
* @param String $password Password given by the user.
|
||||
* @param String $dbHash Password hash stored in the database.
|
||||
*
|
||||
* @return boolean True if the password is correct, false otherwise.
|
||||
*/
|
||||
public function checkPassword($password, $dbHash);
|
||||
}
|
||||
83
lib/Crypto/Joomla.php
Normal file
83
lib/Crypto/Joomla.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* Joomla hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class Joomla extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return md5(
|
||||
$password . ":" . Utils::randomString(
|
||||
32,
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkPassword($password, $dbHash)
|
||||
{
|
||||
return hash_equals($dbHash, self::generateHash($password, $dbHash));
|
||||
}
|
||||
|
||||
private static function generateHash($password, $dbHash)
|
||||
{
|
||||
$split_salt = preg_split("/:/", $dbHash);
|
||||
$salt = false;
|
||||
if (isset($split_salt[1])) {
|
||||
$salt = $split_salt[1];
|
||||
}
|
||||
$pwHash = ($salt) ? md5($password . $salt) : md5($password);
|
||||
$pwHash .= ":" . $salt;
|
||||
return $pwHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "Joomla MD5 Encryption";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/MD5.php
Normal file
58
lib/Crypto/MD5.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* MD5 hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class MD5 extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return md5($password);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "MD5";
|
||||
}
|
||||
}
|
||||
58
lib/Crypto/SHA1.php
Normal file
58
lib/Crypto/SHA1.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* SHA1 hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class SHA1 extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return sha1($password);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "SHA1";
|
||||
}
|
||||
}
|
||||
98
lib/Crypto/SSHA.php
Normal file
98
lib/Crypto/SSHA.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* SSHA* hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
abstract class SSHA extends AbstractAlgorithm
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function checkPassword($password, $dbHash)
|
||||
{
|
||||
$saltedPassword = base64_decode(
|
||||
preg_replace("/" . $this->getPrefix() . "/i", "", $dbHash)
|
||||
);
|
||||
$salt = substr($saltedPassword, -(strlen($saltedPassword) - 32));
|
||||
$hash = self::ssha($password, $salt);
|
||||
|
||||
return hash_equals($dbHash, $hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hash prefix eg. {SSHA256}.
|
||||
*
|
||||
* @return string The hash prefix.
|
||||
*/
|
||||
public abstract function getPrefix();
|
||||
|
||||
/**
|
||||
* Encrypt using SSHA* algorithm.
|
||||
*
|
||||
* @param string $password The password.
|
||||
* @param string $salt The salt to use.
|
||||
*
|
||||
* @return string The hashed password, prefixed by {SSHA*}.
|
||||
*/
|
||||
private function ssha($password, $salt)
|
||||
{
|
||||
return $this->getPrefix() . base64_encode(
|
||||
hash($this->getAlgorithm(), $password . $salt, true) . $salt
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get algorithm used by the hash() function.
|
||||
*
|
||||
* @see hash()
|
||||
* @return string
|
||||
*/
|
||||
public abstract function getAlgorithm();
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPasswordHash($password)
|
||||
{
|
||||
return self::ssha(
|
||||
$password, Utils::randomString(
|
||||
32, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
66
lib/Crypto/SSHA256.php
Normal file
66
lib/Crypto/SSHA256.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* SSHA256 hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class SSHA256 extends SSHA
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPrefix()
|
||||
{
|
||||
return "{SSHA256}";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return "sha256";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "SSHA256";
|
||||
}
|
||||
}
|
||||
66
lib/Crypto/SSHA512.php
Normal file
66
lib/Crypto/SSHA512.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
use OCP\IL10N;
|
||||
|
||||
/**
|
||||
* SSHA512 hashing implementation.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class SSHA512 extends SSHA
|
||||
{
|
||||
/**
|
||||
* The class constructor.
|
||||
*
|
||||
* @param IL10N $localization The localization service.
|
||||
*/
|
||||
public function __construct(IL10N $localization)
|
||||
{
|
||||
parent::__construct($localization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getPrefix()
|
||||
{
|
||||
return "{SSHA512}";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return "sha512";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getAlgorithmName()
|
||||
{
|
||||
return "SSHA512";
|
||||
}
|
||||
}
|
||||
63
lib/Crypto/Utils.php
Normal file
63
lib/Crypto/Utils.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Crypto;
|
||||
|
||||
/**
|
||||
* Cryptographic utilities.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
final class Utils
|
||||
{
|
||||
/**
|
||||
* Convert hexadecimal message to its base64 form.
|
||||
*
|
||||
* @param $hex string The hexadecimal encoded message.
|
||||
*
|
||||
* @return string The same message encoded in base64.
|
||||
*/
|
||||
public static function hexToBase64($hex)
|
||||
{
|
||||
$hexChr = "";
|
||||
foreach (str_split($hex, 2) as $hexPair) {
|
||||
$hexChr .= chr(hexdec($hexPair));
|
||||
}
|
||||
return base64_encode($hexChr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate random string from given alphabet.
|
||||
*
|
||||
* @param $length int The output string length.
|
||||
* @param $alphabet string The output string alphabet.
|
||||
*
|
||||
* @return string Random string from given alphabet.
|
||||
*/
|
||||
public static function randomString($length, $alphabet)
|
||||
{
|
||||
$string = "";
|
||||
for ($idx = 0; $idx != $length; ++$idx) {
|
||||
$string .= $alphabet[mt_rand(0, strlen($alphabet) - 1)];
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user