DigestOutputStream.toString() method in Java with Examples
The toString() method of java.security.DigestOutputStream class used to provide the object of DigestOutputStream in string format.
Syntax:
public String toString()
Return Value: This method returns the object of DigestOutputStream in string format.
Note: All the programs in this article won’t run on online IDE as no ‘name’ file exists. You can check this code on the Java compiler on your system. To check this code, create a file ‘name’ on your system.
Below are the examples to illustrate the toString() method:
Example 1:
import java.security.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
MessageDigest sr
= MessageDigest.getInstance( "MD5" );
OutputStream is
= new FileOutputStream(
"f:/java/name.txt" );
DigestOutputStream di
= new DigestOutputStream(is, sr);
String str = di.toString();
System.out.println( "Status : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
catch (FileNotFoundException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Status : [Digest Output Stream] MD5 Message Digest from SUN,
Example 2:
import java.security.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
MessageDigest sr
= MessageDigest.getInstance( "SHA-1" );
OutputStream is
= new FileOutputStream(
"f:/java/name.txt" );
DigestOutputStream di
= new DigestOutputStream(is, sr);
String str = di.toString();
System.out.println( "Status : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
catch (FileNotFoundException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Status : [Digest Output Stream] SHA-1 Message Digest from SUN,
Reference: https://docs.oracle.com/javase/9/docs/api/java/security/DigestOutputStream.html#setMessageDigest-java.security.MessageDigest-
Last Updated :
09 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...