University of Cincinnati logo and link  
Using class MessageDigest
 
  UC ingot In this demo, I wrote a quick and dirty MessageDigest GUI.

I watered down the book example a bit.

  • I instantiated the MessageDigest in the JFrame's constructor.  I used the static getInstance method of MessageDigest, but I could have just as easily used the constructor for MessageDigest.
  • I performed all of the logic in the event handler for the DigestMe button.  Not very object oriented, but it does the trick for a quick and dirty.
    • The digest method of MessageDigest returns a byte array.  I pretty much copied the book's source on page 923 to handle this.
  • The source:
/*
 * DigestFrame.java
 *
 * Created on May 1, 2003, 12:19 AM
 */

import java.security.*;

/**
 *
 * @author  default
 */
public class DigestFrame extends javax.swing.JFrame {

    // Globally visible variable.
    MessageDigest md;
    
    /** Creates new form DigestFrame */
    public DigestFrame() {
        initComponents();
        try {
            // Create message digest.
            md = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println("No such digest algorythm exists.");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txbEnteredData = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        btnDigestMe = new javax.swing.JButton();
        txtDigest = new javax.swing.JTextField();
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        
        jLabel1.setText("Enter your message");
        getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);
        
        txbEnteredData.setRows(8);
        jScrollPane1.setViewportView(txbEnteredData);
        
        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
        
        btnDigestMe.setText("DigestMe");
        btnDigestMe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDigestMeActionPerformed(evt);
            }
        });
        
        jPanel2.add(btnDigestMe);
        
        txtDigest.setColumns(40);
        jPanel2.add(txtDigest);
        
        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
        
        pack();
    }

    private void btnDigestMeActionPerformed(java.awt.event.ActionEvent evt) {
        // Get the text, send it to the digest.
        String strEntered = txbEnteredData.getText();
        // The getBytes() method of class String gives us a byte array of the String.
        md.update(strEntered.getBytes());
        // Compute the digest.
        byte[] digestValue = md.digest();
        
        String result = "";
        
        // Just copied this out of the book.
        for (int i = 0; i < digestValue.length; i++) {
            int v = digestValue[i] & 0xFF;
            if (v < 16) result += "0";
            result += Integer.toString(v, 16).toUpperCase() + " ";
        }
        
        txtDigest.setText(result);
        
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new DigestFrame().show();
    }
 

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txbEnteredData;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton btnDigestMe;
    private javax.swing.JTextField txtDigest;
    // End of variables declaration

}

 Message Signing