TYPO3 CMS  TYPO3_6-2
PhpassSaltTest.php
Go to the documentation of this file.
1 <?php
3 
23 
29  protected $objectInstance = NULL;
30 
36  public function setUp() {
37  $this->objectInstance = $this->getMock('TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt', array('dummy'));
38  }
39 
43  public function hasCorrectBaseClass() {
44  $hasCorrectBaseClass = get_class($this->objectInstance) === 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt';
45  // XCLASS ?
46  if (!$hasCorrectBaseClass && FALSE != get_parent_class($this->objectInstance)) {
47  $hasCorrectBaseClass = is_subclass_of($this->objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\PhpassSalt');
48  }
49  $this->assertTrue($hasCorrectBaseClass);
50  }
51 
55  public function nonZeroSaltLength() {
56  $this->assertTrue($this->objectInstance->getSaltLength() > 0);
57  }
58 
63  $password = '';
64  $this->assertNull($this->objectInstance->getHashedPassword($password));
65  }
66 
71  $password = 'a';
72  $this->assertNotNull($this->objectInstance->getHashedPassword($password));
73  }
74 
79  $password = 'password';
80  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
81  $this->assertTrue($this->objectInstance->isValidSaltedPW($saltedHashPassword));
82  }
83 
88  $password = 'password';
89  // custom salt without setting
90  $randomBytes = \TYPO3\CMS\Core\Utility\GeneralUtility::generateRandomBytes($this->objectInstance->getSaltLength());
91  $salt = $this->objectInstance->base64Encode($randomBytes, $this->objectInstance->getSaltLength());
92  $this->assertTrue($this->objectInstance->isValidSalt($salt));
93  $saltedHashPassword = $this->objectInstance->getHashedPassword($password, $salt);
94  $this->assertTrue($this->objectInstance->isValidSaltedPW($saltedHashPassword));
95  }
96 
101  $password = 'password';
102  $minHashCount = $this->objectInstance->getMinHashCount();
103  $this->objectInstance->setHashCount($minHashCount);
104  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
105  $this->assertTrue($this->objectInstance->isValidSaltedPW($saltedHashPassword));
106  // reset hashcount
107  $this->objectInstance->setHashCount(NULL);
108  }
109 
119  $password = 'aEjOtY';
120  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
121  $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
122  }
123 
133  $password = '01369';
134  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
135  $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
136  }
137 
147  $password = ' !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
148  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
149  $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
150  }
151 
161  $password = '';
162  for ($i = 160; $i <= 191; $i++) {
163  $password .= chr($i);
164  }
165  $password .= chr(215) . chr(247);
166  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
167  $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
168  }
169 
179  $password = '';
180  for ($i = 192; $i <= 214; $i++) {
181  $password .= chr($i);
182  }
183  for ($i = 216; $i <= 246; $i++) {
184  $password .= chr($i);
185  }
186  for ($i = 248; $i <= 255; $i++) {
187  $password .= chr($i);
188  }
189  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
190  $this->assertTrue($this->objectInstance->checkPassword($password, $saltedHashPassword));
191  }
192 
197  $password = 'password';
198  $password1 = $password . 'INVALID';
199  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
200  $this->assertFalse($this->objectInstance->checkPassword($password1, $saltedHashPassword));
201  }
202 
207  $pad = 'a';
208  $criticalPwLength = 0;
209  // We're using a constant salt.
210  $saltedHashPasswordCurrent = $salt = $this->objectInstance->getHashedPassword($pad);
211  for ($i = 0; $i <= 128; $i += 8) {
212  $password = str_repeat($pad, max($i, 1));
213  $saltedHashPasswordPrevious = $saltedHashPasswordCurrent;
214  $saltedHashPasswordCurrent = $this->objectInstance->getHashedPassword($password, $salt);
215  if ($i > 0 && $saltedHashPasswordPrevious === $saltedHashPasswordCurrent) {
216  $criticalPwLength = $i;
217  break;
218  }
219  }
220  $this->assertTrue($criticalPwLength == 0 || $criticalPwLength > 32, 'Duplicates of hashed passwords with plaintext password of length ' . $criticalPwLength . '+.');
221  }
222 
226  public function modifiedMinHashCount() {
227  $minHashCount = $this->objectInstance->getMinHashCount();
228  $this->objectInstance->setMinHashCount($minHashCount - 1);
229  $this->assertTrue($this->objectInstance->getMinHashCount() < $minHashCount);
230  $this->objectInstance->setMinHashCount($minHashCount + 1);
231  $this->assertTrue($this->objectInstance->getMinHashCount() > $minHashCount);
232  }
233 
237  public function modifiedMaxHashCount() {
238  $maxHashCount = $this->objectInstance->getMaxHashCount();
239  $this->objectInstance->setMaxHashCount($maxHashCount + 1);
240  $this->assertTrue($this->objectInstance->getMaxHashCount() > $maxHashCount);
241  $this->objectInstance->setMaxHashCount($maxHashCount - 1);
242  $this->assertTrue($this->objectInstance->getMaxHashCount() < $maxHashCount);
243  }
244 
248  public function modifiedHashCount() {
249  $hashCount = $this->objectInstance->getHashCount();
250  $this->objectInstance->setMaxHashCount($hashCount + 1);
251  $this->objectInstance->setHashCount($hashCount + 1);
252  $this->assertTrue($this->objectInstance->getHashCount() > $hashCount);
253  $this->objectInstance->setMinHashCount($hashCount - 1);
254  $this->objectInstance->setHashCount($hashCount - 1);
255  $this->assertTrue($this->objectInstance->getHashCount() < $hashCount);
256  // reset hashcount
257  $this->objectInstance->setHashCount(NULL);
258  }
259 
264  $password = 'password';
265  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
266  $this->assertFalse($this->objectInstance->isHashUpdateNeeded($saltedHashPassword));
267  }
268 
273  $password = 'password';
274  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
275  $increasedHashCount = $this->objectInstance->getHashCount() + 1;
276  $this->objectInstance->setMaxHashCount($increasedHashCount);
277  $this->objectInstance->setHashCount($increasedHashCount);
278  $this->assertTrue($this->objectInstance->isHashUpdateNeeded($saltedHashPassword));
279  // reset hashcount
280  $this->objectInstance->setHashCount(NULL);
281  }
282 
287  $password = 'password';
288  $saltedHashPassword = $this->objectInstance->getHashedPassword($password);
289  $decreasedHashCount = $this->objectInstance->getHashCount() - 1;
290  $this->objectInstance->setMinHashCount($decreasedHashCount);
291  $this->objectInstance->setHashCount($decreasedHashCount);
292  $this->assertFalse($this->objectInstance->isHashUpdateNeeded($saltedHashPassword));
293  // reset hashcount
294  $this->objectInstance->setHashCount(NULL);
295  }
296 
297 }
static generateRandomBytes($bytesToReturn)