jQuery 如何在jquery中将密码转换为md5?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18585159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:00:14  来源:igfitidea点击:

How to convert password into md5 in jquery?

javascriptjquerymd5

提问by Khushang Bhavnagarwala.

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.

实际上我正在创建更改密码页面。这是我检查旧密码是否与现有密码匹配的功能。并且该密码存储在数据库中的 MD5 中,因此我想先在 MD5 中转换该密码,然后我可以检查该密码。这是代码。

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.

md5 不是 jquery 中的函数,那么如何在 md5 中转换密码。

回答by Ayyappan Sekar

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

jQuery 没有提供字符串的 md5 的方法。所以你需要使用一些外部脚本。有一个名为 jQuery MD5 的插件。它为您提供了多种实现 md5 的方法。其中很少有人是

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

创建给定字符串值的(十六进制编码)MD5 哈希:

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

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

创建给定字符串值和键的(十六进制编码)HMAC-MD5 哈希:

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

Create raw MD5 hash of a given string value:

创建给定字符串值的原始 MD5 哈希:

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

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

创建给定字符串值和键的原始 HMAC-MD5 哈希:

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

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

这可能会做你想做的......检查这里的片段。jQuery MD5

回答by MaxEcho

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);

回答by Dipesh Parmar

You need additional plugin for this.

为此,您需要额外的插件。

take a look at this plugin

看看这个插件

回答by user2727841

Get the field value through the id and send with ajax

通过id获取字段值并用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

在 db.php 文件中获取变量名

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

回答by Dilip Godhani

<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>

回答by Avin Varghese

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

小提琴:http: //jsfiddle.net/33HMj/

Js:

JS:

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

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