Course outline

PHP / Advanced: reliable models and runtime evidence / Lesson 21

Password hashing and rehashing

Verify synthetic PHP passwords and plan password-hash upgrades.

About 23 min read20 worked examples20 practice exercisesContent reviewed
On this page

Lesson goals

  • Use password-specific hashing and verification.
  • Plan a rehash policy and test failures.
  • Test verifier and rehash decisions without exposing secrets.
  • Design storage-failure and concurrency boundaries for credential migration.

Define the password contract

A password verification system must avoid storing plaintext credentials and must use a password-specific hashing mechanism with a deliberate cost policy. PHP’s password API stores algorithm and parameter information in the resulting hash so verification can use it. Use only synthetic credentials in demonstrations and tests, and never write real passwords or password hashes into logs.

Create a password hash

password_hash creates a hash using the requested algorithm and options. The first example hashes a fixed synthetic password with PASSWORD_DEFAULT and checks it with password_verify; the hash itself is not printed. The default algorithm can change across PHP releases, so choose storage that accommodates future hash lengths and review the deployment’s current policy rather than assuming a particular algorithm forever.

php Synthetic password example
<?php
declare(strict_types=1);

$hash = password_hash("synthetic-example", PASSWORD_DEFAULT);
var_export(password_verify("synthetic-example", $hash));
echo " ";
var_export(password_verify("wrong-example", $hash));
echo "\n";
Expected output
true false

The matching candidate verifies and the different candidate does not.

Verify a candidate

password_verify checks a candidate against a stored hash; it does not require storing a separate salt for the password API’s hash format. Test a correct and an incorrect synthetic candidate without comparing generated hashes for equality. A successful verification is only one authentication step: account status, rate limiting, and session handling remain separate application responsibilities.

Plan rehashing

password_needs_rehash lets an application detect whether a stored hash matches its current algorithm and options. After a successful verification, a system can create and store a replacement under its reviewed policy. The second example uses the same default policy for creation and inspection, so it reports no rehash needed. Treat storage update failure explicitly and do not invalidate a working credential silently.

php Synthetic password example
<?php
declare(strict_types=1);

$hash = password_hash("synthetic-example", PASSWORD_DEFAULT);
var_export(password_needs_rehash($hash, PASSWORD_DEFAULT));
echo "\n";
Expected output
false

The hash was created using the same policy being checked.

Test the policy

Test correct and incorrect synthetic passwords, a changed rehash policy, and storage failure with an isolated test double. Assert verification outcomes rather than exact hash bytes, since salts and implementation details can vary. Review algorithm availability and cost under the supported runtime, and measure the production-relevant workload before changing cost settings. Bound authentication work and retain separate protection against repeated attempts.

Treat the verifier as a protected credential

A password system stores a password-specific verifier rather than plaintext and checks a submitted candidate using the supported password API. password_hash creates a representation containing information needed by password_verify, including algorithm, parameters and salt in the supported format. A fresh salted representation can differ even for the same password, so comparing newly generated hash strings is not a reliable verification test. Keep passwords and verifiers out of logs, public errors and teaching output. Use synthetic credentials only in fixtures and assert matching and mismatching outcomes instead of exact generated bytes. A successful password_verify is one part of authentication, not proof that the account is active or the actor is authorized for a resource. Account status, rate limiting, recovery, session transitions and current authorization have separate owners and tests. No real account, password store or service is used in these examples.

Make algorithm, cost and storage policy explicit

PASSWORD_DEFAULT is designed to change as PHP evolves, so do not assume its current algorithm or output length is permanent. The PHP documentation recommends storage that can grow beyond 60 bytes and notes that 255 bytes is a good choice. Verify that the application’s actual database schema and encoding preserve the complete verifier; a truncated verifier must not be treated as a successful credential. Algorithm availability and accepted options depend on the installed runtime and configuration. Define the reviewed algorithm and cost policy for the deployment and measure representative authentication workload and resource limits in an authorized environment before changing cost. Do not invent a universal cost, latency or throughput figure from a local tutorial. The same policy must be understood by the verification and migration code. A synthetic local API test shows only the observed runtime and options; it is not a production sizing exercise.

Sequence verification, policy check and replacement

Rehash is a migration of a working verifier, not a substitute for authenticating the candidate. First verify the submitted synthetic candidate against the stored verifier. After success, password_needs_rehash can compare that representation with the application’s reviewed current algorithm and options. If a replacement is needed, create it and persist it using a storage operation with explicit failure and concurrency behavior. If the update fails, do not silently discard or overwrite the working credential; define whether authentication proceeds and how the migration is retried under the application’s security policy. Concurrent logins or password changes can make an old read stale, so use an appropriate conditional update or equivalent concurrency strategy in the real store. A local fake can model the decision and failure path but cannot establish atomicity of a production database. Test unchanged policy, changed policy, mismatch and storage failure independently.

Keep failures distinct and private

A wrong candidate, an invalid or unavailable verifier, an inactive account and a storage update failure are different internal events even when the public authentication response deliberately avoids revealing which occurred. Define a generic safe public outcome where appropriate and keep necessary redacted diagnostics in a protected channel. Do not replace an error with a successful empty credential record or print a hash to make a test easier. Verify the stored value is complete before trusting persistence, and test the mismatch path without performing rehash or a write. A successful verifier check also does not override an account-disabled rule or authorize a later action. Rate limiting and recovery protect different parts of the workflow and need their own tests. Follow the application’s approved response and security policy when real credential exposure is suspected; a synthetic model cannot establish that an entire login system is safe.

Test the lifecycle without exposing secrets

Build a test matrix with a synthetic verifier, correct and incorrect candidates, a matching policy, a changed policy and an injected storage failure. Check observable decisions and the retained working verifier rather than asserting exact salt-dependent hash bytes. Include an active-account control and a disabled-account denial where the product requires them; do not let a broken setup satisfy every negative test. For a failed replacement, verify the prior verifier still accepts the correct synthetic candidate in the fake store, and verify no secret is returned or logged. A rehash decision alone does not prove a replacement was persisted, nor does a fake store prove a real database update or concurrency behavior. Record runtime, algorithm availability and scope without exposing the generated hash. Runnable examples below use PHP 8.3-compatible syntax and were qualified on PHP 8.5.12 CLI with synthetic values only; no real credentials, files, sessions or services are accessed.

20 worked examples

1. Verified but inactive

Problem: Interpret this synthetic password-policy model: Verified but inactive.

  1. The synthetic candidate verifies, but the modeled account status is inactive.
  2. The combined gate denies the login; the local model does not exercise a real account.
php Synthetic CLI fixture; no real credential
<?php
$h = password_hash('synthetic-one', PASSWORD_DEFAULT); $verified = password_verify('synthetic-one', $h); $active = false;
echo $verified && $active ? "allow\n" : "deny\n";
Expected output
deny

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces deny.

Why this result follows: The synthetic candidate verifies, but the modeled account status is inactive. The combined gate denies the login; the local model does not exercise a real account.

2. Account-specific verifier

Problem: Interpret this synthetic password-policy model: Account-specific verifier.

  1. The same synthetic candidate is checked against two different account verifiers.
  2. It fails against A and succeeds against B; correct account selection is a separate boundary.
php Synthetic CLI fixture; no real credential
<?php
$a = password_hash('synthetic-a', PASSWORD_DEFAULT); $b = password_hash('synthetic-b', PASSWORD_DEFAULT);
echo password_verify('synthetic-b', $a) ? 'allow ' : 'deny '; echo password_verify('synthetic-b', $b) ? "allow\n" : "deny\n";
Expected output
deny allow

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces deny allow.

Why this result follows: The same synthetic candidate is checked against two different account verifiers. It fails against A and succeeds against B; correct account selection is a separate boundary.

3. Two verifiers same candidate

Problem: Interpret this synthetic password-policy model: Two verifiers same candidate.

  1. Two verifiers are generated independently for one synthetic candidate.
  2. Both can verify that candidate without comparing or printing their salted byte strings.
php Synthetic CLI fixture; no real credential
<?php
$a = password_hash('synthetic-one', PASSWORD_DEFAULT); $b = password_hash('synthetic-one', PASSWORD_DEFAULT);
echo password_verify('synthetic-one', $a) && password_verify('synthetic-one', $b) ? "both-verify\n" : "failure\n";
Expected output
both-verify

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces both-verify.

Why this result follows: Two verifiers are generated independently for one synthetic candidate. Both can verify that candidate without comparing or printing their salted byte strings.

4. Changed policy rehash check

Problem: Interpret this synthetic password-policy model: Changed policy rehash check.

  1. The stored synthetic verifier uses cost 10 while the comparison policy specifies 11.
  2. password_needs_rehash flags the policy difference without updating storage.
php Synthetic CLI fixture; no real credential
<?php
$h = password_hash('synthetic-one', PASSWORD_BCRYPT, ['cost'=>10]); echo password_needs_rehash($h, PASSWORD_BCRYPT, ['cost'=>11]) ? "rehash\n" : "current\n";
Expected output
rehash

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces rehash.

Why this result follows: The stored synthetic verifier uses cost 10 while the comparison policy specifies 11. password_needs_rehash flags the policy difference without updating storage.

5. Verification before migration

Problem: Interpret this synthetic password-policy model: Verification before migration.

  1. The synthetic candidate verifies before the local policy decision.
  2. The model reaches the rehash consideration without claiming an update occurred.
php Synthetic CLI fixture; no real credential
<?php
$h = password_hash('synthetic-one', PASSWORD_DEFAULT); $verified = password_verify('synthetic-one', $h); $considered = false;
if ($verified) { $considered = true; } echo $considered ? "considered\n" : "skipped\n";
Expected output
considered

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces considered.

Why this result follows: The synthetic candidate verifies before the local policy decision. The model reaches the rehash consideration without claiming an update occurred.

6. Mismatch skips migration

Problem: Interpret this synthetic password-policy model: Mismatch skips migration.

  1. The different synthetic candidate fails verification.
  2. The guarded model performs no replacement write for that mismatch.
php Synthetic CLI fixture; no real credential
<?php
$h = password_hash('synthetic-one', PASSWORD_DEFAULT); $verified = password_verify('wrong-synthetic', $h); $writes = 0;
if ($verified) { $writes++; } echo "writes=$writes\n";
Expected output
writes=0

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces writes=0.

Why this result follows: The different synthetic candidate fails verification. The guarded model performs no replacement write for that mismatch.

7. Account status separate

Problem: Interpret this synthetic password-policy model: Account status separate.

  1. The synthetic verifier result is true but account status is inactive.
  2. The local combined policy denies authentication despite the password result.
php Synthetic CLI fixture; no real credential
<?php
$verified = true; $active = false; echo $verified && $active ? "allow\n" : "deny\n";
Expected output
deny

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces deny.

Why this result follows: The synthetic verifier result is true but account status is inactive. The local combined policy denies authentication despite the password result.

8. Active account control

Problem: Interpret this synthetic password-policy model: Active account control.

  1. Both modeled verification and account status are true.
  2. The local gate permits this control without proving any real login.
php Synthetic CLI fixture; no real credential
<?php
$verified = true; $active = true; echo $verified && $active ? "allow\n" : "deny\n";
Expected output
allow

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces allow.

Why this result follows: Both modeled verification and account status are true. The local gate permits this control without proving any real login.

9. Authorization remains separate

Problem: Interpret this synthetic password-policy model: Authorization remains separate.

  1. The model has an authenticated context but denies the requested action.
  2. Password verification is not itself authorization for every resource.
php Synthetic CLI fixture; no real credential
<?php
$authenticated = true; $authorized = false; echo $authenticated && $authorized ? "allow\n" : "deny\n";
Expected output
deny

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces deny.

Why this result follows: The model has an authenticated context but denies the requested action. Password verification is not itself authorization for every resource.

10. No verifier output

Problem: Interpret this synthetic password-policy model: No verifier output.

  1. The local code uses the generated verifier internally.
  2. Only a deterministic decision is printed; the salted verifier is not exposed.
php Synthetic CLI fixture; no real credential
<?php
$h = password_hash('synthetic-one', PASSWORD_DEFAULT); $verified = password_verify('synthetic-one', $h); echo $verified ? "yes\n" : "no\n";
Expected output
yes

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces yes.

Why this result follows: The local code uses the generated verifier internally. Only a deterministic decision is printed; the salted verifier is not exposed.

11. Password omitted from diagnostic

Problem: Interpret this synthetic password-policy model: Password omitted from diagnostic.

  1. The synthetic diagnostic projection selects only the event field.
  2. The password field is absent; no real log or credential is accessed.
php Synthetic CLI fixture; no real credential
<?php
$input = ['event'=>'failed', 'password'=>'synthetic']; $log = ['event'=>$input['event']]; echo array_key_exists('password', $log) ? "leak\n" : "omitted\n";
Expected output
omitted

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces omitted.

Why this result follows: The synthetic diagnostic projection selects only the event field. The password field is absent; no real log or credential is accessed.

12. Generic public failure

Problem: Interpret this synthetic password-policy model: Generic public failure.

  1. The synthetic internal category is kept separate from the public record.
  2. The public output contains a generic failure code, not verifier details.
php Synthetic CLI fixture; no real credential
<?php
$internal = 'verifier-invalid'; $public = ['error'=>'authentication_failed']; echo $public['error'], "\n";
Expected output
authentication_failed

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces authentication_failed.

Why this result follows: The synthetic internal category is kept separate from the public record. The public output contains a generic failure code, not verifier details.

13. Truncated verifier rejected

Problem: Interpret this synthetic password-policy model: Truncated verifier rejected.

  1. The local fixture deliberately truncates the generated verifier.
  2. The correct synthetic candidate does not verify against that incomplete representation.
php Synthetic CLI fixture; no real credential
<?php
$h = password_hash('synthetic-one', PASSWORD_DEFAULT); $stored = substr($h, 0, 10);
echo password_verify('synthetic-one', $stored) ? "verified\n" : "rejected\n";
Expected output
rejected

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces rejected.

Why this result follows: The local fixture deliberately truncates the generated verifier. The correct synthetic candidate does not verify against that incomplete representation.

14. Fake storage success

Problem: Interpret this synthetic password-policy model: Fake storage success.

  1. The local fake is configured to accept the replacement marker.
  2. The model records updated state without writing a real verifier or database.
php Synthetic CLI fixture; no real credential
<?php
$store = ['verifier'=>'old-model']; $updateSucceeded = true;
if ($updateSucceeded) { $store['verifier'] = 'new-model'; } echo $store['verifier'] === 'new-model' ? "updated\n" : "unchanged\n";
Expected output
updated

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces updated.

Why this result follows: The local fake is configured to accept the replacement marker. The model records updated state without writing a real verifier or database.

15. Fake storage failure

Problem: Interpret this synthetic password-policy model: Fake storage failure.

  1. The local fake rejects the replacement attempt.
  2. The prior marker remains in the model; real persistence requires its own test.
php Synthetic CLI fixture; no real credential
<?php
$store = ['verifier'=>'old-model']; $updateSucceeded = false;
if ($updateSucceeded) { $store['verifier'] = 'new-model'; } echo $store['verifier'] === 'old-model' ? "retained\n" : "changed\n";
Expected output
retained

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces retained.

Why this result follows: The local fake rejects the replacement attempt. The prior marker remains in the model; real persistence requires its own test.

16. Stale update guard

Problem: Interpret this synthetic password-policy model: Stale update guard.

  1. The synthetic stored version changed after the read.
  2. The local conditional-update model refuses a stale replacement; it is not a database concurrency proof.
php Synthetic CLI fixture; no real credential
<?php
$readVersion = 1; $currentVersion = 2; $updates = 0; if ($readVersion === $currentVersion) $updates++;
echo "updates=$updates\n";
Expected output
updates=0

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces updates=0.

Why this result follows: The synthetic stored version changed after the read. The local conditional-update model refuses a stale replacement; it is not a database concurrency proof.

17. Current update guard

Problem: Interpret this synthetic password-policy model: Current update guard.

  1. The read and current synthetic versions match.
  2. The model permits one update, subject to its separately assumed policy conditions.
php Synthetic CLI fixture; no real credential
<?php
$readVersion = 2; $currentVersion = 2; $updates = 0; if ($readVersion === $currentVersion) $updates++;
echo "updates=$updates\n";
Expected output
updates=1

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces updates=1.

Why this result follows: The read and current synthetic versions match. The model permits one update, subject to its separately assumed policy conditions.

18. Rate limit separate

Problem: Interpret this synthetic password-policy model: Rate limit separate.

  1. The synthetic password check passes but the separate rate decision denies the attempt.
  2. The local gate illustrates independent workflow controls without implementing a limiter.
php Synthetic CLI fixture; no real credential
<?php
$verified = true; $rateAllowed = false; echo $verified && $rateAllowed ? "allow\n" : "deny\n";
Expected output
deny

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces deny.

Why this result follows: The synthetic password check passes but the separate rate decision denies the attempt. The local gate illustrates independent workflow controls without implementing a limiter.

19. Storage width model

Problem: Interpret this synthetic password-policy model: Storage width model.

  1. The synthetic policy requires capacity 255 while the modeled column provides 60.
  2. The model requests a schema review; it does not inspect a real database.
php Synthetic CLI fixture; no real credential
<?php
$requiredCapacity = 255; $columnCapacity = 60; echo $columnCapacity >= $requiredCapacity ? "adequate\n" : "review\n";
Expected output
review

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces review.

Why this result follows: The synthetic policy requires capacity 255 while the modeled column provides 60. The model requests a schema review; it does not inspect a real database.

20. Integration evidence gap

Problem: Interpret this synthetic password-policy model: Integration evidence gap.

  1. Only the synthetic local verifier case is marked passed.
  2. Storage and session behavior remain explicitly untested rather than inferred.
php Synthetic CLI fixture; no real credential
<?php
$evidence = ['cli'=>'passed', 'storage'=>'untested', 'session'=>'untested']; echo implode(',', $evidence), "\n";
Expected output
passed,untested,untested

Qualified on PHP 8.5.12 CLI using synthetic values; PHP 8.3-compatible syntax, not PHP 8.3 execution or a production authentication test.

Expected result: The local fixture produces passed,untested,untested.

Why this result follows: Only the synthetic local verifier case is marked passed. Storage and session behavior remain explicitly untested rather than inferred.

Common errors and fixes

A real password is logged

Why it happens
Authentication input was included in diagnostics.
How to fix it
Remove sensitive logging and follow the applicable response process.
Prevent it next time
Use synthetic test data.

A hash is truncated in storage

Why it happens
The storage width assumed a fixed algorithm output.
How to fix it
Use storage sized for the supported password API policy.
Prevent it next time
Review storage when the policy changes.

A valid credential is lost during rehash

Why it happens
A storage update failure was ignored.
How to fix it
Handle the failed update without silently replacing the working record.
Prevent it next time
Test storage failure with isolated data.

20 practice exercises

1. Why should tests avoid asserting an exact generated hash?

Show hint

Assert verification outcomes, not a salted byte string.

Show worked solution

Salt and implementation details can vary. Assert that the correct candidate verifies and an incorrect one does not.

Generated password verifiers include salt and may differ across executions or policy changes. Check that the correct synthetic candidate verifies and an incorrect candidate does not, without printing either the verifier or a real credential.

Check your solution

  • The correct synthetic candidate verifies.
  • The different candidate fails without exposing the verifier.

2. When is rehashing considered?

Show hint

Verify first, then inspect the reviewed policy.

Show worked solution

After successful verification, compare the stored hash with the current reviewed policy and update it if needed.

After successful password_verify, use password_needs_rehash with the application’s reviewed algorithm and options. If replacement is needed, handle persistence failure explicitly and preserve the working verifier according to policy; a mismatch must not be treated as an authenticated rehash opportunity.

Check your solution

  • Rehash is considered only after successful verification.
  • A failed storage update does not silently discard the working verifier.

3. A login policy requires all three conditions: password verified, account active and rate limit permits the attempt. Classify (true,true,true), (true,true,false) and (true,false,true), and design a no-effect check.

Show hint

Each condition is an independent required gate.

Show worked solution

Only the first combination permits the modeled login; the second is denied by rate policy and the third by account status despite successful password verification. Assert zero session-creation effects for both denials and test the permitted case separately without claiming a real login was exercised.

Check your solution

  • The outcomes are allow, deny and deny in order.
  • Both denied cases create no modeled session effect.

4. A faulty verification adapter always returns true. Design a synthetic test suite that detects it while avoiding exact hash or secret output.

Show hint

A negative control must fail verification rather than merely execute.

Show worked solution

Create a synthetic verifier and assert that its correct candidate returns true and a different candidate returns false. Run the same tests against the deliberately always-true adapter: the mismatch assertion must fail. Record only pass or failure, never candidate or verifier bytes.

Check your solution

  • The wrong candidate causes the always-true adapter to fail the test.
  • The correct candidate verifies and no secret is printed.

5. A database column stores only 60 characters and truncates a verifier returned by the current policy. Design a storage round-trip test and remediation.

Show hint

The entire verifier representation must survive storage.

Show worked solution

Choose capacity accommodating the documented evolving PASSWORD_DEFAULT representation, commonly 255 bytes, and verify exact storage and retrieval without printing it. Test the retrieved verifier with the correct synthetic candidate; handle existing truncation through the approved account recovery process.

Check your solution

  • The stored representation is not truncated.
  • The retrieved verifier accepts the correct synthetic candidate.

6. The stored verifier was created under an older reviewed policy and the correct candidate verifies. Define the rehash sequence and a no-change control.

Show hint

Verification precedes the migration decision.

Show worked solution

After successful verification, check password_needs_rehash against the current reviewed algorithm and options. Create and safely persist a replacement only if needed; with a matching policy, verify that no replacement write occurs.

Check your solution

  • A changed policy triggers the documented replacement path after verification.
  • A matching policy does not write a replacement.

7. A wrong synthetic candidate arrives while the stored verifier needs an upgrade. Determine whether to create a replacement and define two assertions.

Show hint

A policy mismatch does not authenticate the candidate.

Show worked solution

Reject the candidate and do not create or store a replacement based on that failed verification. Assert the authentication result is denied and the fake store remains unchanged; handle attempts and diagnostics under the separate security policy.

Check your solution

  • The incorrect candidate is denied.
  • No replacement write occurs on mismatch.

8. A verified candidate needs rehash, but a fake storage update fails. Define what to preserve and how to test without a real database.

Show hint

The old working verifier must not be silently lost.

Show worked solution

Keep the prior fake-store verifier when the replacement write fails and return or report the documented failure according to policy. Verify that the prior synthetic credential still matches afterward and that the failure is observable without printing either verifier.

Check your solution

  • The fake store retains its prior verifier.
  • The update failure remains visible to the owning layer.

9. Two rehash workers read verifier version 1 after successful synthetic verification. Worker A conditionally stores version 2; worker B then attempts its version-1 update. Define B’s decision and a recovery test.

Show hint

Both workers started from the same old state, but only one can still match it.

Show worked solution

After A commits version 2, B’s conditional update for version 1 must fail and must not overwrite A’s result. B should re-read and follow the documented current-state policy rather than blindly retrying the stale write; test one successful update and one rejected stale update, while verifying real atomicity separately.

Check your solution

  • A’s update succeeds once and B’s stale update does not overwrite version 2.
  • The recovery path re-reads current state under the documented policy.

10. A rehash replacement is stored, but verification of the retrieved value has not been tested. Define an acceptance check and rollback consideration.

Show hint

A write acknowledgment is not a credential round-trip test.

Show worked solution

Retrieve the replacement in an isolated store and verify the same synthetic candidate against it without printing the hash. Also test failed persistence and the policy for retaining or restoring the prior working verifier; do not claim a real database transaction from a fake.

Check your solution

  • The retrieved replacement verifies the synthetic candidate.
  • The failure path preserves or restores the working credential under policy.

11. A login verifies the password but has no rate-limiting or recovery policy. Explain what the local API check establishes and two separate requirements.

Show hint

Verifier correctness is only one part of authentication.

Show worked solution

The local check establishes password_verify behavior for the synthetic inputs and runtime only. Define and test a rate-limiting policy and account-recovery flow separately, along with account status and session handling; do not claim a complete authentication design from the hash test.

Check your solution

  • The claim is limited to synthetic verifier behavior.
  • Rate limiting and recovery remain separate requirements.

12. A public login error exposes whether the stored verifier was malformed or the candidate was wrong. Design a safer public response and protected diagnostics.

Show hint

Public feedback and internal classification serve different audiences.

Show worked solution

Return the application’s documented generic authentication failure for both cases without exposing verifier details. Keep necessary redacted diagnostic context protected, and test that neither path authenticates; do not claim this alone eliminates every timing or enumeration channel.

Check your solution

  • Both failures use the generic public outcome.
  • No verifier or candidate appears in public output.

13. A proposed password algorithm is available in one CLI build but its availability on the web worker is unknown. Define the compatibility evidence.

Show hint

Runtime interface and algorithm capability must both be observed.

Show worked solution

Record the actual CLI and web-worker PHP versions and relevant password algorithm availability under an authorized isolated check. Test creation and verification with synthetic values on the intended interface; a CLI pass does not prove the web worker supports the same policy.

Check your solution

  • The web-worker capability is observed independently.
  • Only synthetic candidates are used in tests.

14. A team proposes increasing password-hash cost based on one laptop measurement. Design a controlled capacity review without asserting a universal cost.

Show hint

Measure the intended workload and resource budget.

Show worked solution

Review available algorithms and options on the target runtime, then measure representative concurrent authentication load and resource limits in an authorized isolated environment. Record variability and operational constraints, and choose the policy with owners; do not infer production latency from one laptop run.

Check your solution

  • The target-equivalent workload and resource limits are measured.
  • No universal cost or timing is claimed.

15. Build a local rehash test matrix for matching policy, changed policy, wrong candidate and failed storage update. Specify the expected write behavior.

Show hint

Only a verified candidate can enter the migration decision.

Show worked solution

Expect no replacement for matching policy, one attempted replacement for a verified candidate under changed policy, none for a wrong candidate, and preservation of the old fake-store verifier when the update fails. Test each with fresh synthetic state.

Check your solution

  • Wrong candidates and matching policy cause no replacement write.
  • A failed replacement retains the prior working verifier.

16. A design proposes plaintext storage or a fast general-purpose hash for passwords. Explain the boundary and select the appropriate API family.

Show hint

Password verifiers need a password-specific construction and policy.

Show worked solution

Reject plaintext or a custom fast-hash design for stored passwords. Use PHP’s password_hash and password_verify with a reviewed algorithm and cost policy, and separately define storage protection, rate limiting and recovery; the API call alone is not a complete login design.

Check your solution

  • Plaintext and custom fast-hash storage are not accepted.
  • Password-specific APIs and separate workflow controls are specified.

17. A fake store returns a truncated or malformed verifier. Define the authentication outcome and a safe diagnostic boundary.

Show hint

An invalid representation is not a successful empty credential.

Show worked solution

Do not authenticate when verification fails; report the generic documented failure and keep necessary redacted diagnostics protected. Test a valid stored verifier as a control and follow the approved recovery process for a damaged real record, without exposing its contents.

Check your solution

  • The malformed verifier does not authenticate.
  • The public outcome contains no verifier details.

18. A test log records event ID, synthetic password, verifier and outcome. Redesign the log projection and specify two assertions.

Show hint

A test fixture should not normalize secret logging.

Show worked solution

Keep an approved event identifier and non-sensitive outcome, and exclude the password and verifier from the projected log. Assert both sensitive keys are absent and the event remains identifiable; apply the actual organization’s protected logging policy in deployment.

Check your solution

  • Password and verifier fields are absent.
  • The safe event and outcome remain available.

19. An old verifier still works, but the runtime upgrade changes the reviewed default policy. Define a safe migration plan and a rollback boundary.

Show hint

Do not discard the working verifier before replacement is safely stored.

Show worked solution

On successful synthetic verification, check the current policy, create a replacement if required and persist it with explicit failure and concurrency handling. Keep the prior valid record according to the approved recovery plan until replacement is confirmed; test target runtime compatibility without exposing hashes.

Check your solution

  • The old working credential is not silently lost on update failure.
  • The replacement is verified and stored under the reviewed policy.

20. A local fake store passes correct, wrong and rehash tests on PHP 8.5.12 CLI. Write the conclusion and name remaining integration gaps.

Show hint

A fake store and CLI are limited evidence.

Show worked solution

Report the exact synthetic cases and runtime that passed, and state that real storage atomicity, concurrent updates, HTTP, session rotation and rate limiting were not tested by that run. Plan authorized isolated integration tests for the required boundaries rather than claiming a secure production login.

Check your solution

  • The report identifies PHP 8.5.12 CLI and the fake store.
  • Untested storage, session and HTTP boundaries remain explicit.

Key takeaways

  • Use password-specific hashing and verification.
  • Review rehash policy and storage failures.
  • Use password_verify with the stored verifier.
  • Consider rehash only after successful verification under an explicit policy.
  • A local fake does not prove a complete authentication system.

Official references