How to increase PBKDF2-SHA256 iterations to 600,000 in Django 4.2.2 without forcing migrations or breaking existing passwords?

I have a Django 4.2.2 / Python 3.11.4 application where employee passwords are stored using PBKDF2-SHA256. Currently the code uses a hardcoded default hash and a runtime helper that calls make_password("123456"):

python
# current example
EMP_DEFAULT_PASSWORD = (
    "pbkdf2_sha256$36000$Gj09deAAUqW7$ih9vyun8PPwxvbG1+bhxB0TIQF+2IUfdhiDUf0AS0o4="
)

PASSWORD_HASHERS = [
    "django.contrib.auth.hashers.PBKDF2PasswordHasher",
]

def default_employee_password():
    """Return a password hash using the configured current hasher."""
    return make_password("123456")

Security requirement: increase PBKDF2-SHA256 iterations to at least 600,000 (subject to performance testing).

Constraints and concerns:

  • Existing users have hashes in the DB generated with 36,000 iterations — they must continue to authenticate without a forced reset.

  • Changing the model field default from a hardcoded hash to a callable or removing it previously caused an AlterField migration; I want to avoid unnecessary/destructive migrations that rewrite DB defaults or risk locking users out.

    • I want new passwords to use the stronger iteration count, and to phase in the stronger hashing without forcing all users to reset.

    enter image description here

Django 4.2 already hashes with 600,000 iterations, so make_password("123456") already gives pbkdf2_sha256$600000$...

Old 36,000 hashes keep working because each hash stores its own iteration count. To upgrade them when the user logs in, pass a setter to check_password

def verify_employee_password(employee, raw):
    def upgrade(raw):
        employee.self_password = make_password(raw)
        employee.save(update_fields=["self_password"])
    return check_password(raw, employee.self_password, setter=upgrade)

Changing the default to default_employee_password is safe. Django applies defaults in Python, so the AlterField migration doesn't touch existing rows, and on PostgreSQL sqlmigrate shows it as a no-op.

Вернуться на верх