简体   繁体   English

当我在客户端 hash 我的密码时,我没有得到散列字符串,而是得到密码本身

[英]when I hash my password on the client side , I am not getting the hashed string instead getting the password itself

I am trying to hash my password on the client-side before sending it to my server(Tomcat server), when I click on submit the form is getting submitted before updating the value of the password to hashcode how can I make sure That my form gets submitted after the script is executed?我正在尝试 hash 在客户端将我的密码发送到我的服务器(Tomcat 服务器)之前,当我单击提交时,在将密码的值更新为哈希码之前提交了表单我如何确保我的表单脚本执行后提交? or is there a better way to go by this?或者有没有更好的方法来 go?

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, 
initial-scale=1.0">
</head>
<body>
    <h1></h1>
    <form method="post" onsubmit="encrypt(event);" action="login" >
        <input type="text" name="user_id">
        <input type="password" name="password" id="pass">
        <button id="but1" type="submit">submit</button>
    </form>
    <script>
function encrypt(event){
//  event.preventDefault();//prevents from form getting auto submitted              
document.querySelector("body").style.color="red";             
sha512(document.getElementById("pass").value).then((x)=> 
{                
document.getElementById("pass").value=x;                   
document.querySelector("h1").innerHTML = x;
});
}


            function sha512(str) {
            return crypto.subtle.digest("SHA-512", new TextEncoder("utf- 
8").encode(str)).then(buf => {
            return Array.prototype.map.call(new Uint8Array(buf), x=> 
(('00'+x.toString(16)).slice(-2))).join('');
            });
            }
            sha512("my string for hashing").then(x => console.log(x)); 
    </script>
  </body>
</html>

Submit your form after the process完成后提交您的表格

 <form id="form1" method="post" onsubmit="encrypt(event);" action="login" >
        <input type="text" name="user_id">
        <input type="password" name="password" id="pass">
        <button id="but1" type="submit">submit</button>
    </form>


function encrypt(event) {
  event.preventDefault(); //prevents from form getting auto submitted
  document.querySelector("body").style.color = "red";
  sha512(document.getElementById("pass").value).then((x) => {
    document.getElementById("pass").value = x;
    document.querySelector("h1").innerHTML = x;
    document.getElementById("form1").submit(); // submitting the form
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我保护工作表时没有应用密码 - password is not getting applied when i protect sheet 我正在使用 bcrypt 创建登录 api 和散列密码 - i am creating login api and hashed password using bcrypt 为什么我得到这个而不是一个字符串? - Why am I getting this instead of a string? 为什么即使两个密码字符串完全匹配,我也会从 bcrypt 比较中得到 isMatch null? - Why am I getting isMatch null from bcrypt compare even though two password string matches perfectly? 我没有同时显示文字和滚动图像吗? - I am not getting my text and scroll images side by side? 为什么我在客户端状态下得到 406 not accepatble 服务器,甚至我的弹簧控制器也没有调用? - Why i am getting 406 not accepatble at client side status by the server and even my spring controller is not calling? 为什么在使用String.fromCharCode时会得到\\ u0000而不是字符? - Why am I getting \u0000 instead of characters when using String.fromCharCode? 重构jQuery代码时,为什么会得到[object Object]而不是预期的HTML? - Why am I getting [object Object] instead of my expected HTML when refactoring jQuery code? 为什么当我生成错误时我收到字符串错误? - Why when i generate an Error i am getting an String Error? 获取密码? - Getting Password?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM