Digital Edition

SYS-CON.TV
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.

Code:

  1.        import java.io.*;
  2.        import java.security.*;

  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=
           MessageDigest.getInstance("SHA1");

  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=
           MessageDigest.getInstance("SHA1");

  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.

Assignment:
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



ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters

ADS BY GOOGLE

"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical ...
In his session at 20th Cloud Expo, Scott Davis, CTO of Embotics, discussed how automation can provid...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
In an era of historic innovation fueled by unprecedented access to data and technology, the low cost...
DXWorldEXPO LLC announced today that All in Mobile, a mobile app development company from Poland, wi...
Everyone wants the rainbow - reduced IT costs, scalability, continuity, flexibility, manageability, ...
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing w...
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT...
Today, we have more data to manage than ever. We also have better algorithms that help us access our...
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
The standardization of container runtimes and images has sparked the creation of an almost overwhelm...
CloudEXPO New York 2018, colocated with DXWorldEXPO New York 2018 will be held November 11-13, 2018,...
Most DevOps journeys involve several phases of maturity. Research shows that the inflection point wh...
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22...
Dynatrace is an application performance management software company with products for the informatio...
SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 1...
A valuable conference experience generates new contacts, sales leads, potential strategic partners a...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by Fi...
SYS-CON Events announced today that IoT Global Network has been named “Media Sponsor” of SYS-CON's @...