Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Django SHA1 passwords

I need to provide access to a Django database from another python software based on the users already created in Django. I'm ok with the whole access thing, I just need a piece of code to generate a Django password as the admin auth module does.

What would be the best way of doing that? Note that, if possible, I don't want to have the whole Django package for that.

Many Thanks

like image 961
vmassuchetto Avatar asked Dec 05 '25 13:12

vmassuchetto


2 Answers

Got it how it works. I wrote a small function to check a user's password:

def check_passwd (user, raw_password):
     import hashlib
     # ... get 'hsh_passwd' from database based on 'user' ...
     hsh_passwd = hsh_passwd.split('$')
     salt = hsh_passwd[1]
     hsh = hsh_passwd[2]
     if hsh == hashlib.sha1(salt + raw_password).hexdigest():
         return True
     return False

Of course, there's not a lot of verifications and it's not flexible, but it's what I've been looking for.

like image 111
vmassuchetto Avatar answered Dec 08 '25 01:12

vmassuchetto


The entire code for creating and verifying user passwords is in django.contrib.auth.models.

Specifically, look at the methods User.set_password and User.check_password. You'd need to extract those two bits and the Django code they reference to create and verify the project's user passwords as per Django.

like image 20
Filip Dupanović Avatar answered Dec 08 '25 02:12

Filip Dupanović



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!