function LegacyPasswordHashingTest::testPasswordRehashing

Same name and namespace in other branches
  1. 11.x core/modules/phpass/tests/src/Unit/LegacyPasswordHashingTest.php \Drupal\Tests\phpass\Unit\LegacyPasswordHashingTest::testPasswordRehashing()

Tests password rehashing.

@covers ::__construct
@covers ::hash
@covers ::getCountLog2
@covers ::check
@covers ::needsRehash

File

core/modules/phpass/tests/src/Unit/LegacyPasswordHashingTest.php, line 104

Class

LegacyPasswordHashingTest
Unit tests for password hashing API.

Namespace

Drupal\Tests\phpass\Unit

Code

public function testPasswordRehashing() : void {
  // Increment the log2 iteration to MIN + 1.
  $password_hasher = new PhpassHashedPassword(PhpassHashedPassword::MIN_HASH_COUNT + 1);
  $this->assertTrue($password_hasher->needsRehash($this->hashedPassword), 'Needs a new hash after incrementing the log2 count.');
  // Re-hash the password.
  $rehashed_password = $password_hasher->hash($this->password);
  $this->assertSame(PhpassHashedPassword::MIN_HASH_COUNT + 1, $password_hasher->getCountLog2($rehashed_password), 'Re-hashed password has the correct number of log2 iterations.');
  $this->assertNotEquals($rehashed_password, $this->hashedPassword, 'Password hash changed again.');
  // Now the hash should be OK.
  $this->assertFalse($password_hasher->needsRehash($rehashed_password), 'Re-hashed password does not need a new hash.');
  $this->assertTrue($password_hasher->check($this->password, $rehashed_password), 'Password check succeeds with re-hashed password.');
  $this->assertTrue($this->passwordHasher
    ->check($this->password, $rehashed_password), 'Password check succeeds with re-hashed password with original hasher.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.