Courier hashes. New traits.
This commit is contained in:
41
lib/HashAlgorithm/Base/Base64.php
Normal file
41
lib/HashAlgorithm/Base/Base64.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@
|
||||
* 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.
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
namespace OCA\user_sql\HashAlgorithm;
|
||||
|
||||
use OCA\user_sql\HashAlgorithm\Base\Singleton;
|
||||
|
||||
/**
|
||||
* Cleartext password hash implementation.
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
|
||||
57
lib/HashAlgorithm/CourierMD5.php
Normal file
57
lib/HashAlgorithm/CourierMD5.php
Normal 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));
|
||||
}
|
||||
}
|
||||
55
lib/HashAlgorithm/CourierMD5Raw.php
Normal file
55
lib/HashAlgorithm/CourierMD5Raw.php
Normal 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);
|
||||
}
|
||||
}
|
||||
57
lib/HashAlgorithm/CourierSHA1.php
Normal file
57
lib/HashAlgorithm/CourierSHA1.php
Normal 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));
|
||||
}
|
||||
}
|
||||
57
lib/HashAlgorithm/CourierSHA256.php
Normal file
57
lib/HashAlgorithm/CourierSHA256.php
Normal 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));
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
namespace OCA\user_sql\HashAlgorithm;
|
||||
|
||||
use OCA\user_sql\HashAlgorithm\Base\Singleton;
|
||||
|
||||
/**
|
||||
* MD5 password hash implementation.
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
namespace OCA\user_sql\HashAlgorithm;
|
||||
|
||||
use OCA\user_sql\HashAlgorithm\Base\Singleton;
|
||||
|
||||
/**
|
||||
* SHA1 password hash implementation.
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user