Courier hashes. New traits.

This commit is contained in:
Marcin Łojewski
2018-02-28 23:20:19 +01:00
parent 9c13804638
commit a6a502fe7c
9 changed files with 282 additions and 9 deletions

View File

@@ -0,0 +1,41 @@
<?php
/**
* Nextcloud - user_sql
* Copyright (C) 2012-2018 Andreas Böhler <dev (at) aboehler (dot) at>
*
* 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\user_sql\HashAlgorithm\Base;
/**
* Base64 utilities trait.
* @author Marcin Łojewski <dev@mlojewski.me>
*/
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);
}
}

View File

@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
namespace OCA\user_sql\HashAlgorithm; namespace OCA\user_sql\HashAlgorithm\Base;
/** /**
* Singleton pattern trait. * Singleton pattern trait.

View File

@@ -19,6 +19,8 @@
namespace OCA\user_sql\HashAlgorithm; namespace OCA\user_sql\HashAlgorithm;
use OCA\user_sql\HashAlgorithm\Base\Singleton;
/** /**
* Cleartext password hash implementation. * Cleartext password hash implementation.
* @author Marcin Łojewski <dev@mlojewski.me> * @author Marcin Łojewski <dev@mlojewski.me>

View File

@@ -0,0 +1,57 @@
<?php
/**
* Nextcloud - user_sql
* Copyright (C) 2012-2018 Andreas Böhler <dev (at) aboehler (dot) at>
*
* 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\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 <dev@mlojewski.me>
*/
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));
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Nextcloud - user_sql
* Copyright (C) 2012-2018 Andreas Böhler <dev (at) aboehler (dot) at>
*
* 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\user_sql\HashAlgorithm;
use OCA\user_sql\HashAlgorithm\Base\Singleton;
/**
* Courier MD5 RAW password hash implementation.
* @author Marcin Łojewski <dev@mlojewski.me>
*/
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);
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* Nextcloud - user_sql
* Copyright (C) 2012-2018 Andreas Böhler <dev (at) aboehler (dot) at>
*
* 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\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 <dev@mlojewski.me>
*/
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));
}
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* Nextcloud - user_sql
* Copyright (C) 2012-2018 Andreas Böhler <dev (at) aboehler (dot) at>
*
* 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\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 <dev@mlojewski.me>
*/
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));
}
}

View File

@@ -19,6 +19,8 @@
namespace OCA\user_sql\HashAlgorithm; namespace OCA\user_sql\HashAlgorithm;
use OCA\user_sql\HashAlgorithm\Base\Singleton;
/** /**
* MD5 password hash implementation. * MD5 password hash implementation.
* @author Marcin Łojewski <dev@mlojewski.me> * @author Marcin Łojewski <dev@mlojewski.me>
@@ -38,16 +40,16 @@ class MD5 implements HashAlgorithm
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getPasswordHash($password) public function checkPassword($password, $dbHash)
{ {
return md5($password); return $this->getPasswordHash($password) === $dbHash;
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function checkPassword($password, $dbHash) public function getPasswordHash($password)
{ {
return md5($password) === $dbHash; return md5($password);
} }
} }

View File

@@ -19,6 +19,8 @@
namespace OCA\user_sql\HashAlgorithm; namespace OCA\user_sql\HashAlgorithm;
use OCA\user_sql\HashAlgorithm\Base\Singleton;
/** /**
* SHA1 password hash implementation. * SHA1 password hash implementation.
* @author Marcin Łojewski <dev@mlojewski.me> * @author Marcin Łojewski <dev@mlojewski.me>
@@ -38,16 +40,16 @@ class SHA1 implements HashAlgorithm
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getPasswordHash($password) public function checkPassword($password, $dbHash)
{ {
return sha1($password); return $this->getPasswordHash($password) === $dbHash;
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function checkPassword($password, $dbHash) public function getPasswordHash($password)
{ {
return sha1($password) === $dbHash; return sha1($password);
} }
} }