Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly encrypt a password string in Django without an User Model?

Tags:

python

django

Based on my current Django app settings, is there a function or a snippet that allows me to view the encrypted password, given a raw string? I am testing some functionality and this would be useful for me.

I am looking for something like:

password = encrypt_raw_password("abcdef")
like image 522
linkyndy Avatar asked Jan 08 '14 13:01

linkyndy


3 Answers

There is a small util function just for that: make_password.

like image 200
mariodev Avatar answered Oct 19 '22 06:10

mariodev


You can make use of Django auth hashers:

from django.contrib.auth.hashers import make_password

password = make_password('somepass@123')

The version of Django should be 1.8 and above. I have tested in the latest version Django 3+

like image 20
user3785966 Avatar answered Oct 19 '22 07:10

user3785966


An update on this question since the previous answer does not seem to be supported.

import crypt
# To encrypt the password. This creates a password hash with a random salt.
password_hash = crypt.crypt(password)

# To check the password.
valid_password = crypt.crypt(cleartext, password_hash) == password_hash

Source: https://docs.python.org/2/library/crypt.html

like image 32
Javier Carmona Avatar answered Oct 19 '22 05:10

Javier Carmona