1 package net.suberic.util.gui.propedit; 2 import javax.swing.*; 3 import java.awt.event.*; 4 import net.suberic.util.*; 5 import java.awt.FlowLayout ; 6 7 public class PasswordEditorPane extends StringEditorPane { 8 String originalScrambledValue; 9 10 17 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 18 configureBasic(propertyName, template, propertyBaseName, newManager); 19 20 originalScrambledValue = manager.getProperty(property, ""); 21 if (!originalScrambledValue.equals("")) 22 originalValue = descrambleString(originalScrambledValue); 23 else 24 originalValue = ""; 25 currentValue = originalValue; 26 27 getLogger().fine("property is " + property + "; editorTemplate is " + editorTemplate); 28 29 label = createLabel(); 30 31 inputField = new JPasswordField(originalValue); 32 inputField.setPreferredSize(new java.awt.Dimension (150, inputField.getMinimumSize().height)); 33 inputField.setMaximumSize(new java.awt.Dimension (Integer.MAX_VALUE, inputField.getMinimumSize().height)); 34 inputField.addFocusListener(new FocusAdapter() { 35 public void focusLost(FocusEvent e) { 36 String newPassword = new String (((JPasswordField)inputField).getPassword()); 37 if (! newPassword.equals(currentValue)) { 38 try { 39 firePropertyChangingEvent(newPassword); 40 firePropertyChangedEvent(newPassword); 41 currentValue = newPassword; 42 } catch (PropertyValueVetoException pvve) { 43 manager.getFactory().showError(inputField, "Error changing value " + label.getText() + ": " + pvve.getReason()); 44 inputField.setText(currentValue); 45 } 46 } 47 } 48 49 public void focusGained(FocusEvent e) { 50 if (inputField.getText() != null && inputField.getText().length() > 0) { 51 inputField.setSelectionStart(0); 52 inputField.setSelectionEnd(inputField.getText().length()); 53 } 54 } 55 56 }); 57 this.add(label); 58 this.add(inputField); 59 updateEditorEnabled(); 60 61 labelComponent = label; 62 valueComponent = inputField; 63 64 } 66 67 71 public void setValue() throws PropertyValueVetoException { 72 String value = new String (((JPasswordField)inputField).getPassword()); 73 74 if (isEditorEnabled() && ! (value.equals(currentValue))) { 75 firePropertyChangingEvent(value); 76 firePropertyChangedEvent(value); 77 } 78 79 if (isEditorEnabled() && !(value.equals(originalValue))) { 80 manager.setProperty(property, scrambleString(value)); 81 originalValue = value; 82 } 83 } 84 85 89 public java.util.Properties getValue() { 90 String value = new String (((JPasswordField)inputField).getPassword()); 91 java.util.Properties retProps = new java.util.Properties (); 92 if (value.equals(originalValue)) 93 retProps.setProperty(property, originalScrambledValue); 94 else 95 retProps.setProperty(property, scrambleString(value)); 96 return retProps; 97 } 98 99 103 public void resetDefaultValue() { 104 String fieldValue = new String (((JPasswordField)inputField).getPassword()); 105 if (! (fieldValue.equals(currentValue) && fieldValue.equals(originalValue))) { 106 try { 108 if (! currentValue.equals(originalValue)) { 109 firePropertyChangingEvent(originalValue); 110 firePropertyChangedEvent(originalValue); 111 currentValue = originalValue; 112 } 113 inputField.setText(originalValue); 114 } catch (PropertyValueVetoException pvve) { 115 manager.getFactory().showError(inputField, "Error changing value " + label.getText() + " to " + originalValue + ": " + pvve.getReason()); 116 } 117 } 118 } 119 120 private static char[] scrambleChars = new char[] {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z'}; 122 123 126 public static String scrambleString(String key) { 127 int[] salt = new int[4]; 128 int keySize = key.length(); 129 long seed = System.currentTimeMillis(); 130 131 salt[0] = (int)((seed / 107) % 2704); 132 salt[1] = (int)((seed / 19) % 2704); 133 salt[2] = (int)((seed / 17) % 2704); 134 salt[3] = (int)((seed / 91) % 2704); 135 136 char [] scrambledString = new char[(keySize * 2) + 8]; 137 138 for (int i = 0; i < keySize; i++) { 139 int numValue = (int)(key.charAt(i)); 140 numValue = (numValue + salt[i % 4]) % 2704; 141 scrambledString[i * 2] = scrambleChars[numValue / 52]; 142 scrambledString[(i * 2) + 1] = scrambleChars[numValue % 52]; 143 } 144 145 for (int i = 0; i < 3; i++) { 146 int numValue = (salt[i] + salt[i + 1]) % 2704; 147 scrambledString[(keySize + i) * 2] = scrambleChars[numValue / 52]; 148 scrambledString[((keySize + i) * 2) + 1] = scrambleChars[numValue % 52]; 149 } 150 151 scrambledString[(keySize + 3) * 2] = scrambleChars[salt[3] / 52]; 152 scrambledString[((keySize + 3) * 2) + 1] = scrambleChars[salt[3] % 52]; 153 154 return new String (scrambledString); 155 } 156 157 160 public static String descrambleString(String value) { 161 int[] salt = new int[4]; 162 int scrambleSize = value.length(); 163 char[] key = new char[(scrambleSize - 8) / 2]; 164 salt[3] = (findCharValue(value.charAt(scrambleSize - 2)) * 52) + findCharValue(value.charAt(scrambleSize - 1)); 165 166 for (int i = 2; i >= 0; i--) { 167 salt[i] = (2704 - salt[i + 1] + (findCharValue(value.charAt(scrambleSize - ((4 - i) * 2) )) * 52) + findCharValue(value.charAt(scrambleSize - ((4 - i) * 2) + 1))) % 2704; 168 } 169 170 for (int i = 0; i < (scrambleSize - 8) / 2; i++) { 171 key[i] = (char)((2704 - salt[i % 4] + (findCharValue(value.charAt(i * 2)) * 52) + findCharValue(value.charAt((i * 2) + 1))) % 2704); 172 } 173 174 return new String (key); 175 } 176 177 181 private static int findCharValue(char a) { 182 for (int i = 0; i < scrambleChars.length; i++) 183 if (a == scrambleChars[i]) 184 return i; 185 186 return 0; 187 } 188 189 } 190 | Popular Tags |