import java.security.MessageDigest; public class HashLengthExample { public static void main(String[] args) throws Exception { String inputString = "Hello World!"; MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] hashBytes = md.digest(inputString.getBytes()); int hashLength = md.getDigestLength(); System.out.println("The length of the hash value produced is: " + hashLength + " bytes"); } }
import java.io.File; import java.io.FileInputStream; import java.security.MessageDigest; public class FileHashLengthExample { public static void main(String[] args) throws Exception { File inputFile = new File("input.txt"); MessageDigest md = MessageDigest.getInstance("SHA-1"); FileInputStream fis = new FileInputStream(inputFile); byte[] buffer = new byte[1024]; int read; while ((read = fis.read(buffer)) != -1) { md.update(buffer, 0, read); } fis.close(); byte[] hashBytes = md.digest(); int hashLength = md.getDigestLength(); System.out.println("The length of the hash value produced is: " + hashLength + " bytes"); } }This example shows how to create a digest of a file using the SHA-1 algorithm, obtain the hash value as a byte array, and then use the getDigestLength() method to determine the length of the hash value produced. Package Library: The java.security package is a part of the core Java class library and provides interfaces and classes for implementing security-related features such as authentication, authorization, encryption, digital signatures, key management, and message digests.