How can I add a client side JavaScript hashing function? I have created this function :
<script type = "text/javascript">
function myHash(){
var password = document.getElementById("password")
var hash = '';
for(i=0;i<password.length;i++){
var temp = password.charAt(i).charCodeAt(0);
temp = Math.pow(temp,5)%14;
hash += String.fromCharCode(temp);
}
return hash;
}
</script>
Put this file in polls/static/polls folder.
Following is the HTML form :
<HTML>
<HEADER>
{% load static %}
<script type="text/javascript src = '{% static 'hashing.js' %}'"></script>
<link rel="stylesheet" href="../../static/css/css.css">
<TITLE>LOGIN</TITLE>
<h1>POLLING APP</h1>
</HEADER>
<BODY>
<h1>USER LOGIN :-</h1>
<br />
<br />
<br />
<FORM id="login_form" method="post" action="/login/user_login/" onsubmit="return myHash()">
{% csrf_token %}
Username:
<input type="text" name="username" value="" id="username" size="50" />
<br />
<br />
Password:
<input type="password" name="password" value="" id="password" size="50" />
<br />
<br />
<INPUT type="submit" name="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
The polls/views.py file is as follows :
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from .models import Question, Choice
from django.urls import reverse
from django.template import RequestContext
from django.contrib.auth import authenticate, login, logout
#from django.template import loader
#def index(request):
# return HttpResponse("Hello world! you are at the polls index")
# Create your views here.
def index(request):
print(request.user)
latest_question_list = Question.objects.order_by('-pub_date')#[:5]
# template=loader.get_template('polls/index.html'
context={'latest_question_list':latest_question_list}
return render(request,'polls/index.html',context)
def detail(request, question_id):
#return HttpResponse("You're looking at question %s." % question_id)
question=get_object_or_404(Question,pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
question=get_object_or_404(Question,pk=question_id)
return render(request,'polls/results.html',{'question':question})
def vote(request, question_id):
print('something happened!')
question=get_object_or_404(Question,pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except(KeyError):
return render(request,'polls/detail.html',{'question':question,
'error_message':'you didn\'t select a choice'})
else:
selected_choice.votes+=1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
def login(request):
return render(request,'polls/login.html')
def user_login(request):
context = RequestContext(request)
print(context)
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
print([username,password])
user = authenticate(username=username, password=password)
a=user.is_superuser
if user is not None:
if user.is_active:
login(request)
# Redirect to index page.
return HttpResponseRedirect("/login/polls/")
else:
# Return a 'disabled account' error message
return HttpResponse("You're account is disabled.")
else:
# Return an 'invalid login' error message.
print ("invalid login details " + username + " " + password)
#return render_to_response('login.html', {}, context)
return HttpResponse('some shit happenned1')
else:
# the login is a GET request, so just show the user the login form.
#return render_to_response('login.html', {}, context)
return HttpResponse('some shit happenned !!!')
def user_logout(request):
logout(request)
return HttpResponseRedirect('/login/')
I need to secure the password at client side using the myHash() function. I want the server to receive the encrypted password, decrypt it (I have the decryption function as well.) and authenticate the username and password.