Dedicated class for crypto parameters

This commit is contained in:
Marcin Łojewski
2018-12-24 15:28:05 +01:00
parent 5045c7d931
commit 93e769bfe5
8 changed files with 83 additions and 44 deletions

View File

@@ -21,6 +21,7 @@
namespace OCA\UserSQL\Crypto; namespace OCA\UserSQL\Crypto;
use OCA\UserSQL\Model\CryptoParam;
use OCP\IL10N; use OCP\IL10N;
/** /**
@@ -106,18 +107,14 @@ class CryptArgon2 extends AbstractAlgorithm
public function configuration() public function configuration()
{ {
return [ return [
[ new CryptoParam(
"name" => "memoryCost", "visible_name" => "Memory cost (KiB)", "Memory cost (KiB)", PASSWORD_ARGON2_DEFAULT_MEMORY_COST, 1,
"default" => PASSWORD_ARGON2_DEFAULT_MEMORY_COST, "min" => 1, "max" => 1048576 1048576
], ),
[ new CryptoParam(
"name" => "timeCost", "visible_name" => "Time cost", "Time cost", PASSWORD_ARGON2_DEFAULT_TIME_COST, 1, 1024
"default" => PASSWORD_ARGON2_DEFAULT_TIME_COST, "min" => 1, "max" => 1024 ),
], new CryptoParam("Threads", PASSWORD_ARGON2_DEFAULT_THREADS, 1, 1024)
[
"name" => "threads", "visible_name" => "Threads",
"default" => PASSWORD_ARGON2_DEFAULT_THREADS, "min" => 1, "max" => 1024
]
]; ];
} }

View File

@@ -21,6 +21,7 @@
namespace OCA\UserSQL\Crypto; namespace OCA\UserSQL\Crypto;
use OCA\UserSQL\Model\CryptoParam;
use OCP\IL10N; use OCP\IL10N;
/** /**
@@ -72,12 +73,7 @@ class CryptBlowfish extends AbstractAlgorithm
*/ */
public function configuration() public function configuration()
{ {
return [ return [new CryptoParam("Cost", 10, 4, 31)];
[
"name" => "cost", "visible_name" => "Cost", "default" => 10,
"min" => 4, "max" => 31
]
];
} }
/** /**

View File

@@ -21,6 +21,7 @@
namespace OCA\UserSQL\Crypto; namespace OCA\UserSQL\Crypto;
use OCA\UserSQL\Model\CryptoParam;
use OCP\IL10N; use OCP\IL10N;
/** /**
@@ -53,12 +54,7 @@ class CryptExtendedDES extends AbstractCrypt
*/ */
public function configuration() public function configuration()
{ {
return [ return [new CryptoParam("Iterations", 1000, 0, 16777215)];
[
"name" => "iterations", "visible_name" => "Iterations", "default" => 1000,
"min" => 0, "max" => 16777215
]
];
} }
/** /**

View File

@@ -21,6 +21,7 @@
namespace OCA\UserSQL\Crypto; namespace OCA\UserSQL\Crypto;
use OCA\UserSQL\Model\CryptoParam;
use OCP\IL10N; use OCP\IL10N;
/** /**
@@ -54,12 +55,7 @@ class CryptSHA256 extends AbstractCrypt
*/ */
public function configuration() public function configuration()
{ {
return [ return [new CryptoParam("Rounds", 5000, 1000, 999999999)];
[
"name" => "rounds", "visible_name" => "Rounds", "default" => 5000,
"min" => 1000, "max" => 999999999
]
];
} }
/** /**

View File

@@ -21,6 +21,7 @@
namespace OCA\UserSQL\Crypto; namespace OCA\UserSQL\Crypto;
use OCA\UserSQL\Model\CryptoParam;
use OCP\IL10N; use OCP\IL10N;
/** /**
@@ -54,12 +55,7 @@ class CryptSHA512 extends AbstractCrypt
*/ */
public function configuration() public function configuration()
{ {
return [ return [new CryptoParam("Rounds", 5000, 1000, 999999999)];
[
"name" => "rounds", "visible_name" => "Rounds", "default" => 5000,
"min" => 1000, "max" => 999999999
]
];
} }
/** /**

View File

@@ -61,8 +61,7 @@ interface IPasswordAlgorithm
/** /**
* Configuration for the algorithm. * Configuration for the algorithm.
* The return array should contain entries which define keys: * The return array should contain entries of class <code>CryptoParam</code>
* name, visible_name, default, min, max.
* *
* @return array The configuration array. * @return array The configuration array.
*/ */

View File

@@ -21,6 +21,7 @@
namespace OCA\UserSQL\Crypto; namespace OCA\UserSQL\Crypto;
use OCA\UserSQL\Model\CryptoParam;
use OCP\IL10N; use OCP\IL10N;
/** /**
@@ -160,12 +161,7 @@ class Phpass extends AbstractAlgorithm
*/ */
public function configuration() public function configuration()
{ {
return [ return [new CryptoParam("Iterations (log2)", 8, 4, 31)];
[
"name" => "iterations", "visible_name" => "Iterations (log2)",
"default" => 8, "min" => 4, "max" => 31
]
];
} }
/** /**

63
lib/Model/CryptoParam.php Normal file
View 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\Model;
/**
* A parameter of a hash algorithm.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/
class CryptoParam
{
/**
* @var string Parameter name.
*/
public $name;
/**
* @var int Parameter default value.
*/
public $value;
/**
* @var int Minimal value for parameter.
*/
public $min;
/**
* @var int Maximum value for parameter.
*/
public $max;
/**
* Class constructor.
*
* @param $name string Parameter name.
* @param $value int Parameter default value.
* @param $min int Minimal value for parameter.
* @param $max int Maximum value for parameter.
*/
public function __construct($name, $value, $min, $max)
{
$this->name = $name;
$this->value = $value;
$this->min = $min;
$this->max = $max;
}
}