/* * Cay S. Horstmann & Gary Cornell, Core Java * Published By Sun Microsystems Press/Prentice-Hall * Copyright (C) 1997 Sun Microsystems Inc. * All Rights Reserved. * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * @version 1.00 10 Sep 1997 * @author Cay Horstmann */ /* * Modified by M. Gallant 09/12/2000 * - converted to applet (requires digital signature). * - suitable for IE4+ or JavaPlugin1.2.2+. * - MessageDigest not implemented in Netscape JVM. * - modified loadBytes() for faster file reading. * - modified computeDigest() for local formatting. * - replaced menu with two buttons; modified actionPerformed() * - Modified 04/21/2004 to add b64 encoding of hash */ import java.io.*; import java.security.*; import java.awt.*; import java.awt.event.*; public final class MessageDigestApplet extends java.applet.Applet implements ActionListener, ItemListener{ private Button b0, b1, b2 ; private TextArea message = new TextArea(); private TextField digest = new TextField(); private TextField digest64 = new TextField(); private String filename = null; private String lastDir = ""; private MessageDigest currentAlgorithm; public void init(){ this.setLayout(new BorderLayout(7,7)) ; Panel p = new Panel(); b0 = new Button("Clear text") ; b0.addActionListener(this) ; p.add(b0) ; b1 = new Button("File digest") ; b1.addActionListener(this) ; p.add(b1) ; b2 = new Button("Text digest") ; b2.addActionListener(this) ; p.add(b2) ; CheckboxGroup g = new CheckboxGroup(); addCheckbox(p, "SHA-1", g, true); addCheckbox(p, "MD5", g, false); add(p, "North"); add(message, "Center"); p = new Panel(new GridLayout(2,1, 7, 7)); p.add(digest); p.add(digest64); add(p, "South"); //add(new Label(""), "East") ; //add(new Label(""), "West") ; digest.setFont(new Font("Courier", Font.PLAIN, 12)); digest64.setFont(new Font("Courier", Font.PLAIN, 12)); setAlgorithm("SHA-1"); } public Insets getInsets() { return new Insets(5,7,7,7) ; } private void addCheckbox(Panel p, String name, CheckboxGroup g, boolean v) { Checkbox c = new Checkbox(name, g, v); c.addItemListener(this); p.add(c); } public void itemStateChanged(ItemEvent evt) { if (evt.getStateChange() == ItemEvent.SELECTED) setAlgorithm((String)evt.getItem()); } private void setAlgorithm(String alg) { try { currentAlgorithm = MessageDigest.getInstance(alg); } catch(NoSuchAlgorithmException e) { digest.setText("" + e); } } public void actionPerformed(ActionEvent evt) { String arg = evt.getActionCommand(); if (arg.equals("File digest")) { FileDialog d = new FileDialog(new Frame(), "Open text file", FileDialog.LOAD); d.setFile("*.txt"); d.setDirectory(lastDir); d.show(); String f = d.getFile(); lastDir = d.getDirectory(); if (f != null) { filename = lastDir + f; computeDigest(loadBytes(filename)); } } else if (arg.equals("Text digest")) { String m = message.getText(); computeDigest(m.getBytes()); } else if (arg.equals("Clear text")) { message.setText(""); digest.setText(""); digest64.setText(""); } } private byte[] loadBytes(String name) { FileInputStream in = null; try { in = new FileInputStream(name); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int bytesread = 0; byte [] tbuff = new byte[512] ; while(true) { bytesread = in.read(tbuff); if (bytesread == -1) // if EOF break ; buffer.write(tbuff,0,bytesread) ; } return buffer.toByteArray(); } catch (IOException e) { if (in != null) { try { in.close(); } catch (IOException e2) {} } return null; } } private void computeDigest(byte[] b) { currentAlgorithm.reset(); currentAlgorithm.update(b); byte[] hash = currentAlgorithm.digest(); String d = ""; int usbyte = 0; // unsigned byte for (int i = 0; i < hash.length; i++){ usbyte = hash[i] & 0xFF ; // byte-wise AND converts signed byte to unsigned. if(usbyte<16) d += "0" + Integer.toHexString(usbyte); // pad on left if single hex digit. else d += Integer.toHexString(usbyte); } digest.setText("Hash(hex): " + d.toUpperCase()); //----- Display the digest as b64 encoded -------------- sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder() ; String b64str = enc.encodeBuffer(hash).trim(); //removing any leading/trailing whitespace digest64.setText("Hash(b64): " + b64str) ; } public void paint(Graphics g) { // put black border around applet. g.setColor(Color.black) ; g.drawRect(0,0,this.getSize().width-1, this.getSize().height-1) ; } }