3

I need to generate an encrypted password that can be used within Django in a simple python script. What package/module would I need to import to achieve that? I am looking for something like:

from django.auth import create_password_from_string

django_password = create_password_from_string('coolpassword')

The background is, that I have a PHP application on server1 which should share the accounts with a Django application on server2. When an account is created in the PHP application, I want to fire up a python script that generates the encrypted password for the django application and transfers it to server2.

Of course, creating the encrypted string by PHP would be much better for this usecase, but I bet its even harder to achieve.

1

2 Answers 2

2

What you're looking for is make_password.

Basic usage:

Simply pass your plain-text password, it returns the hash you need.

Furthermore, according to the project reqs and specs, you can set the algorithm on the server2 app: just pass also the hasher parameter (note: in Django, the default is PBKDF2).

Environment requisites:

First of all, if you're new to Python my advice is to take a look to the PYTHONPATH env var, used to "tell" to the interpreter where he has to search. Then, using Django, if you want you can define which settings file the running instance has to point to. This is accomplished with another env var, the DJANGO_SETTINGS_MODULE, and for django-admin it can be set also inline:

django-admin runserver --settings=server2.settings

make_password example:

Assuming that your Python/Django env/path variables are properly configured, you have:

$ python manage.py shell
>>> from django.contrib.auth.hashers import make_password
>>> make_password('password', 'generate something random', 'pbkdf2_sha256')

Result:

'pbkdf2_sha256$36000$generate something random$0F6M8tj8Y65YXuUgfRkDAwYqrky6Ob5JN+HLPO+6Ayg='
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, looks good, but I tried it and got error django.core.exceptions.ImproperlyConfigured: Requested setting PASSWORD_HASHERS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings I tried to use the code under The default for PASSWORD_HASHERS is: on the linked page, but it also does not work
thanks for your help but I found a PHP solution, sorry that I misguided with my question for python
This issue is related to the environment configuration: for example you can set the DJANGO_SETTINGS_MODULE (see docs.djangoproject.com/en/dev/topics/settings/…)... But It could be better if you give us more details (for example tha Django version, what happens if you try to access the shell, ...)
I'm happy you've found a solution, anyway I edited my answer adding the information I think you miss, so next time you'll be fine with Python :)
1

I actually found an easy approach for PHP, which suits better for my usecase:

$password = 'password'
$salt = 'generate something random'
$iterations = 120000
$hash = base64_encode(hash_pbkdf2 ( 'sha256' , $password , $salt , $iterations, 32, true));

$django_password = 'pbkdf2_256'. '$' . $iterations . '$' . $hash;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.