0

I need help in this. I want to encrypt password before assigning it to jquery variable then send to php controller via ajax. Unfortunately I cant insert the password(which is jquery variable) into php hash function. Any help will be appreciated.I want the 'password' inside hash function to be the value I received in the var password. This is the code snippet:

$(document).ready(function() {
    $("#login").click(function() {
        var username = $("#username").val();
        var password = $("#password").val();
        var encPass = '<?php echo hash("SHA512", "password"); ?>'
    });
});
tomsseisums
  • 13,168
  • 19
  • 83
  • 145
Kizangila
  • 11
  • 3
  • You cannot pass the values to PHP like this because the values are only available on the client side. Why don't you hash the password with jQuery? E.g.: https://github.com/alexweber/jquery.sha256 – t.h3ads Nov 19 '14 at 08:50
  • Then you need to write your JavaScript code inside PHP tag. – Domain Nov 19 '14 at 08:54

1 Answers1

1

You can't do this, because firstly, php script work on the server. After php script has been done, javascript will work on the client side.

You need to use only javascript code to make hash if you don't want to send password publicly.

For example:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha512.js"></script>
<script>
    var hash = CryptoJS.SHA512("Message");
</script>

For your case:

$(document).ready(function(){
   $("#login").click(function(){
        var username = $("#username").val();
        var password = $("#password").val();
        var encPass = CryptoJS.SHA512(password);
   });
});