From ed5ec8247957df5ced15dc5ff1fde2394dce1d11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20=C5=81ojewski?= Date: Fri, 2 Mar 2018 09:09:09 +0100 Subject: [PATCH] New hashing algorithms. --- lib/HashAlgorithm/Base/BaseCrypt.php | 60 +++++++++++++++ .../{ => Base}/HashAlgorithm.php | 4 +- lib/HashAlgorithm/Base/SSHA.php | 75 +++++++++++++++++++ lib/HashAlgorithm/Base/Singleton.php | 4 +- .../Base/{Base64.php => Utils.php} | 23 +++++- lib/HashAlgorithm/Cleartext.php | 9 ++- lib/HashAlgorithm/CourierMD5.php | 13 ++-- lib/HashAlgorithm/CourierMD5Raw.php | 9 ++- lib/HashAlgorithm/CourierSHA1.php | 13 ++-- lib/HashAlgorithm/CourierSHA256.php | 13 ++-- lib/HashAlgorithm/Crypt.php | 59 +++++++++++++++ lib/HashAlgorithm/CryptArgon2.php | 57 ++++++++++++++ lib/HashAlgorithm/CryptBlowfish.php | 57 ++++++++++++++ lib/HashAlgorithm/CryptExtendedDES.php | 63 ++++++++++++++++ lib/HashAlgorithm/CryptMD5.php | 48 ++++++++++++ lib/HashAlgorithm/CryptSHA256.php | 49 ++++++++++++ lib/HashAlgorithm/CryptSHA512.php | 49 ++++++++++++ lib/HashAlgorithm/CryptStandardDES.php | 48 ++++++++++++ lib/HashAlgorithm/Joomla.php | 71 ++++++++++++++++++ lib/HashAlgorithm/MD5.php | 9 ++- lib/HashAlgorithm/SHA1.php | 9 ++- lib/HashAlgorithm/SSHA256.php | 53 +++++++++++++ lib/HashAlgorithm/SSHA512.php | 53 +++++++++++++ 23 files changed, 806 insertions(+), 42 deletions(-) create mode 100644 lib/HashAlgorithm/Base/BaseCrypt.php rename lib/HashAlgorithm/{ => Base}/HashAlgorithm.php (94%) create mode 100644 lib/HashAlgorithm/Base/SSHA.php rename lib/HashAlgorithm/Base/{Base64.php => Utils.php} (65%) create mode 100644 lib/HashAlgorithm/Crypt.php create mode 100644 lib/HashAlgorithm/CryptArgon2.php create mode 100644 lib/HashAlgorithm/CryptBlowfish.php create mode 100644 lib/HashAlgorithm/CryptExtendedDES.php create mode 100644 lib/HashAlgorithm/CryptMD5.php create mode 100644 lib/HashAlgorithm/CryptSHA256.php create mode 100644 lib/HashAlgorithm/CryptSHA512.php create mode 100644 lib/HashAlgorithm/CryptStandardDES.php create mode 100644 lib/HashAlgorithm/Joomla.php create mode 100644 lib/HashAlgorithm/SSHA256.php create mode 100644 lib/HashAlgorithm/SSHA512.php diff --git a/lib/HashAlgorithm/Base/BaseCrypt.php b/lib/HashAlgorithm/Base/BaseCrypt.php new file mode 100644 index 0000000..620cbea --- /dev/null +++ b/lib/HashAlgorithm/Base/BaseCrypt.php @@ -0,0 +1,60 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm\Base; + +/** + * Implements standard Unix DES-based algorithm or + * alternative algorithms that may be available on the system. + * @see crypt() + * @author Marcin Łojewski + */ +abstract class BaseCrypt implements HashAlgorithm +{ + use Singleton; + + const SALT_ALPHABET = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + /** + * @inheritdoc + */ + abstract public function getVisibleName(); + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return hash_equals($dbHash, crypt($password, $dbHash)); + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + return crypt($password, self::getSalt()); + } + + /** + * Generate salt for hashing algorithm. + * @return string + */ + protected abstract function getSalt(); +} diff --git a/lib/HashAlgorithm/HashAlgorithm.php b/lib/HashAlgorithm/Base/HashAlgorithm.php similarity index 94% rename from lib/HashAlgorithm/HashAlgorithm.php rename to lib/HashAlgorithm/Base/HashAlgorithm.php index 5c074ef..0f378a4 100644 --- a/lib/HashAlgorithm/HashAlgorithm.php +++ b/lib/HashAlgorithm/Base/HashAlgorithm.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm\Base; /** * Interface which defines all function required by a hash algorithm. diff --git a/lib/HashAlgorithm/Base/SSHA.php b/lib/HashAlgorithm/Base/SSHA.php new file mode 100644 index 0000000..baac624 --- /dev/null +++ b/lib/HashAlgorithm/Base/SSHA.php @@ -0,0 +1,75 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm\Base; + +/** + * SSHA* hashing implementation. + * @author Marcin Łojewski + */ +abstract class SSHA implements HashAlgorithm +{ + use Singleton; + use Utils; + + /** + * @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 + */ + public abstract function getPrefix(); + + /** + * Encrypt using SSHA256 algorithm + * @param string $password The password. + * @param string $salt The salt to use. + * @return string The hashed password, prefixed by {SSHA256}. + */ + 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, + self::randomString(32, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); + } +} diff --git a/lib/HashAlgorithm/Base/Singleton.php b/lib/HashAlgorithm/Base/Singleton.php index 39dadc4..3dc4440 100644 --- a/lib/HashAlgorithm/Base/Singleton.php +++ b/lib/HashAlgorithm/Base/Singleton.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm\Base; +namespace OCA\UserSQL\HashAlgorithm\Base; /** * Singleton pattern trait. diff --git a/lib/HashAlgorithm/Base/Base64.php b/lib/HashAlgorithm/Base/Utils.php similarity index 65% rename from lib/HashAlgorithm/Base/Base64.php rename to lib/HashAlgorithm/Base/Utils.php index ca9fb05..956c3b6 100644 --- a/lib/HashAlgorithm/Base/Base64.php +++ b/lib/HashAlgorithm/Base/Utils.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,13 +17,13 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm\Base; +namespace OCA\UserSQL\HashAlgorithm\Base; /** - * Base64 utilities trait. + * Cryptographic utilities trait. * @author Marcin Łojewski */ -trait Base64 +trait Utils { /** * Convert hexadecimal message to its base64 form. @@ -38,4 +38,19 @@ trait Base64 } return base64_encode($hexChr); } + + /** + * Generate random string from given alphabet. + * @param $length int Output string length. + * @param $alphabet string Output string alphabet. + * @return string Random string from given alphabet. + */ + private static function randomString($length, $alphabet) + { + $string = ""; + for ($i = 0; $i != $length; ++$i) { + $string .= $alphabet[mt_rand(0, strlen($alphabet) - 1)]; + } + return $string; + } } diff --git a/lib/HashAlgorithm/Cleartext.php b/lib/HashAlgorithm/Cleartext.php index e5a60bd..c15b11b 100644 --- a/lib/HashAlgorithm/Cleartext.php +++ b/lib/HashAlgorithm/Cleartext.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,12 +17,13 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm; -use OCA\user_sql\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; /** - * Cleartext password hash implementation. + * Cleartext password implementation. * @author Marcin Łojewski */ class Cleartext implements HashAlgorithm diff --git a/lib/HashAlgorithm/CourierMD5.php b/lib/HashAlgorithm/CourierMD5.php index 25476eb..00cd67a 100644 --- a/lib/HashAlgorithm/CourierMD5.php +++ b/lib/HashAlgorithm/CourierMD5.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,19 +17,20 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm; -use OCA\user_sql\HashAlgorithm\Base\Base64; -use OCA\user_sql\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\Utils; /** - * Courier MD5 password hash implementation. + * Courier MD5 hashing implementation. * @author Marcin Łojewski */ class CourierMD5 implements HashAlgorithm { - use Base64; use Singleton; + use Utils; /** * @inheritdoc diff --git a/lib/HashAlgorithm/CourierMD5Raw.php b/lib/HashAlgorithm/CourierMD5Raw.php index dbdfc04..d0cc952 100644 --- a/lib/HashAlgorithm/CourierMD5Raw.php +++ b/lib/HashAlgorithm/CourierMD5Raw.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,12 +17,13 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm; -use OCA\user_sql\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; /** - * Courier MD5 RAW password hash implementation. + * Courier MD5 RAW hashing implementation. * @author Marcin Łojewski */ class CourierMD5Raw implements HashAlgorithm diff --git a/lib/HashAlgorithm/CourierSHA1.php b/lib/HashAlgorithm/CourierSHA1.php index 21f3a44..5c2a82b 100644 --- a/lib/HashAlgorithm/CourierSHA1.php +++ b/lib/HashAlgorithm/CourierSHA1.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,19 +17,20 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm; -use OCA\user_sql\HashAlgorithm\Base\Base64; -use OCA\user_sql\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\Utils; /** - * Courier SHA1 password hash implementation. + * Courier SHA1 hashing implementation. * @author Marcin Łojewski */ class CourierSHA1 implements HashAlgorithm { - use Base64; use Singleton; + use Utils; /** * @inheritdoc diff --git a/lib/HashAlgorithm/CourierSHA256.php b/lib/HashAlgorithm/CourierSHA256.php index 0951d59..f275bbe 100644 --- a/lib/HashAlgorithm/CourierSHA256.php +++ b/lib/HashAlgorithm/CourierSHA256.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,19 +17,20 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm; -use OCA\user_sql\HashAlgorithm\Base\Base64; -use OCA\user_sql\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\Utils; /** - * Courier SHA256 password hash implementation. + * Courier SHA256 hashing implementation. * @author Marcin Łojewski */ class CourierSHA256 implements HashAlgorithm { - use Base64; use Singleton; + use Utils; /** * @inheritdoc diff --git a/lib/HashAlgorithm/Crypt.php b/lib/HashAlgorithm/Crypt.php new file mode 100644 index 0000000..348dbf2 --- /dev/null +++ b/lib/HashAlgorithm/Crypt.php @@ -0,0 +1,59 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; + +/** + * Implements standard Unix DES-based algorithm or + * alternative algorithms that may be available on the system. + * This implementation does not support password changing. + * @see crypt() + * @author Marcin Łojewski + */ +class Crypt implements HashAlgorithm +{ + use Singleton; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Crypt (Unix)"; + } + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return hash_equals($dbHash, crypt($password, $dbHash)); + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + return password_hash($password, PASSWORD_DEFAULT); + } +} diff --git a/lib/HashAlgorithm/CryptArgon2.php b/lib/HashAlgorithm/CryptArgon2.php new file mode 100644 index 0000000..d628723 --- /dev/null +++ b/lib/HashAlgorithm/CryptArgon2.php @@ -0,0 +1,57 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; + +/** + * Argon2 Crypt hashing implementation. + * @author Marcin Łojewski + */ +class CryptArgon2 implements HashAlgorithm +{ + use Singleton; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Argon2 (Crypt)"; + } + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return password_verify($password, $dbHash); + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + // TODO - add support for options: memory_cost, time_cost, threads. + return password_hash($password, PASSWORD_ARGON2I); + } +} diff --git a/lib/HashAlgorithm/CryptBlowfish.php b/lib/HashAlgorithm/CryptBlowfish.php new file mode 100644 index 0000000..2a2f1c0 --- /dev/null +++ b/lib/HashAlgorithm/CryptBlowfish.php @@ -0,0 +1,57 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; + +/** + * Blowfish Crypt hashing implementation. + * @author Marcin Łojewski + */ +class CryptBlowfish implements HashAlgorithm +{ + use Singleton; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Blowfish (Crypt)"; + } + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return password_verify($password, $dbHash); + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + // TODO - add support for options: cost. + return password_hash($password, PASSWORD_BCRYPT); + } +} diff --git a/lib/HashAlgorithm/CryptExtendedDES.php b/lib/HashAlgorithm/CryptExtendedDES.php new file mode 100644 index 0000000..c49563f --- /dev/null +++ b/lib/HashAlgorithm/CryptExtendedDES.php @@ -0,0 +1,63 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\BaseCrypt; +use OCA\UserSQL\HashAlgorithm\Base\Utils; + +/** + * Extended DES Crypt hashing implementation. + * @author Marcin Łojewski + */ +class CryptExtendedDES extends BaseCrypt +{ + use Utils; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Extended DES (Crypt)"; + } + + /** + * @inheritdoc + */ + protected function getSalt() + { + // TODO - add support for options: iteration_count. + return self::base64IntEncode(1000) . self::randomString(4, self::SALT_ALPHABET); + } + + private static function base64IntEncode($number) + { + $alphabet = str_split(self::SALT_ALPHABET); + $chars = array(); + $base = sizeof($alphabet); + while ($number) { + $rem = $number % $base; + $number = (int)($number / $base); + $arr[] = $alphabet[$rem]; + } + $string = implode($chars); + return str_pad($string, 4, '.', STR_PAD_RIGHT); + } +} diff --git a/lib/HashAlgorithm/CryptMD5.php b/lib/HashAlgorithm/CryptMD5.php new file mode 100644 index 0000000..2cc096e --- /dev/null +++ b/lib/HashAlgorithm/CryptMD5.php @@ -0,0 +1,48 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\BaseCrypt; +use OCA\UserSQL\HashAlgorithm\Base\Utils; + +/** + * MD5 Crypt hashing implementation. + * @author Marcin Łojewski + */ +class CryptMD5 extends BaseCrypt +{ + use Utils; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "MD5 (Crypt)"; + } + + /** + * @inheritdoc + */ + protected function getSalt() + { + return "$1$" . self::randomString(8, self::SALT_ALPHABET) . "$"; + } +} diff --git a/lib/HashAlgorithm/CryptSHA256.php b/lib/HashAlgorithm/CryptSHA256.php new file mode 100644 index 0000000..ca466f9 --- /dev/null +++ b/lib/HashAlgorithm/CryptSHA256.php @@ -0,0 +1,49 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\BaseCrypt; +use OCA\UserSQL\HashAlgorithm\Base\Utils; + +/** + * SHA256 Crypt hashing implementation. + * @author Marcin Łojewski + */ +class CryptSHA256 extends BaseCrypt +{ + use Utils; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "SHA256 (Crypt)"; + } + + /** + * @inheritdoc + */ + protected function getSalt() + { + // TODO - add support for options: rounds. + return "$5\$rounds=5000$" . self::randomString(16, self::SALT_ALPHABET) . "$"; + } +} diff --git a/lib/HashAlgorithm/CryptSHA512.php b/lib/HashAlgorithm/CryptSHA512.php new file mode 100644 index 0000000..f117606 --- /dev/null +++ b/lib/HashAlgorithm/CryptSHA512.php @@ -0,0 +1,49 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\BaseCrypt; +use OCA\UserSQL\HashAlgorithm\Base\Utils; + +/** + * SHA512 Crypt hashing implementation. + * @author Marcin Łojewski + */ +class CryptSHA512 extends BaseCrypt +{ + use Utils; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "SHA512 (Crypt)"; + } + + /** + * @inheritdoc + */ + protected function getSalt() + { + // TODO - add support for options: rounds. + return "$5\$rounds=5000$" . self::randomString(16, self::SALT_ALPHABET) . "$"; + } +} diff --git a/lib/HashAlgorithm/CryptStandardDES.php b/lib/HashAlgorithm/CryptStandardDES.php new file mode 100644 index 0000000..44e4a23 --- /dev/null +++ b/lib/HashAlgorithm/CryptStandardDES.php @@ -0,0 +1,48 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\BaseCrypt; +use OCA\UserSQL\HashAlgorithm\Base\Utils; + +/** + * Standard DES Crypt hashing implementation. + * @author Marcin Łojewski + */ +class CryptStandardDES extends BaseCrypt +{ + use Utils; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Standard DES (Crypt)"; + } + + /** + * @inheritdoc + */ + protected function getSalt() + { + return self::randomString(2, self::SALT_ALPHABET); + } +} diff --git a/lib/HashAlgorithm/Joomla.php b/lib/HashAlgorithm/Joomla.php new file mode 100644 index 0000000..abc92af --- /dev/null +++ b/lib/HashAlgorithm/Joomla.php @@ -0,0 +1,71 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\Utils; + +/** + * Joomla hashing implementation. + * @author Marcin Łojewski + */ +class Joomla implements HashAlgorithm +{ + use Singleton; + use Utils; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Joomla MD5 Encryption"; + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + return md5($password . ":" . self::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; + } +} diff --git a/lib/HashAlgorithm/MD5.php b/lib/HashAlgorithm/MD5.php index 8b43bb2..ad39f59 100644 --- a/lib/HashAlgorithm/MD5.php +++ b/lib/HashAlgorithm/MD5.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,12 +17,13 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm; -use OCA\user_sql\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; /** - * MD5 password hash implementation. + * MD5 hashing implementation. * @author Marcin Łojewski */ class MD5 implements HashAlgorithm diff --git a/lib/HashAlgorithm/SHA1.php b/lib/HashAlgorithm/SHA1.php index b59b9c2..dfd8d7c 100644 --- a/lib/HashAlgorithm/SHA1.php +++ b/lib/HashAlgorithm/SHA1.php @@ -1,7 +1,7 @@ + * Copyright (C) 2018 Marcin Łojewski * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -17,12 +17,13 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\UserSQL\HashAlgorithm; -use OCA\user_sql\HashAlgorithm\Base\Singleton; +use OCA\UserSQL\HashAlgorithm\Base\HashAlgorithm; +use OCA\UserSQL\HashAlgorithm\Base\Singleton; /** - * SHA1 password hash implementation. + * SHA1 hashing implementation. * @author Marcin Łojewski */ class SHA1 implements HashAlgorithm diff --git a/lib/HashAlgorithm/SSHA256.php b/lib/HashAlgorithm/SSHA256.php new file mode 100644 index 0000000..516e0bf --- /dev/null +++ b/lib/HashAlgorithm/SSHA256.php @@ -0,0 +1,53 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\SSHA; + +/** + * SSHA256 hashing implementation. + * @author Marcin Łojewski + */ +class SSHA256 extends SSHA +{ + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "SSHA256"; + } + + /** + * @inheritdoc + */ + public function getPrefix() + { + return "{SSHA256}"; + } + + /** + * @inheritdoc + */ + public function getAlgorithm() + { + return "sha256"; + } +} diff --git a/lib/HashAlgorithm/SSHA512.php b/lib/HashAlgorithm/SSHA512.php new file mode 100644 index 0000000..948a28d --- /dev/null +++ b/lib/HashAlgorithm/SSHA512.php @@ -0,0 +1,53 @@ + + * + * 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 . + */ + +namespace OCA\UserSQL\HashAlgorithm; + +use OCA\UserSQL\HashAlgorithm\Base\SSHA; + +/** + * SSHA512 hashing implementation. + * @author Marcin Łojewski + */ +class SSHA512 extends SSHA +{ + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "SSHA512"; + } + + /** + * @inheritdoc + */ + public function getPrefix() + { + return "{SSHA512}"; + } + + /** + * @inheritdoc + */ + public function getAlgorithm() + { + return "sha512"; + } +}