netbeans 当我在客户端对密码进行散列时,我没有得到散列字符串,而是得到密码本身

3j86kqsm  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(104)

我尝试在客户端将密码发送到服务器(Tomcat服务器)之前对密码进行哈希处理,当我单击提交时,表单在密码值更新为哈希码之前被提交。如何确保表单在脚本执行后被提交?或者有更好的方法吗?

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

j1dl9f461#

完成流程后提交表单

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

相关问题