‪TYPO3CMS  11.5
Argon2idPasswordHashTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
23 class ‪Argon2idPasswordHashTest extends UnitTestCase
24 {
26 
27  protected function ‪setUp(): void
28  {
29  parent::setUp();
30  $options = [
31  'memory_cost' => 65536,
32  'time_cost' => 4,
33  'threads' => 1,
34  ];
35  $this->subject = new ‪Argon2idPasswordHash($options);
36  }
37 
42  {
43  $this->expectException(\InvalidArgumentException::class);
44  $this->expectExceptionCode(1533899612);
45  new ‪Argon2idPasswordHash(['memory_cost' => 1]);
46  }
47 
52  {
53  $this->expectException(\InvalidArgumentException::class);
54  $this->expectExceptionCode(1533899613);
55  new ‪Argon2idPasswordHash(['time_cost' => 1]);
56  }
57 
62  {
63  self::assertNull($this->subject->getHashedPassword(''));
64  }
65 
69  public function ‪getHashedPasswordReturnsString(): void
70  {
71  $hash = $this->subject->getHashedPassword('password');
72  self::assertNotNull($hash);
73  self::assertIsString($hash);
74  }
75 
80  {
81  $hash = $this->subject->getHashedPassword('password');
82  self::assertTrue($this->subject->isValidSaltedPW($hash));
83  }
84 
91  {
92  $password = 'aEjOtY';
93  $hash = $this->subject->getHashedPassword($password);
94  self::assertTrue($this->subject->checkPassword($password, $hash));
95  }
96 
103  {
104  $password = '01369';
105  $hash = $this->subject->getHashedPassword($password);
106  self::assertTrue($this->subject->checkPassword($password, $hash));
107  }
108 
115  {
116  $password = ' !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
117  $hash = $this->subject->getHashedPassword($password);
118  self::assertTrue($this->subject->checkPassword($password, $hash));
119  }
120 
127  {
128  $password = '';
129  for ($i = 160; $i <= 191; $i++) {
130  $password .= chr($i);
131  }
132  $password .= chr(215) . chr(247);
133  $hash = $this->subject->getHashedPassword($password);
134  self::assertTrue($this->subject->checkPassword($password, $hash));
135  }
136 
143  {
144  $password = '';
145  for ($i = 192; $i <= 255; $i++) {
146  if ($i === 215 || $i === 247) {
147  // skip multiplication sign (×) and obelus (÷)
148  continue;
149  }
150  $password .= chr($i);
151  }
152  $hash = $this->subject->getHashedPassword($password);
153  self::assertTrue($this->subject->checkPassword($password, $hash));
154  }
155 
160  {
161  $password = 'password';
162  $password1 = $password . 'INVALID';
163  $hash = $this->subject->getHashedPassword($password);
164  self::assertFalse($this->subject->checkPassword($password1, $hash));
165  }
166 
171  {
172  $password = 'password';
173  $hash = $this->subject->getHashedPassword($password);
174  self::assertFalse($this->subject->isHashUpdateNeeded($hash));
175  }
176 
181  {
182  $originalOptions = [
183  'memory_cost' => 65536,
184  'time_cost' => 4,
185  'threads' => 2,
186  ];
187  ‪$subject = new ‪Argon2idPasswordHash($originalOptions);
188  $hash = ‪$subject->‪getHashedPassword('password');
189 
190  // Change $memoryCost
191  $newOptions = $originalOptions;
192  $newOptions['memory_cost'] = $newOptions['memory_cost'] + 1;
193  ‪$subject = new ‪Argon2idPasswordHash($newOptions);
194  self::assertTrue(‪$subject->‪isHashUpdateNeeded($hash));
195 
196  // Change $timeCost
197  $newOptions = $originalOptions;
198  $newOptions['time_cost'] = $newOptions['time_cost'] + 1;
199  ‪$subject = new ‪Argon2idPasswordHash($newOptions);
200  self::assertTrue(‪$subject->‪isHashUpdateNeeded($hash));
201 
202  // Change $threads
203  // Changing $threads does nothing with libsodium, so skip that.
204  if (!extension_loaded('sodium')) {
205  $newOptions = $originalOptions;
206  $newOptions['threads'] = $newOptions['threads'] + 1;
207  ‪$subject = new ‪Argon2idPasswordHash($newOptions);
208  self::assertTrue(‪$subject->‪isHashUpdateNeeded($hash));
209  }
210  }
211 }
‪TYPO3\CMS\Core\Crypto\PasswordHashing\AbstractArgon2PasswordHash\isHashUpdateNeeded
‪bool isHashUpdateNeeded(string $passString)
Definition: AbstractArgon2PasswordHash.php:145
‪TYPO3\CMS\Core\Crypto\PasswordHashing\AbstractArgon2PasswordHash\getHashedPassword
‪string null getHashedPassword(string $password)
Definition: AbstractArgon2PasswordHash.php:126
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidNumericCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidNumericCharClassPassword()
Definition: Argon2idPasswordHashTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidAsciiSpecialCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidAsciiSpecialCharClassPassword()
Definition: Argon2idPasswordHashTest.php:114
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidAlphaCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidAlphaCharClassPassword()
Definition: Argon2idPasswordHashTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidLatin1UmlautCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidLatin1UmlautCharClassPassword()
Definition: Argon2idPasswordHashTest.php:142
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\constructorThrowsExceptionIfTimeCostIsTooLow
‪constructorThrowsExceptionIfTimeCostIsTooLow()
Definition: Argon2idPasswordHashTest.php:51
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\isHashUpdateNeededReturnsFalseForJustGeneratedHash
‪isHashUpdateNeededReturnsFalseForJustGeneratedHash()
Definition: Argon2idPasswordHashTest.php:170
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\getHashedPasswordReturnsNullOnEmptyPassword
‪getHashedPasswordReturnsNullOnEmptyPassword()
Definition: Argon2idPasswordHashTest.php:61
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\getHashedPasswordReturnsString
‪getHashedPasswordReturnsString()
Definition: Argon2idPasswordHashTest.php:69
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidLatin1SpecialCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidLatin1SpecialCharClassPassword()
Definition: Argon2idPasswordHashTest.php:126
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithNonValidPassword
‪checkPasswordReturnsTrueForHashedPasswordWithNonValidPassword()
Definition: Argon2idPasswordHashTest.php:159
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\$subject
‪Argon2idPasswordHash $subject
Definition: Argon2idPasswordHashTest.php:25
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest
Definition: Argon2idPasswordHashTest.php:24
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\isHashUpdateNeededReturnsTrueForHashGeneratedWithOldOptions
‪isHashUpdateNeededReturnsTrueForHashGeneratedWithOldOptions()
Definition: Argon2idPasswordHashTest.php:180
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\constructorThrowsExceptionIfMemoryCostIsTooLow
‪constructorThrowsExceptionIfMemoryCostIsTooLow()
Definition: Argon2idPasswordHashTest.php:41
‪TYPO3\CMS\Core\Crypto\PasswordHashing\Argon2idPasswordHash
Definition: Argon2idPasswordHash.php:31
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\setUp
‪setUp()
Definition: Argon2idPasswordHashTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing
Definition: Argon2idPasswordHashTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\isValidSaltedPwValidatesHastCreatedByGetHashedPassword
‪isValidSaltedPwValidatesHastCreatedByGetHashedPassword()
Definition: Argon2idPasswordHashTest.php:79