Функцията "Използване по-долу" в JavaScript (forms.js включете) за извличане на парола. Какво е еквивалентът в 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();
}
Отговори:
5 за отговор № 1Това е кодът, който го прави. Копирано от 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")