‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\Test;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
24 final class ‪Argon2idPasswordHashTest extends UnitTestCase
25 {
27 
28  protected function ‪setUp(): void
29  {
30  parent::setUp();
31  $options = [
32  'memory_cost' => 65536,
33  'time_cost' => 4,
34  'threads' => 1,
35  ];
36  $this->subject = new ‪Argon2idPasswordHash($options);
37  }
38 
39  #[Test]
41  {
42  $this->expectException(\InvalidArgumentException::class);
43  $this->expectExceptionCode(1533899612);
44  new ‪Argon2idPasswordHash(['memory_cost' => 1]);
45  }
46 
47  #[Test]
49  {
50  $this->expectException(\InvalidArgumentException::class);
51  $this->expectExceptionCode(1533899613);
52  new ‪Argon2idPasswordHash(['time_cost' => 1]);
53  }
54 
55  #[Test]
57  {
58  self::assertNull($this->subject->getHashedPassword(''));
59  }
60 
61  #[Test]
62  public function ‪getHashedPasswordReturnsString(): void
63  {
64  $hash = $this->subject->getHashedPassword('password');
65  self::assertNotNull($hash);
66  self::assertIsString($hash);
67  }
68 
69  #[Test]
71  {
72  $hash = $this->subject->getHashedPassword('password');
73  self::assertTrue($this->subject->isValidSaltedPW($hash));
74  }
75 
79  #[Test]
81  {
82  $password = 'aEjOtY';
83  $hash = $this->subject->getHashedPassword($password);
84  self::assertTrue($this->subject->checkPassword($password, $hash));
85  }
86 
90  #[Test]
92  {
93  $password = '01369';
94  $hash = $this->subject->getHashedPassword($password);
95  self::assertTrue($this->subject->checkPassword($password, $hash));
96  }
97 
101  #[Test]
103  {
104  $password = ' !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
105  $hash = $this->subject->getHashedPassword($password);
106  self::assertTrue($this->subject->checkPassword($password, $hash));
107  }
108 
112  #[Test]
114  {
115  $password = '';
116  for ($i = 160; $i <= 191; $i++) {
117  $password .= chr($i);
118  }
119  $password .= chr(215) . chr(247);
120  $hash = $this->subject->getHashedPassword($password);
121  self::assertTrue($this->subject->checkPassword($password, $hash));
122  }
123 
127  #[Test]
129  {
130  $password = '';
131  for ($i = 192; $i <= 255; $i++) {
132  if ($i === 215 || $i === 247) {
133  // skip multiplication sign (×) and obelus (÷)
134  continue;
135  }
136  $password .= chr($i);
137  }
138  $hash = $this->subject->getHashedPassword($password);
139  self::assertTrue($this->subject->checkPassword($password, $hash));
140  }
141 
142  #[Test]
144  {
145  $password = 'password';
146  $password1 = $password . 'INVALID';
147  $hash = $this->subject->getHashedPassword($password);
148  self::assertFalse($this->subject->checkPassword($password1, $hash));
149  }
150 
151  #[Test]
153  {
154  $password = 'password';
155  $hash = $this->subject->getHashedPassword($password);
156  self::assertFalse($this->subject->isHashUpdateNeeded($hash));
157  }
158 
159  #[Test]
161  {
162  $originalOptions = [
163  'memory_cost' => 65536,
164  'time_cost' => 4,
165  'threads' => 2,
166  ];
167  ‪$subject = new ‪Argon2idPasswordHash($originalOptions);
168  $hash = ‪$subject->‪getHashedPassword('password');
169 
170  // Change $memoryCost
171  $newOptions = $originalOptions;
172  $newOptions['memory_cost'] = $newOptions['memory_cost'] + 1;
173  ‪$subject = new ‪Argon2idPasswordHash($newOptions);
174  self::assertTrue(‪$subject->‪isHashUpdateNeeded($hash));
175 
176  // Change $timeCost
177  $newOptions = $originalOptions;
178  $newOptions['time_cost'] = $newOptions['time_cost'] + 1;
179  ‪$subject = new ‪Argon2idPasswordHash($newOptions);
180  self::assertTrue(‪$subject->‪isHashUpdateNeeded($hash));
181 
182  // Change $threads
183  // Changing $threads does nothing with libsodium, so skip that.
184  if (!extension_loaded('sodium')) {
185  $newOptions = $originalOptions;
186  $newOptions['threads'] = $newOptions['threads'] + 1;
187  ‪$subject = new ‪Argon2idPasswordHash($newOptions);
188  self::assertTrue(‪$subject->‪isHashUpdateNeeded($hash));
189  }
190  }
191 }
‪TYPO3\CMS\Core\Crypto\PasswordHashing\AbstractArgon2PasswordHash\isHashUpdateNeeded
‪bool isHashUpdateNeeded(string $passString)
Definition: AbstractArgon2PasswordHash.php:136
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidNumericCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidNumericCharClassPassword()
Definition: Argon2idPasswordHashTest.php:91
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidAsciiSpecialCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidAsciiSpecialCharClassPassword()
Definition: Argon2idPasswordHashTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidAlphaCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidAlphaCharClassPassword()
Definition: Argon2idPasswordHashTest.php:80
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidLatin1UmlautCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidLatin1UmlautCharClassPassword()
Definition: Argon2idPasswordHashTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\constructorThrowsExceptionIfTimeCostIsTooLow
‪constructorThrowsExceptionIfTimeCostIsTooLow()
Definition: Argon2idPasswordHashTest.php:48
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\isHashUpdateNeededReturnsFalseForJustGeneratedHash
‪isHashUpdateNeededReturnsFalseForJustGeneratedHash()
Definition: Argon2idPasswordHashTest.php:152
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\getHashedPasswordReturnsNullOnEmptyPassword
‪getHashedPasswordReturnsNullOnEmptyPassword()
Definition: Argon2idPasswordHashTest.php:56
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\getHashedPasswordReturnsString
‪getHashedPasswordReturnsString()
Definition: Argon2idPasswordHashTest.php:62
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithValidLatin1SpecialCharClassPassword
‪checkPasswordReturnsTrueForHashedPasswordWithValidLatin1SpecialCharClassPassword()
Definition: Argon2idPasswordHashTest.php:113
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\checkPasswordReturnsTrueForHashedPasswordWithNonValidPassword
‪checkPasswordReturnsTrueForHashedPasswordWithNonValidPassword()
Definition: Argon2idPasswordHashTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\$subject
‪Argon2idPasswordHash $subject
Definition: Argon2idPasswordHashTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest
Definition: Argon2idPasswordHashTest.php:25
‪TYPO3\CMS\Core\Crypto\PasswordHashing\AbstractArgon2PasswordHash\getHashedPassword
‪getHashedPassword(string $password)
Definition: AbstractArgon2PasswordHash.php:117
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\isHashUpdateNeededReturnsTrueForHashGeneratedWithOldOptions
‪isHashUpdateNeededReturnsTrueForHashGeneratedWithOldOptions()
Definition: Argon2idPasswordHashTest.php:160
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\constructorThrowsExceptionIfMemoryCostIsTooLow
‪constructorThrowsExceptionIfMemoryCostIsTooLow()
Definition: Argon2idPasswordHashTest.php:40
‪TYPO3\CMS\Core\Crypto\PasswordHashing\Argon2idPasswordHash
Definition: Argon2idPasswordHash.php:31
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\setUp
‪setUp()
Definition: Argon2idPasswordHashTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing
Definition: Argon2idPasswordHashTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Crypto\PasswordHashing\Argon2idPasswordHashTest\isValidSaltedPwValidatesHastCreatedByGetHashedPassword
‪isValidSaltedPwValidatesHastCreatedByGetHashedPassword()
Definition: Argon2idPasswordHashTest.php:70