Migrating legacy password hashes to bcrypt on login
passwordsbcryptphppassword_hashmigrationrollback
Old apps often store sha256(password + salt). You cannot convert those to bcrypt in bulk
because you do not have the passwords, but you can upgrade each account the next time its owner
logs in. TLDR did this in v1.31.0 (July 2026). It works well; its one sharp edge is that it is a
one-way migration, which changes how you are allowed to roll back.
The opportunistic upgrade
function passwordMatches(array $row, string $pw): bool {
if ($row['pass_salt'] === '') { // already bcrypt
return password_verify($pw, $row['password']);
}
$legacy = hash('sha256', $pw . $row['pass_salt']);
return hash_equals($row['password'], $legacy); // constant-time
}
// on successful login with a legacy row:
// store password_hash($pw, PASSWORD_DEFAULT) in the hash column and set pass_salt to ''
- Mark migrated rows unambiguously (here
pass_salt = ''), so verification picks the right path. - New accounts and password changes write bcrypt directly.
- Use
hash_equalsfor the legacy comparison, never==.
The one-way door
After the deploy, every user who logs in gets a bcrypt hash. The previous API version only knows sha256, so rolling the API back locks those users out. Treat any auth-storage migration as a point you cannot blind-rollback past: fix forward instead, or ship the rollback build with the new verification code. Record such points next to the deploy instructions.
Change-password details
POST /account/passwordwith{current_password, new_password}, identity from the token only, re-verify the current password with the samepasswordMatches()(legacy or bcrypt), reject with 403 and a reason code on mismatch.- Length rule 6 to 72 characters: bcrypt silently truncates input at 72 bytes, so longer passwords give a false sense of strength.
- Delete pending reset tokens for that user when the password changes.
- Existing JWTs on other devices stay valid (stateless tokens) unless you add revocation.
- If the password also wraps encryption keys (E2E messaging), re-wrap them in the same flow. See E2E key management that never loses a key (keyring, backup, re-wrap).
- Admin resets done directly in the DB should use the server's own PHP to hash
(
php -r 'echo password_hash(...);') so the format matches what the app verifies.
Change notifications and testing
- Send a "your password was changed" email after both a settings change and a reset-link confirm. Make it best-effort (catch everything, skip invalid addresses) and say when and how it changed, plus what to do if it was not you.
- Test email flows end to end on production with a temporary user whose address is a
+tagalias. Check the mail log forstatus=sentand the inbox, then walk the cases: wrong token 401, right token 201, reused token 401, old password 401, new password 201. Delete the reset row before the user (foreign key). Example: TLDR posts #1796 and #1815.
See also
- API authorization from the verified identity (and the holes it closes)
- Release-directory deploys with an atomic symlink swap