diff --git a/lib/HashAlgorithm/Base/Base64.php b/lib/HashAlgorithm/Base/Base64.php new file mode 100644 index 0000000..ca9fb05 --- /dev/null +++ b/lib/HashAlgorithm/Base/Base64.php @@ -0,0 +1,41 @@ + + * + * 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\user_sql\HashAlgorithm\Base; + +/** + * Base64 utilities trait. + * @author Marcin Łojewski + */ +trait Base64 +{ + /** + * Convert hexadecimal message to its base64 form. + * @param $hex string Hexadecimal encoded message. + * @return string Same message encoded in base64. + */ + private static function hexToBase64($hex) + { + $hexChr = ''; + foreach (str_split($hex, 2) as $hexPair) { + $hexChr .= chr(hexdec($hexPair)); + } + return base64_encode($hexChr); + } +} diff --git a/lib/HashAlgorithm/Singleton.php b/lib/HashAlgorithm/Base/Singleton.php similarity index 96% rename from lib/HashAlgorithm/Singleton.php rename to lib/HashAlgorithm/Base/Singleton.php index b54c550..39dadc4 100644 --- a/lib/HashAlgorithm/Singleton.php +++ b/lib/HashAlgorithm/Base/Singleton.php @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -namespace OCA\user_sql\HashAlgorithm; +namespace OCA\user_sql\HashAlgorithm\Base; /** * Singleton pattern trait. diff --git a/lib/HashAlgorithm/Cleartext.php b/lib/HashAlgorithm/Cleartext.php index 179d1ba..428ea83 100644 --- a/lib/HashAlgorithm/Cleartext.php +++ b/lib/HashAlgorithm/Cleartext.php @@ -19,6 +19,8 @@ namespace OCA\user_sql\HashAlgorithm; +use OCA\user_sql\HashAlgorithm\Base\Singleton; + /** * Cleartext password hash implementation. * @author Marcin Łojewski diff --git a/lib/HashAlgorithm/CourierMD5.php b/lib/HashAlgorithm/CourierMD5.php new file mode 100644 index 0000000..42d6e7b --- /dev/null +++ b/lib/HashAlgorithm/CourierMD5.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\user_sql\HashAlgorithm; + +use OCA\user_sql\HashAlgorithm\Base\Base64; +use OCA\user_sql\HashAlgorithm\Base\Singleton; + +/** + * Courier MD5 password hash implementation. + * @author Marcin Łojewski + */ +class CourierMD5 implements HashAlgorithm +{ + use Base64; + use Singleton; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Courier base64-encoded MD5"; + } + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return $this->getPasswordHash($password) === $dbHash; + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + return '{MD5}' . self::hexToBase64(md5($password)); + } +} diff --git a/lib/HashAlgorithm/CourierMD5Raw.php b/lib/HashAlgorithm/CourierMD5Raw.php new file mode 100644 index 0000000..3d31819 --- /dev/null +++ b/lib/HashAlgorithm/CourierMD5Raw.php @@ -0,0 +1,55 @@ + + * + * 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\user_sql\HashAlgorithm; + +use OCA\user_sql\HashAlgorithm\Base\Singleton; + +/** + * Courier MD5 RAW password hash implementation. + * @author Marcin Łojewski + */ +class CourierMD5Raw implements HashAlgorithm +{ + use Singleton; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Courier hexadecimal MD5"; + } + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return $this->getPasswordHash($password) === $dbHash; + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + return '{MD5RAW}' . md5($password); + } +} diff --git a/lib/HashAlgorithm/CourierSHA1.php b/lib/HashAlgorithm/CourierSHA1.php new file mode 100644 index 0000000..2553c61 --- /dev/null +++ b/lib/HashAlgorithm/CourierSHA1.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\user_sql\HashAlgorithm; + +use OCA\user_sql\HashAlgorithm\Base\Base64; +use OCA\user_sql\HashAlgorithm\Base\Singleton; + +/** + * Courier SHA1 password hash implementation. + * @author Marcin Łojewski + */ +class CourierSHA1 implements HashAlgorithm +{ + use Base64; + use Singleton; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Courier base64-encoded SHA1"; + } + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return $this->getPasswordHash($password) === $dbHash; + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + return '{SHA}' . self::hexToBase64(sha1($password)); + } +} diff --git a/lib/HashAlgorithm/CourierSHA256.php b/lib/HashAlgorithm/CourierSHA256.php new file mode 100644 index 0000000..c7e4c94 --- /dev/null +++ b/lib/HashAlgorithm/CourierSHA256.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\user_sql\HashAlgorithm; + +use OCA\user_sql\HashAlgorithm\Base\Base64; +use OCA\user_sql\HashAlgorithm\Base\Singleton; + +/** + * Courier SHA256 password hash implementation. + * @author Marcin Łojewski + */ +class CourierSHA256 implements HashAlgorithm +{ + use Base64; + use Singleton; + + /** + * @inheritdoc + */ + public function getVisibleName() + { + return "Courier base64-encoded SHA256"; + } + + /** + * @inheritdoc + */ + public function checkPassword($password, $dbHash) + { + return $this->getPasswordHash($password) === $dbHash; + } + + /** + * @inheritdoc + */ + public function getPasswordHash($password) + { + return '{SHA256}' . self::hexToBase64(hash('sha256', $password)); + } +} diff --git a/lib/HashAlgorithm/MD5.php b/lib/HashAlgorithm/MD5.php index acf3370..f87db67 100644 --- a/lib/HashAlgorithm/MD5.php +++ b/lib/HashAlgorithm/MD5.php @@ -19,6 +19,8 @@ namespace OCA\user_sql\HashAlgorithm; +use OCA\user_sql\HashAlgorithm\Base\Singleton; + /** * MD5 password hash implementation. * @author Marcin Łojewski @@ -38,16 +40,16 @@ class MD5 implements HashAlgorithm /** * @inheritdoc */ - public function getPasswordHash($password) + public function checkPassword($password, $dbHash) { - return md5($password); + return $this->getPasswordHash($password) === $dbHash; } /** * @inheritdoc */ - public function checkPassword($password, $dbHash) + public function getPasswordHash($password) { - return md5($password) === $dbHash; + return md5($password); } } diff --git a/lib/HashAlgorithm/SHA1.php b/lib/HashAlgorithm/SHA1.php index e9f7a1e..086a527 100644 --- a/lib/HashAlgorithm/SHA1.php +++ b/lib/HashAlgorithm/SHA1.php @@ -19,6 +19,8 @@ namespace OCA\user_sql\HashAlgorithm; +use OCA\user_sql\HashAlgorithm\Base\Singleton; + /** * SHA1 password hash implementation. * @author Marcin Łojewski @@ -38,16 +40,16 @@ class SHA1 implements HashAlgorithm /** * @inheritdoc */ - public function getPasswordHash($password) + public function checkPassword($password, $dbHash) { - return sha1($password); + return $this->getPasswordHash($password) === $dbHash; } /** * @inheritdoc */ - public function checkPassword($password, $dbHash) + public function getPasswordHash($password) { - return sha1($password) === $dbHash; + return sha1($password); } }