Sanket Ray
Sanket Ray

Reputation: 1141

Generate Django Password

In my PostgreSQL, there is a password column, which has passwords stored in the format

argon2$argon2i$v=19$m=512,t=2,p=2$Z1BlYkltRjNhakFE$1cgxZNRetdjoCYwBrjAICA

Is there a way to generate this password manually? I am not a backend developer and hence not much idea about how these things are usually handled.

Thanks

Upvotes: 0

Views: 1513

Answers (2)

ruddra
ruddra

Reputation: 51978

You can use make_password function:

from django.contrib.auth.hashers import make_password

make_password('abc123')

But if you want to change password for a user, then use this(copy pasted from documentation):

>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='john')
>>> u.set_password('new password')
>>> u.save()

Adding the below code in the Setting.py will help generate Argon2 passwords:

PASSWORD_HASHERS = [
'django.contrib.auth.hashers.Argon2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
]

You can install argon by pip install django[argon2]

Upvotes: 3

elvonkh
elvonkh

Reputation: 107

You can also use the built in function https://docs.djangoproject.com/en/stable/topics/auth/customizing/#django.contrib.auth.models.BaseUserManager.make_random_password

for user in new_users:
    password = User.objects.make_random_password()
    user.set_password(password)

Also notice that make_random_password() accepts keyword arguments length and allowed_chars

Upvotes: 1

Related Questions