hash of string java
// Needed imports
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// Return the hash for the given string using the specified hashing algorithm (hashType)
// Usable algorithms include: "MD5", "SHA-256", "SHA3-256"
public static String getStringHash(String str, String hashType) throws NoSuchAlgorithmException
{
// hash string into byte array
MessageDigest md = MessageDigest.getInstance(hashType);
byte[] hashbytes = md.digest(str.getBytes());
// convert byte array into hex string and return
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < hashbytes.length; i++) {
stringBuffer.append(Integer.toString((hashbytes[i] & 0xff) + 0x100, 16)
.substring(1));
}
return stringBuffer.toString();
}