KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > dialogs > PasswordInputDialog


1 package SnowMailClient.view.dialogs;
2
3 import SnowMailClient.crypto.Utilities;
4 import SnowMailClient.utils.*;
5 import snow.utils.gui.*;
6 import SnowMailClient.*;
7 import SnowMailClient.Language.Language;
8 import javax.swing.*;
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.util.Arrays JavaDoc;
12
13 /** A modal that ask for a password.
14  * when released, getPassword gives you a sha-1 of the password or null if no pass given.
15  * the signature sign of the password must accord...
16  */

17 public class PasswordInputDialog extends JDialog
18 {
19    private final JPasswordField pass1 = new JPasswordField(20);
20    private boolean testPassed = false;
21    Frame modalOwner;
22    byte[] sign;
23
24    public PasswordInputDialog(final Frame modalOwner,
25                               String JavaDoc title,
26                               String JavaDoc description,
27                               final byte[] sign)
28    {
29       super(modalOwner, title, true);
30
31       this.modalOwner = modalOwner;
32       this.sign = (sign!=null? sign.clone(): null);
33
34
35
36       JTextArea descrTA = new JTextArea(description);
37       descrTA.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
38       descrTA.setEditable(false);
39
40       getContentPane().setLayout( new BorderLayout());
41
42       JPanel centerPanel = new JPanel(new BorderLayout());
43       JPanel passPanel = new JPanel(new GridLayout(1, 2));
44       centerPanel.add(passPanel,BorderLayout.CENTER);
45       getContentPane().add(centerPanel, BorderLayout.CENTER);
46       getContentPane().add(descrTA, BorderLayout.NORTH);
47
48       JPanel contrlpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
49
50       getContentPane().add(contrlpanel, BorderLayout.SOUTH);
51
52       ActionListener disposeActionListener = new ActionListener()
53       {
54        public void actionPerformed(ActionEvent e)
55         {
56            dispose();
57         }
58       };
59       ActionListener okActionListener = new ActionListener()
60       {
61        public void actionPerformed(ActionEvent e)
62         {
63           okAction(); }
64       };
65
66
67       JButton cancel = new JButton(Language.translate("Cancel"), Icons.CrossIcon.shared10);
68       contrlpanel.add(cancel);
69       cancel.addActionListener(disposeActionListener);
70
71       JButton close = new JButton(Language.translate(" Ok "), Icons.OkIcon.shared10);
72       contrlpanel.add(close);
73       close.addActionListener(okActionListener);
74
75       passPanel.add(new JLabel("Password "));
76       passPanel.add(pass1);
77
78       pass1.addActionListener(okActionListener);
79
80
81
82       this.getRootPane().registerKeyboardAction( disposeActionListener,
83                                                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
84                                                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
85
86       this.getRootPane().registerKeyboardAction( okActionListener,
87                                                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
88                                                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
89
90
91
92       // modality
93

94       pack();
95       SnowMailClientApp.centerComponentOnMainFrame(this);
96       pass1.requestFocus();
97       setVisible(true);
98    }
99
100
101    private void okAction()
102    {
103
104            // the hash is the password
105
String JavaDoc hash = Utilities.hashPassword(new String JavaDoc(pass1.getPassword()));
106
107            // the signatures are the first 4 bytes of the hash of the password,
108
// also h(h(****))
109
if(sign!=null && !Arrays.equals(sign, Utilities.hashPassword(hash).substring(0,4).getBytes() ))
110            {
111                JOptionPane.showMessageDialog( modalOwner, "Bad Password" );
112                pass1.setSelectionStart(0);
113                pass1.setSelectionEnd(pass1.getPassword().length);
114                pass1.requestFocus();
115
116            }
117            else
118            {
119                // OK, but we are not 100% sure, only the four first bytes of the hash
120
// match... each 256^4 random pass can pass through,
121
// of course, it will result an exception later, the data cannot be deciphered...
122
testPassed = true;
123               dispose();
124            }
125    }
126
127
128    public char[] getPassword() {return pass1.getPassword();}
129    public boolean getPasswordOK() {return testPassed;}
130
131
132 }
Popular Tags