Skip to content

Commit be70df4

Browse files
committed
Merge branch 'feature/drupal_7' into develop
2 parents 97aafd9 + 23ccb5d commit be70df4

5 files changed

Lines changed: 139 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
### Added
99
- Whirlpool hash algorithm
1010
- 'Prepend salt' toggle
11+
- Drupal 7 hash algorithm
1112
### Fixed
1213
- Error when 'Display name' not set
1314
- Encoding of iteration for 'Extended DES (Crypt)'

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ MD5 (Crypt) | | $1$RzaFbNcU$u9adfTY/Q6za6nu0Ogrl1/
190190
SHA256 (Crypt) | Generates hash with 5000 rounds. | $5$rounds=5000$VIYD0iHkg7uY9SRc$v2XLS/9dvfFN84mzGvW9wxnVt9Xd/urXaaTkpW8EwD1
191191
SHA512 (Crypt) | Generates hash with 5000 rounds. | $6$rounds=5000$yH.Q0OL4qbCOUJ3q$Xry5EVFva3wKnfo8/ktrugmBd8tcl34NK6rXInv1HhmdSUNLEm0La9JnA57rqwQ.9/Bz513MD4tvmmISLUIHs/
192192
Standard DES (Crypt) | | yTBnb7ab/N072
193+
Drupal 7 | See [phpass](http://www.openwall.com/phpass/). | $S$DC7eCpJQ3SUQtW4Bp.vKb2rpeaffi4iqk9OpYwJyEoSMsezn67Sl
193194
Joomla MD5 Encryption | Generates 32 chars salt. | 14d21b49b0f13e2acba962b6b0039edd:haJK0yTvBXTNMh76xwEw5RYEVpJsN8us
194195
MD5 | No salt supported. | 5f4dcc3b5aa765d61d8327deb882cf99
195196
Portable PHP password | See [phpass](http://www.openwall.com/phpass/). | $P$BxrwraqNTi4as0EI.IpiA/K.muk9ke/

lib/Crypto/Drupal7.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Crypto;
23+
24+
/**
25+
* Drupal 7 overrides of phpass hash implementation.
26+
*
27+
* @author BrandonKerr
28+
* @author Marcin Łojewski <dev@mlojewski.me>
29+
*/
30+
class Drupal7 extends Phpass
31+
{
32+
/**
33+
* The expected (and maximum) number of characters in a hashed password.
34+
*/
35+
const DRUPAL_HASH_LENGTH = 55;
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
protected function crypt($password, $setting)
41+
{
42+
return substr(parent::crypt($password, $setting), 0, self::DRUPAL_HASH_LENGTH);
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
protected function hash($input)
49+
{
50+
return hash('sha512', $input, true);
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function getAlgorithmName()
57+
{
58+
return "Drupal 7";
59+
}
60+
}

lib/Crypto/Phpass.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function checkPassword($password, $dbHash)
6161
*
6262
* @return string|null Generated hash. Null on invalid settings.
6363
*/
64-
private function crypt($password, $setting)
64+
protected function crypt($password, $setting)
6565
{
6666
$countLog2 = strpos(self::ITOA64, $setting[3]);
6767
if ($countLog2 < 7 || $countLog2 > 30) {
@@ -75,17 +75,29 @@ private function crypt($password, $setting)
7575
return null;
7676
}
7777

78-
$hash = md5($salt . $password, true);
78+
$hash = $this->hash($salt . $password);
7979
do {
80-
$hash = md5($hash . $password, true);
80+
$hash = $this->hash($hash . $password);
8181
} while (--$count);
8282

8383
$output = substr($setting, 0, 12);
84-
$output .= $this->encode64($hash, 16);
84+
$output .= $this->encode64($hash, strlen($hash));
8585

8686
return $output;
8787
}
8888

89+
/**
90+
* Apply hash function to input.
91+
*
92+
* @param string $input Input string.
93+
*
94+
* @return string Hashed input.
95+
*/
96+
protected function hash($input)
97+
{
98+
return md5($input, true);
99+
}
100+
89101
/**
90102
* Encode binary input to base64 string.
91103
*

tests/Crypto/Drupal7Test.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 Tests\UserSQL\Crypto;
23+
24+
use OCA\UserSQL\Crypto\Drupal7;
25+
use OCA\UserSQL\Crypto\IPasswordAlgorithm;
26+
use OCP\IL10N;
27+
use Test\TestCase;
28+
29+
/**
30+
* Unit tests for class <code>Drupal7</code>.
31+
*
32+
* @author Marcin Łojewski <dev@mlojewski.me>
33+
*/
34+
class Drupal7Test extends TestCase
35+
{
36+
/**
37+
* @var IPasswordAlgorithm
38+
*/
39+
private $crypto;
40+
41+
public function testCheckPassword()
42+
{
43+
$this->assertTrue(
44+
$this->crypto->checkPassword(
45+
"password", "\$S\$DC7eCpJQ3SUQtW4Bp.vKb2rpeaffi4iqk9OpYwJyEoSMsezn67Sl"
46+
)
47+
);
48+
}
49+
50+
public function testPasswordHash()
51+
{
52+
$hash = $this->crypto->getPasswordHash("password");
53+
$this->assertTrue($this->crypto->checkPassword("password", $hash));
54+
}
55+
56+
protected function setUp()
57+
{
58+
parent::setUp();
59+
$this->crypto = new Drupal7($this->createMock(IL10N::class));
60+
}
61+
}

0 commit comments

Comments
 (0)