在服务器上提交表单之前散列密码

Hashing a password before form submission on Server

下面函数在 JavaScript(forms.js include) 中用于散列密码。 什么是 JAVA,

中的等价物
function formhash(form, password) {
      console.log("Hashing form");
      // Create a new element input, this will be our hashed password field.
      var p = document.createElement("input");
      // Add the new element to our form.
      form.appendChild(p);
      p.name = "p";
      p.type = "hidden"
      p.value = hex_sha512(password.value);
      // Make sure the plaintext password doesn't get sent.
      password.value = "";
      // Finally submit the form.
      form.submit();
    }

这是执行此操作的代码。复制自 http://runnable.com/U8lo-rXJWGlhL-OG/sha512-for-java

import java.security.MessageDigest;

public class SHA512 {
      public static void main(String args[]) throws Exception {
          String password = "pass@word1";

            if ((args.length == 1) && (args[0].length() > 0))
            {
                password = args[0];
            }
            System.out.println("Password: " + password + " in SHA512 is:");
            System.out.println(hashText(password));
      }

    public static String convertByteToHex(byte data[])
    {
        StringBuffer hexData = new StringBuffer();
        for (int byteIndex = 0; byteIndex < data.length; byteIndex++)
            hexData.append(Integer.toString((data[byteIndex] & 0xff) + 0x100, 16).substring(1));

        return hexData.toString();
    }

    public static String hashText(String textToHash) throws Exception
    {
        final MessageDigest sha512 = MessageDigest.getInstance("SHA-512");
        sha512.update(textToHash.getBytes());

        return convertByteToHex(sha512.digest());
    }
}

[编辑]

如果使用不同的字符集,请将其传递给 getBytes。示例 textToHash.getBytes("UTF-8")