Skip to content

Commit 93e769b

Browse files
committed
Dedicated class for crypto parameters
1 parent 5045c7d commit 93e769b

8 files changed

Lines changed: 83 additions & 44 deletions

File tree

lib/Crypto/CryptArgon2.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OCA\UserSQL\Crypto;
2323

24+
use OCA\UserSQL\Model\CryptoParam;
2425
use OCP\IL10N;
2526

2627
/**
@@ -106,18 +107,14 @@ public function getPasswordHash($password, $salt = null)
106107
public function configuration()
107108
{
108109
return [
109-
[
110-
"name" => "memoryCost", "visible_name" => "Memory cost (KiB)",
111-
"default" => PASSWORD_ARGON2_DEFAULT_MEMORY_COST, "min" => 1, "max" => 1048576
112-
],
113-
[
114-
"name" => "timeCost", "visible_name" => "Time cost",
115-
"default" => PASSWORD_ARGON2_DEFAULT_TIME_COST, "min" => 1, "max" => 1024
116-
],
117-
[
118-
"name" => "threads", "visible_name" => "Threads",
119-
"default" => PASSWORD_ARGON2_DEFAULT_THREADS, "min" => 1, "max" => 1024
120-
]
110+
new CryptoParam(
111+
"Memory cost (KiB)", PASSWORD_ARGON2_DEFAULT_MEMORY_COST, 1,
112+
1048576
113+
),
114+
new CryptoParam(
115+
"Time cost", PASSWORD_ARGON2_DEFAULT_TIME_COST, 1, 1024
116+
),
117+
new CryptoParam("Threads", PASSWORD_ARGON2_DEFAULT_THREADS, 1, 1024)
121118
];
122119
}
123120

lib/Crypto/CryptBlowfish.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OCA\UserSQL\Crypto;
2323

24+
use OCA\UserSQL\Model\CryptoParam;
2425
use OCP\IL10N;
2526

2627
/**
@@ -72,12 +73,7 @@ public function getPasswordHash($password, $salt = null)
7273
*/
7374
public function configuration()
7475
{
75-
return [
76-
[
77-
"name" => "cost", "visible_name" => "Cost", "default" => 10,
78-
"min" => 4, "max" => 31
79-
]
80-
];
76+
return [new CryptoParam("Cost", 10, 4, 31)];
8177
}
8278

8379
/**

lib/Crypto/CryptExtendedDES.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OCA\UserSQL\Crypto;
2323

24+
use OCA\UserSQL\Model\CryptoParam;
2425
use OCP\IL10N;
2526

2627
/**
@@ -53,12 +54,7 @@ public function __construct(IL10N $localization, $iterationCount = 1000)
5354
*/
5455
public function configuration()
5556
{
56-
return [
57-
[
58-
"name" => "iterations", "visible_name" => "Iterations", "default" => 1000,
59-
"min" => 0, "max" => 16777215
60-
]
61-
];
57+
return [new CryptoParam("Iterations", 1000, 0, 16777215)];
6258
}
6359

6460
/**

lib/Crypto/CryptSHA256.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OCA\UserSQL\Crypto;
2323

24+
use OCA\UserSQL\Model\CryptoParam;
2425
use OCP\IL10N;
2526

2627
/**
@@ -54,12 +55,7 @@ public function __construct(IL10N $localization, $rounds = 5000)
5455
*/
5556
public function configuration()
5657
{
57-
return [
58-
[
59-
"name" => "rounds", "visible_name" => "Rounds", "default" => 5000,
60-
"min" => 1000, "max" => 999999999
61-
]
62-
];
58+
return [new CryptoParam("Rounds", 5000, 1000, 999999999)];
6359
}
6460

6561
/**

lib/Crypto/CryptSHA512.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OCA\UserSQL\Crypto;
2323

24+
use OCA\UserSQL\Model\CryptoParam;
2425
use OCP\IL10N;
2526

2627
/**
@@ -54,12 +55,7 @@ public function __construct(IL10N $localization, $rounds = 5000)
5455
*/
5556
public function configuration()
5657
{
57-
return [
58-
[
59-
"name" => "rounds", "visible_name" => "Rounds", "default" => 5000,
60-
"min" => 1000, "max" => 999999999
61-
]
62-
];
58+
return [new CryptoParam("Rounds", 5000, 1000, 999999999)];
6359
}
6460

6561
/**

lib/Crypto/IPasswordAlgorithm.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public function checkPassword($password, $dbHash, $salt = null);
6161

6262
/**
6363
* Configuration for the algorithm.
64-
* The return array should contain entries which define keys:
65-
* name, visible_name, default, min, max.
64+
* The return array should contain entries of class <code>CryptoParam</code>
6665
*
6766
* @return array The configuration array.
6867
*/

lib/Crypto/Phpass.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OCA\UserSQL\Crypto;
2323

24+
use OCA\UserSQL\Model\CryptoParam;
2425
use OCP\IL10N;
2526

2627
/**
@@ -160,12 +161,7 @@ private function genSalt()
160161
*/
161162
public function configuration()
162163
{
163-
return [
164-
[
165-
"name" => "iterations", "visible_name" => "Iterations (log2)",
166-
"default" => 8, "min" => 4, "max" => 31
167-
]
168-
];
164+
return [new CryptoParam("Iterations (log2)", 8, 4, 31)];
169165
}
170166

171167
/**

lib/Model/CryptoParam.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Nextcloud - user_sql
4+
*
5+
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
6+
* @author Marcin Łojewski <dev@mlojewski.me>
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
namespace OCA\UserSQL\Model;
23+
24+
/**
25+
* A parameter of a hash algorithm.
26+
*
27+
* @author Marcin Łojewski <dev@mlojewski.me>
28+
*/
29+
class CryptoParam
30+
{
31+
/**
32+
* @var string Parameter name.
33+
*/
34+
public $name;
35+
/**
36+
* @var int Parameter default value.
37+
*/
38+
public $value;
39+
/**
40+
* @var int Minimal value for parameter.
41+
*/
42+
public $min;
43+
/**
44+
* @var int Maximum value for parameter.
45+
*/
46+
public $max;
47+
48+
/**
49+
* Class constructor.
50+
*
51+
* @param $name string Parameter name.
52+
* @param $value int Parameter default value.
53+
* @param $min int Minimal value for parameter.
54+
* @param $max int Maximum value for parameter.
55+
*/
56+
public function __construct($name, $value, $min, $max)
57+
{
58+
$this->name = $name;
59+
$this->value = $value;
60+
$this->min = $min;
61+
$this->max = $max;
62+
}
63+
}

0 commit comments

Comments
 (0)