Answers for "hash of string java"

1

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();
}
Posted by: Guest on July-21-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language