Digital Edition

Frank's Java Code Stack #4 Using Message Digest Stream
Frank's Java Code Stack #4 Using Message Digest Stream

(November 12, 2002) - In Java Code Stack #1 and #3, we observed some code snippets on both Symmetric and Asymmetric Cryptography. But most of our applications, such as password authentication and logon verification, need a simpler way of creating a Digest of a given string or a message. Message Digest is a hash algorithm that takes as input a message of arbitrary length and produces as output a 128-bit fingerprint or message digest of the input. This Digest algorithm is meant for digital signature applications, where a large file/Data must be compressed in a secure manner before being encrypted with a Secret key under a public key crypto model. But instead of passing a byte array to the Digest system, we can pipe a stream to the Digest object for reading and writing Digest directly into the stream. This week, we'll build one such class, which performs a Digest for a string and writes it to a stream.


  1.        import*;
  2.        import*;

  3.        public class msgStream{
  4.        public static void main(String ar[]){

  5.        try{
  6.        /* Let us write the Digest to a
  7.        File Stream */
  8.        FileOutputStream fos=
           new FileOutputStream("MyDigest");
  9.        /* We are using SHA1 Algorithm */
  10.        MessageDigest md=

  11.        /* A transparent stream that
           updates the associated message
           digest using the bits going
           through the stream. */
  12.        DigestOutputStream dos=
           new DigestOutputStream(fos, md);
  13.        ObjectOutputStream oos=
           new ObjectOutputStream(dos);

  14.        /* String to be processed */
  15.        String text="This class
           works with Digest Streams";
  16.        oos.writeObject(text);

  17.        /* Before writing the digest
           to the Stream, turn the
           Digest OFF. When it is off,
           a call to one of the write
           methods does not result
           in an update on the
           message digest. However you        can ignore this.*/
  18.        dos.on(false);
  19.        oos.writeObject(md.digest());
  20.        }catch(Exception e){}

  21.        try{
  22.        /* Read the Digest from
           the File Stream */
  23.        FileInputStream fis=
  24.        new FileInputStream("MyDigest");

  25.        MessageDigest md=

  26.        DigestInputStream dis=
           new DigestInputStream(fis,md);
  27.        ObjectInputStream ois=
           new ObjectInputStream(dis);
  28.        String text=(String)ois.readObject();

  29.        /* We got the Original Text..
           Not the Digest! */
  30.        System.out.println(""+text);

  31.        dis.on(false);
  32.        byte rdigest[]=
           (byte[]) ois.readObject();

  33.        /* Comparing the Digest of the
           String with the Original Digest */
  34.        if(MessageDigest.isEqual
           (md.digest(), rdigest))
  35.        System.out.println("Valid Messg.");
  36.        else
  37.        System.out.println("Invalid Messg.");
  38.        }catch(Exception e){}

  39.        }
  40.        }

As you can see, the DigestOutputStream allows us to write Data to any Output Stream and calculate the Message Digest of that Data transparently as the Data passes through the Stream. Note that unlike usual Message Digest calculation, which involves only the Data, we are calculating the Digest over the serialized String Object, which can have additional information like Class definition along with the Data.

Try to build a Secure Message Digest (MAC) by using any standard Encryption Engine.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters


The standardization of container runtimes and images has sparked the creation of an almost overwhelm...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will ...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing w...
@DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novem...
The dynamic nature of the cloud means that change is a constant when it comes to modern cloud-based ...
DXWorldEXPO LLC announced today that Dez Blanchfield joined the faculty of CloudEXPO's "10-Year Anni...
As DevOps methodologies expand their reach across the enterprise, organizations face the daunting ch...
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO an...
"We started a Master of Science in business analytics - that's the hot topic. We serve the business ...
The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news an...
As you move to the cloud, your network should be efficient, secure, and easy to manage. An enterpris...
There is a huge demand for responsive, real-time mobile and web experiences, but current architectur...
DXWorldEXPO LLC announced today that "Miami Blockchain Event by FinTechEXPO" has announced that its ...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to ...
"NetApp is known as a data management leader but we do a lot more than just data management on-prem ...
We call it DevOps but much of the time there’s a lot more discussion about the needs and concerns of...
"Since we launched LinuxONE we learned a lot from our customers. More than anything what they respon...