DigestInputStream Class Example

In this post under Java –> Security, I will explain with example the use of DigestInputStream class.

When any stream class under java.io package is used, the data flows from source to destination in a stream.

DigestInputStream class can be used to calculate a Message Digest when data is being read from a source using the streams. As new data enters the stream, the digest is regularly updated internally.

Once the data is completely read from the destination, a final message digest is calculated.

The DigestInputStream class takes help of MessageDigest class to calculate the message digest.

MessageDigest class are one way hash function which take arbitrary data and calculates fixed length hash value. The fixed length hash value is called Message Digest.

Below main class shows how to use the DigestInputStream class.

Main class


1  package security;
2  
3  import java.io.BufferedReader;
4  import java.io.File;
5  import java.io.FileInputStream;
6  import java.io.IOException;
7  import java.io.InputStreamReader;
8  import java.security.DigestInputStream;
9  import java.security.MessageDigest;
10 import java.security.NoSuchAlgorithmException;
11 
12 public class Example2 {
13 	public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
14 		MessageDigest messageDigest = MessageDigest.getInstance("SHA");
15 		File file = new File("OutputFile.txt");
16 		try(FileInputStream fis = new FileInputStream(file);
17 			DigestInputStream dis = new DigestInputStream(fis, messageDigest);
18 			InputStreamReader isr = new InputStreamReader(dis);
19 			BufferedReader br = new BufferedReader(isr)) {
20 			String data = br.readLine();
21 			while(data != null) {
22 				System.out.println(data);
23 				data = br.readLine();
24 			}
25 			br.close();
26 			byte[] digest = dis.getMessageDigest().digest();
27 			System.out.println(digest);
28 		}
29 	}
30 }

In the above java code, at line 14, we create an instance of MessageDigest by passing algorithm name as an argument to “getInstance” static method. Based on the algorithm name passed, appropriate MessageDigest instance will be created.

At line 16 within try-with resource block, I have created FileInputStream “fis” to read the file “OutputFile.txt”.

At line 17 within try-with resource block, wrapped the “fis” with an instance of DigestInputStream “dis”. The DigestInputStream constructor takes two arguments, one being InputStream instance and another being an instance of MessageDigest class, in this case it is the instance created at line 14.

We are wrapping the FileInputStream “fis” with DigestInputStream “dis” to enhance the functionality of input stream. The input stream along with reading file content, calculates the message digest.

From line 20 to 25, we read the contents of the file. Once done we close the stream.

At line 26, we call the “digest” method on MessageDigest instance created at line 14. This will return final computed hash value as byte array, which is printed at line 27.

Leave a Reply