KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > FileEncryptor > HashCodeDialog


1 package snow.FileEncryptor;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7 import javax.swing.event.*;
8 import javax.swing.filechooser.*;
9 import java.util.*;
10 import java.util.zip.*;
11
12 import java.security.*;
13 import java.security.spec.*;
14 import javax.crypto.spec.*;
15 import javax.crypto.*;
16
17 import snow.utils.gui.*;
18 import snow.utils.storage.*;
19 import snow.crypto.*;
20 import snow.Language.Language;
21
22 import SnowMailClient.crypto.Utilities;
23
24 import java.io.*;
25
26 public final class HashCodeDialog extends JDialog
27 {
28   final AppProperties props;
29   final FileField inputFileField;
30   final JTextArea codeArea = new JTextArea(4,40);
31   final JTextArea verificationArea = new JTextArea(
32      //Language.translate("paste a hashcode to verify here"),
33
"",
34      4,40);
35
36   byte[] lastComputedHash = null;
37
38   public HashCodeDialog(JFrame parent, final AppProperties props)
39   {
40      super(parent, Language.translate("Snowmail HashCode Utility"), false);
41      this.props = props;
42
43      getContentPane().setLayout(new BorderLayout());
44      inputFileField = new FileField(
45        props.getProperty("HashCodeDialog.File", ""),
46        false,
47        Language.translate("Choose a file for which to compute the hashcode"),
48        JFileChooser.FILES_ONLY
49      );
50
51      ActionListener computeHashAction = new ActionListener()
52      {
53         public void actionPerformed(ActionEvent ae)
54         {
55           computeHashCode();
56         }
57      };
58      inputFileField.addActionListener(computeHashAction);
59
60      // north
61
//
62
JPanel inputPanel = new JPanel();
63      inputPanel.setBorder(new EmptyBorder(5,1,5,1));
64      getContentPane().add(GUIUtils.wrapLeft(inputPanel), BorderLayout.NORTH);
65      GridLayout3 gl3 = new GridLayout3(2,inputPanel);
66      gl3.add(Language.translate("File to analyse"));
67      gl3.add(inputFileField, true);
68      inputFileField.setComponentWidth(265);
69      JButton computeHash = new JButton(Language.translate("Compute HashCode"));
70      gl3.add(computeHash);
71      computeHash.addActionListener(computeHashAction);
72
73      // Center
74
//
75
JPanel outputPanel = new JPanel();
76      getContentPane().add(outputPanel, BorderLayout.CENTER);
77      GridLayout3 gl2 = new GridLayout3(1, outputPanel);
78      gl2.add(Language.translate("Computed Hashcodes"));
79      gl2.add(new JScrollPane(codeArea));
80      codeArea.setEditable(false);
81      gl2.add(Language.translate("Verification (Paste an external hashcode to verify)"));
82      gl2.add(new JScrollPane(verificationArea));
83
84      verificationArea.getDocument().addDocumentListener(new DocumentListener()
85      {
86         public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc de)
87         {
88           verifyHashCode();
89         }
90
91         public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc de)
92         {
93           verifyHashCode();
94         }
95
96         public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc de)
97         {
98           verifyHashCode();
99         }
100      });
101
102
103      // South
104
//
105
CloseControlPanel ccp = new CloseControlPanel(this, false, false, Language.translate("Close"));
106      getContentPane().add(ccp, BorderLayout.SOUTH);
107
108
109      pack();
110      this.setLocationRelativeTo(parent);
111      setVisible(true);
112   } // Constructor
113

114
115
116   private void computeHashCode()
117   {
118     Thread JavaDoc t = new Thread JavaDoc()
119     {
120       public void run()
121       {
122         codeArea.setForeground(UIManager.getColor("TextArea.foreground"));
123         final ProgressModalDialog pd = new ProgressModalDialog(HashCodeDialog.this, Language.translate("computing hashcode"), false);
124         try
125         {
126           final File file = inputFileField.getPath();
127           if(file.getName().trim().length()==0)
128           {
129             throw new Exception JavaDoc(Language.translate("No file given"));
130           }
131
132           if(!file.exists()) throw new Exception JavaDoc(Language.translate("File % dont exist", ""+file));
133
134           props.setProperty("HashCodeDialog.File", file.getAbsolutePath());
135
136           lastComputedHash = CryptoUtilities.hashSHA1( file, pd );
137           codeArea.setText("SHA-1: "+ Utilities.asHex(lastComputedHash) );
138         }
139         catch(Exception JavaDoc e)
140         {
141           codeArea.setText(Language.translate("Error: ")+e.getMessage());
142           codeArea.setForeground(Color.red);
143         }
144         finally
145         {
146           pd.closeDialog();
147           verifyHashCode();
148         }
149       }
150     };
151
152     t.start();
153   }
154
155
156   private void verifyHashCode()
157   {
158      String JavaDoc pasted = verificationArea.getText().trim();
159      pasted = pasted.replaceAll("\\s", "");
160
161      String JavaDoc computed = Utilities.asHex(lastComputedHash);
162
163      if(pasted.equalsIgnoreCase(computed))
164      {
165         verificationArea.setForeground(UIManager.getColor("TextArea.foreground"));
166      }
167      else
168      {
169         verificationArea.setForeground(Color.red);
170      }
171   }
172
173
174
175   public static void main(String JavaDoc[] aa)
176   {
177      JFrame parent = new JFrame("");
178      parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
179
180      AppProperties app = new AppProperties();
181      HashCodeDialog hd = new HashCodeDialog(parent, app);
182   }
183
184
185 } // HashCodeDialog
Popular Tags