Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: MD5 and Java
|
Posted: Apr 20, 2002 6:52 PM
|
|
/* MessageDigestComputer.java * @author: Charles Bell * @version: Apr 20, 2002 */
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.security.*;
import javax.swing.*;
/** Computes the Message Digest of the Text contained in the text area and displays it * in the TextField when the Compute button is clicked. * Message Digest Algorithms: * MD2: The MD2 message digest algorithm as defined in RFC 1319. * MD5: The MD5 message digest algorithm as defined in RFC 1321. * SHA-1: The Secure Hash Algorithm, as defined in Secure Hash Standard, NIST FIPS 180-1. * SHA-256, SHA-384, and SHA-512: New hash algorithms for which the draft Federal Information * Processing Standard 180-2, Secure Hash Standard (SHS) is now available. SHA- 256 is a 256-bit * hash function intended to provide 128 bits of security against collision attacks, while SHA-512 is * a 512-bit hash function intended to provide 256 bits of security. A 384-bit hash may be obtained * by truncating the SHA-512 output. */ public class MessageDigestComputer extends Applet implements ActionListener{
JFrame frame; JTextArea editor; JTextField digestdisplay; MessageDigest messagedigest; boolean inanapplet = true;
/** Creates an instance of this class and initiates it. The variable * inanapplet is only set to false when main is invoked, i.e. when * this is run as an application. */ public static void main(String[] args){ MessageDigestComputer computer = new MessageDigestComputer(); computer.inanapplet = false; //otherwise in an applet it will be true computer.init(); }
/** Initiates the user interface and initializes the Digest Algorithm */ public void init(){ frame = new JFrame("MessageDigest"); frame.setSize(450,450); if (!inanapplet){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }else{ frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); } JButton md5button = new JButton("Compute MD5"); JButton sha1button = new JButton("Compute SHA-1");
JButton exitbutton = new JButton("Exit"); JPanel buttonpanel = new JPanel(); md5button.addActionListener(this); sha1button.addActionListener(this); buttonpanel.add(md5button); buttonpanel.add(sha1button); exitbutton.addActionListener(this); buttonpanel.add(exitbutton); frame.getContentPane().add(buttonpanel,"North"); editor = new JTextArea(); frame.getContentPane().add(editor,"Center"); digestdisplay = new JTextField(); Font myfont = new Font("Monospaced",Font.PLAIN,12); editor.setFont(myfont); digestdisplay.setFont(myfont); frame.getContentPane().add(digestdisplay,"South"); frame.show(); }
/** initializes the Digest Algorithm */ public void setDigestAlgorithm(String algorithm){ try{ messagedigest = MessageDigest.getInstance(algorithm); displayDigest(""); }catch(NoSuchAlgorithmException nsae){ System.out.println("NoSuchAlgorithmException: " + nsae.getMessage()); System.out.println("Should be SHA-1, MD2, or MD5."); } }
/** Gets the byte array corresponding to the text in the digestdisplay text area. */ public byte[] getDigestDisplayBytes(){ return digestdisplay.getText().getBytes(); }
/** Computes the message digest of the input string. */ public String computeDigestText(String s){ messagedigest.reset(); messagedigest.update(s.getBytes()); byte[] digestarray = messagedigest.digest(); String digesttext = ""; for (int i = 0;i< digestarray.length;i++){ int j = digestarray[i] & 0xFF; if (j < 16) digesttext = digesttext + "0"; digesttext = digesttext + Integer.toString(j,16).toUpperCase() + " "; } return digesttext; }
/** Computes the message digest from the byte array given as input. */ public String computeDigestText(byte[] bytearray){ messagedigest.reset(); messagedigest.update(bytearray); byte[] digestarray = messagedigest.digest(); String digesttext = ""; for (int i = 0;i< digestarray.length;i++){ int j = digestarray[i] & 0xFF; if (j < 16) digesttext = digesttext + "0"; digesttext = digesttext + Integer.toString(j,16).toUpperCase() + " "; } return digesttext; }
/** Outputs the computed Message Digest to the digestdisplay JTextField. */ public void displayDigest(String text){ digestdisplay.setText(text); }
/** Implemants the ActionListener interface providing response to buttons * used in user interface. */ public void actionPerformed(ActionEvent actionevent){ String commandstring = actionevent.getActionCommand();
if (commandstring.compareTo("Compute MD5")==0){ setDigestAlgorithm("MD5"); byte[] bytearray = getDigestDisplayBytes(); displayDigest(computeDigestText(bytearray)); }else if (commandstring.compareTo("Compute SHA-1")==0){ setDigestAlgorithm("SHA-1"); byte[] bytearray = getDigestDisplayBytes(); displayDigest(computeDigestText(bytearray)); }else if (commandstring.compareTo("Exit") ==0){ if (!inanapplet){ System.exit(0); }else{ frame.hide(); } } } }
|
|