Password encryption

419 views
Skip to first unread message

Denis Bahati

unread,
Nov 4, 2009, 11:53:25 PM11/4/09
to django...@googlegroups.com
Hi All,
Can anyone show me how to encrypt password in django/python?

Kenneth Gonsalves

unread,
Nov 5, 2009, 12:04:09 AM11/5/09
to django...@googlegroups.com
On Thursday 05 Nov 2009 10:23:25 am Denis Bahati wrote:
> Hi All,
> Can anyone show me how to encrypt password in django/python?
>
http://docs.python.org/library/hashlib.html
--
regards
Kenneth Gonsalves
Senior Project Officer
NRC-FOSS
http://nrcfosshelpline.in/web/

Grant Livingston

unread,
Nov 5, 2009, 2:33:04 AM11/5/09
to django...@googlegroups.com
If you are using django.core.auth then it's done by default.

Denis Bahati

unread,
Nov 5, 2009, 2:51:32 AM11/5/09
to django...@googlegroups.com
My project require to have my own table for users and roles. Am not using the default auth table. It becomes a problem on how i can authenticate and encrypt the password as well as maintain the session of logged in users.
Thanks in advance.
Regards
Denis.

Vany

unread,
Nov 5, 2009, 5:40:15 PM11/5/09
to Django users
You can use the same method that django uses I've tried it and it
works
in your model inside the class for example users and after the
definitions of the fields
write


def save(self):
raw_password = self.password
import random
algo = 'sha1'
salt = get_hexdigest(algo, str(random.random()), str
(random.random()))[:5]
hsh = get_hexdigest(algo, salt, raw_password)
password = '%s$%s$%s' % (algo, salt, hsh)
self.password = password
super(yourclass,self).save()


or there is another method like this.

but I don't like so much

def _get_ssn(self):
enc_obj = Blowfish.new( settings.SECRET_KEY )
return u"%s" % enc_obj.decrypt( binascii.a2b_hex
(self.password) ).rstrip()

def _set_pass(self, ssn_value):
enc_obj = Blowfish.new( settings.SECRET_KEY )
repeat = 8 - (len( ssn_value ) % 8)
ssn_value = ssn_value + " " * repeat
password = binascii.b2a_hex(enc_obj.encrypt( ssn_value ))
return password

sspass = property(_get_ssn)

def save(self):
self.password = self._set_pass(self.password)
super(Usuario,self).save()


with the last method you need to add this from Crypto.Cipher import
Blowfish
and you have to install Crypto.Cipher.

I hope you solve your problem

Gabriel Gunderson

unread,
Nov 15, 2009, 11:50:47 PM11/15/09
to django...@googlegroups.com
On Thu, Nov 5, 2009 at 12:51 AM, Denis Bahati <djm...@gmail.com> wrote:
> My project require to have my own table for users and roles. Am not using
> the default auth table.

Does this work for your additional user info?

http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

Gabe

Denis Bahati

unread,
Nov 16, 2009, 3:31:53 AM11/16/09
to django...@googlegroups.com
Nah;
Here is my Model.
class Title(models.Model):
    name = models.CharField(max_length=10)
   
    def __unicode__(self):
        return self.name

class UserProfile(models.Model):
    title = models.ForeignKey(Title)
    address = models.TextField()
    date_added = models.DateTimeField()

class UserProfileForm(ModelForm):
    username=forms.CharField(label=("User Name"), max_length=100)
    password_Confirm=forms.CharField(label=("Confirm Password"), widget=forms.PasswordInput,max_length=100)
    first_name=forms.CharField(label=("First Name"), max_length=100)
    last_name=forms.CharField(label=("Last Name"), max_length=100)
    date_added=forms.DateField()
    class Meta:
        model = UserProfile

here is my view.py

from django.contrib.auth import authenticate, login
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.shortcuts import get_list_or_404
from django.template import loader, Context
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from commTrack.commtrack.models import *

def UserProfileEditor(request, id=None):
    form = UserProfileForm(request.POST or None,
                       instance=id and UserProfile.objects.get(id=id))

    # Save new/edited User
    if request.method == 'POST' and form.is_valid():
        form.save()
        return HttpResponseRedirect('/commTrack/userProfile/list/')

    return render_to_response('userProfile/adduserProfile.html', {'form':form})

and here is my form.html

<form {% if has_file_field %}enctype="multipart/form-data" {% endif %} action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% block form_top %}{% endblock %}
<div>
    <table align="center" cellpadding="1" cellspacing="2" bgcolor="" width="60%">
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.title.errors }}       
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_title">Title:</label>
            </div>
            </td><td>
            <div class="fieldWrapper">
                {{ form.title }}
            </div>
        </td></tr>
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.first_name.errors }}
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_first_name">First Name:</label>
            </div>
            </td><td>
            <div class="fieldWrapper">
                {{ form.first_name }}
            </div>
        </td></tr>
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.last_name.errors }}
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_last_name">Last Name:</label>
            </div>
            </td><td>
            <div class="fieldWrapper">
                {{ form.last_name }}
            </div>
        </td></tr>
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.address.errors }}
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_address">Address:</label>
            </div>
        </td><td>
            <div class="fieldWrapper">
                {{ form.address }}
            </div>
        </td></tr>
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.username.errors }}
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_username">User Name:</label>
            </div>
            </td><td>
            <div class="fieldWrapper">
                {{ form.username }}
            </div>
        </td></tr>
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.password.errors }}
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_password">Password:</label>
            </div>
            </td><td>
            <div class="fieldWrapper">
                {{ form.password }}
            </div>
        </td></tr>
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.password_Confirm.errors }}
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_password_Confirm">Confirm Password:</label>
            </div>
            </td><td>
            <div class="fieldWrapper">
                {{ form.password_Confirm }}
            </div>
        </td></tr>
        <tr><td colspan="2">
            <div class="fieldWrapper">
                {{ form.date_added.errors }}
            </div>
        </td></tr>
        <tr><td>
            <div class="fieldWrapper">
                <label for="id_date_added">Date Added:</label>
            </div>
            </td><td>
            <div class="fieldWrapper">
                {{ form.date_added }}
            </div>
        </td></tr>
        <tr><td>
            </td>
            <td>
                <input type="submit" value="Register" />
            </td>
        </tr>
    </table>
    </form>

It does not save anything when i click the submit button and it remains on the same interface. Any ideas?


--

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=.



Reply all
Reply to author
Forward
0 new messages