How to convert password into md5 in jquery? [duplicate]

旧街凉风 提交于 2019-12-05 18:44:23

问题


Actually i am creating changepassword page. and this is my function of checking old password is match with the existing password or not. And that password is stored in MD5 in database so i want to first convert that password in MD5 and after that i can check that password. Here is the code.

function fnIsValidOldPassword()
{
var oldPassword = "";
var objUser = new Object();

objUser.UserID = <?php echo $_SESSION['UserId'] ?>;
$.ajax({
    type: "POST",
    url: "db.php?GetUser",
    data: {data:objUser},
    async:false,
    dataType:"json",
    success: function(response)
    {
        if(response.IsError)
            alert(response.ErrorMessage);
        else
            oldPassword = response.Records[0].Password;
    },
    error:function(message)
    {
        alert("Error: " + message);
    }
});

if($.md5($("#txtOldPassword").val())) != oldPassword)
         ^^ //here it shows error. that md5 is not a function.
{
    $("#errorPassword")[0].innerHTML = "Wrong Old Password.";
    $("#txtOldPassword").removeClass("successTextBox").addClass("errorTextBox");
    return false;
}

$("#txtOldPassword").removeClass("errorTextBox").addClass("successTextBox");
$("#errorPassword")[0].innerHTML = "";
return true;
}

md5 is not a function in jquery then how to convert the password in md5.


回答1:


jQuery doesnt have a method to provide the md5 of a string. So you need to use some external script. There is a plugin called jQuery MD5. and it gives you number of methods to achieve md5. Few of those are

Create (hex-encoded) MD5 hash of a given string value:

var md5 = $.md5('value');

Create (hex-encoded) HMAC-MD5 hash of a given string value and key:

var md5 = $.md5('value', 'key');

Create raw MD5 hash of a given string value:

var md5 = $.md5('value', null, true);

Create raw HMAC-MD5 hash of a given string value and key:

var md5 = $.md5('value', 'key', true);

This might do what you want... Check the snippet here. jQuery MD5




回答2:


Download and include this plugin

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js"></script>

and use like

if(CryptoJS.MD5($("#txtOldPassword").val())) != oldPassword) {

}

//Following lines shows md5 value
//var hash = CryptoJS.MD5("Message");
//alert(hash);



回答3:


You need additional plugin for this.

take a look at this plugin




回答4:


Get the field value through the id and send with ajax

var field = $("#field").val();
$.ajax({
    type: "POST",
    url: "db.php",
    data: {variable_name:field},
    async:false,
    dataType:"json",
    success: function(response) {
       alert(response);
    }
 });

At db.php file get the variable name

$variable_name = $_GET['variable_name'];
mysql_query("SELECT password FROM table_name WHERE password='".md5($variable_name)."'");



回答5:


<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
<script>
    var passhash = CryptoJS.MD5(password).toString();

    $.post(
      'includes/login.php', 
      { user: username, pass: passhash },
      onLogin, 
      'json' );
</script>



回答6:


Fiddle: http://jsfiddle.net/33HMj/

Js:

var md5 = function(value) {
    return CryptoJS.MD5(value).toString();
}

$("input").keyup(function () {
     var value = $(this).val(),
         hash = md5(value);
     $(".test").html(hash);
 });


来源:https://stackoverflow.com/questions/18585159/how-to-convert-password-into-md5-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!