O SHA256 pode ser gerado usando a classe MessageDigest no jdk.
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.reset();
digest.update(input.getBytes("utf8"));
toReturn = String.format("%064x", new BigInteger(1, digest.digest()));
Um SHA-512 também pode ser gerado sem bibliotecas extras de java:
MessageDigest digest = MessageDigest.getInstance("SHA-512");
digest.reset();
digest.update(input.getBytes("utf8"));
toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));
O código a seguir cria um SHA256 e um SHA512 usando o "este é um exemplo" como uma entrada e as funções SHA-2 Hash. Os primeiros 2 métodos são criados para cada uma das funções hash. O primeiro é para SHA-256 e gera o hash usando a classe MessageDigest . O segundo método é criado da mesma maneira, mas usando o SHA-512. A validação da saída também geramos o hash com a classe DigestUtils de Apache Commons. Todos os hash geneados estão escritos no console.
import java.math.BigInteger;
import java.security.MessageDigest;
public class SHA2 {
public static String getSHA256(String input){
String toReturn = null;
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.reset();
digest.update(input.getBytes("utf8"));
toReturn = String.format("%064x", new BigInteger(1, digest.digest()));
} catch (Exception e) {
e.printStackTrace();
}
return toReturn;
}
public static String getSHA512(String input){
String toReturn = null;
try {
MessageDigest digest = MessageDigest.getInstance("SHA-512");
digest.reset();
digest.update(input.getBytes("utf8"));
toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));
} catch (Exception e) {
e.printStackTrace();
}
return toReturn;
}
public static void main(String[] argv) {
String inputValue = "this is an example";
// With the java libraries
String sha256 = getSHA256(inputValue);
System.out.println("The SHA-256 of \"" + inputValue + "\" is:");
System.out.println(sha256);
System.out.println();
// With Apache commons
sha256 = org.apache.commons.codec.digest.DigestUtils.sha256Hex(inputValue);
System.out.println("The SHA-256 of \"" + inputValue + "\" is:");
System.out.println(sha256);
System.out.println();
// With the java libraries
String sha512 = getSHA512( inputValue );
System.out.println("The SHA-512 of \"" + inputValue + "\" is:");
System.out.println(sha512);
System.out.println();
// With Apache commons
sha512 = org.apache.commons.codec.digest.DigestUtils.sha512Hex(inputValue);
System.out.println("The SHA-512 of \"" + inputValue + "\" is:");
System.out.println(sha512);
}
}
The SHA-256 of "this is an example13" is: 066f61646a92c8cf04943577b3c4d9ee6e5d125fe850b4c8c4d315ddd3aa1f50 The SHA-256 of "this is an example13" is: 066f61646a92c8cf04943577b3c4d9ee6e5d125fe850b4c8c4d315ddd3aa1f50 The SHA-512 of "this is an example4" is: 05c1649335528b45ab4965619db876e5090e42dfc526b486dde9368df2b77830c4e523cee1fd0054d9009729a6a11e697a1dd121e36be3ada593b8d76cb80ced The SHA-512 of "this is an example4" is: 05c1649335528b45ab4965619db876e5090e42dfc526b486dde9368df2b77830c4e523cee1fd0054d9009729a6a11e697a1dd121e36be3ada593b8d76cb80ced