-1

i want to create an application that admin can make user with a new messages that encrypted into database. I have the core function and all can work properly. But, i still can't encrypt the message from text to md5/sha1. I have read and try this and this. But, i still can't do it. I'm new in django, and I'm very grateful for your response. Thank you

This is my models:

class UserProfile(models.Model):
    user = models.OneToOneField(User) #digunakan untuk relasi ke model User (default) alias UserProfile adalah sebagai extending model
    CATEGORY_CHOICES = (
        ('admin','Admin'),
        ('user','User'),
        )
    hak_akses = models.CharField(max_length=100, choices = CATEGORY_CHOICES) 
    messages = models.CharField(max_length=100, blank=True)
    # password_pckelas = models.CharField(max_length=100, blank=True)

    # Override the __unicode__() method to return out something meaningful!
    def __unicode__(self):
        return self.user.username

This is my view function:

def tambah_user(request, template_name='form_user.html'):
    #cek session
    if 'username' in request.session and request.session['hak_akses'] == 'admin':
        #ambil dari database untuk mengaktifkan ubah_password_admin
        users = User.objects.all()
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        messages = profile_form.data['messages'] #ambil messages
        if request.method == 'POST':
            if user_form.is_valid() and profile_form.is_valid():
                #i want hash messages in here and then save into database
                user = user_form.save()
                user.set_password(user.password)
                user.save()
                profile = profile_form.save(commit=False)
                profile.user = user
                profile.save()
                return redirect('manajemen_user')
            else:
                print user_form.errors, profile_form.errors
        else:
            user_form = UserForm()
            profile_form = UserProfileForm()

        data = {
                'user_form': user_form,
                'profile_form': profile_form,
                'object_list': users
        }

        return render(request, template_name, data)
    #jika session tidak ada
    else:
        return HttpResponseRedirect('/simofa/logout')

This is my html templates:

<form name="tambah_user" class="form-horizontal style-form" method="POST">{% csrf_token %}
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">Nama User</label>
              <div class="col-sm-10">
                <!-- <input type="text" class='form-control'> -->
                {{ user_form.username }}
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">Password</label>
              <div class="col-sm-10">
                {{ user_form.password }}
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">Messages</label>
              <div class="col-sm-10">
                {{ profile_form.messages }}
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 col-sm-2 control-label">Hak Akses</label>
              <div class="col-sm-10">
                {{ profile_form.hak_akses }}
                <br><br><br>
                <button type="submit" class="btn btn-theme">Submit</button>
              </div>
            </div>
          </form>
Community
  • 1
  • 1
ranggatry
  • 125
  • 1
  • 15

2 Answers2

2

Hashing is not encrypting!

A hash is a function that converts one message to another. Consider a hash function (LAME_HASH) that just takes the count of the number of characters in a string.

LAME_HASH("TOOMANYSECRETS")
>>> 13
LAME_HASH("SETECASTRONOMY")
>>> 13

If I tell you my string is "TOOMANYSECRETS" and the hash was 10, you can immediately tell I am lying. But, if I tell you the hash is 13, you can't verify what the message is, it could be "TOOMANYSECRETS" or "SETECASTRONOMY". While this all seems trivial, this is essentially how hashing mechanisms like MD5 and SHA work.

There is no "decryption" of hashes, and if anyone tells you otherwise they are lying, or the entire security world would collective explode.

  • Thank you very much for your explanation. I'm sorry i was wrong in my question. But, can you help me how to encrypt plain text to md5/sha1 ? or do you have found another similiar question like this? I'm very grateful for your response sir :) – ranggatry Mar 16 '15 at 02:31
  • You can't encrypt plain text using MD5 or SHA. I just explained that. –  Mar 16 '15 at 02:34
2

i can encrypt plain text to the md5 / sha1 with this

import hashlib
print hashlib.md5('test').hexdigest()

and that is the answer I was looking for

ranggatry
  • 125
  • 1
  • 15