1 16 17 package de.schlichtherle.key.passwd.console; 18 19 import de.schlichtherle.key.*; 20 21 import java.io.*; 22 import java.util.*; 23 24 32 public class PromptingKeyProviderUI 33 implements de.schlichtherle.key.PromptingKeyProviderUI { 34 35 private static final String CLASS_NAME 36 = "de/schlichtherle/key/passwd/console/PromptingKeyProviderUI".replace('/', '.'); protected static final ResourceBundle resources 38 = ResourceBundle.getBundle(CLASS_NAME); 39 40 45 protected static final Console con = System.console(); 46 47 50 private static final PromptingLock lock = new PromptingLock(); 51 52 53 private static final int MIN_PASSWD_LEN = 6; 54 55 59 private static String lastResourceID = ""; 60 61 public final void promptCreateKey(final PromptingKeyProvider provider) { 62 synchronized (lock) { 63 final String resourceID = provider.getResourceID(); 64 if (!resourceID.equals(lastResourceID)) 65 printf(resources.getString("createKey.banner"), 66 provider.getResourceID()); 67 lastResourceID = resourceID; 68 69 while (true) { 70 char[] newPasswd1 = readPassword( 71 resources.getString("createKey.newPasswd1")); 72 if (newPasswd1 == null || newPasswd1.length <= 0) 73 return; 74 75 char[] newPasswd2 = readPassword( 76 resources.getString("createKey.newPasswd2")); 77 if (newPasswd2 == null) 78 return; 79 80 if (!Arrays.equals(newPasswd1, newPasswd2)) { 81 printf(resources.getString("createKey.passwd.noMatch")); 82 continue; 83 } 84 85 if (newPasswd1.length < MIN_PASSWD_LEN) { 86 printf(resources.getString("createKey.passwd.tooShort")); 87 continue; 88 } 89 90 provider.setKey(newPasswd1); 91 break; 92 } 93 94 promptExtraData(provider); 95 } 96 } 97 98 protected void promptExtraData(PromptingKeyProvider provider) 99 { 100 } 101 102 public final boolean promptUnknownOpenKey(PromptingKeyProvider provider) { 103 synchronized (lock) { 104 return promptOpenKey(provider, false); 105 } 106 } 107 108 public final boolean promptInvalidOpenKey(PromptingKeyProvider provider) { 109 synchronized (lock) { 110 return promptOpenKey(provider, true); 111 } 112 } 113 114 private boolean promptOpenKey(final PromptingKeyProvider provider, final boolean invalid) { 115 if (invalid) 116 printf(resources.getString("openKey.invalid")); 117 118 final String resourceID = provider.getResourceID(); 119 if (!resourceID.equals(lastResourceID)) 120 printf(resources.getString("openKey.banner"), 121 provider.getResourceID()); 122 lastResourceID = resourceID; 123 124 char[] passwd = readPassword(resources.getString("openKey.passwd")); 125 if (passwd == null || passwd.length <= 0) { 126 provider.setKey(null); 127 return false; 128 } 129 130 provider.setKey(passwd); 131 132 while (true) { 133 String changeKey = readLine(resources.getString("openKey.change")); 134 if (changeKey == null) 135 return false; 136 changeKey = changeKey.toLowerCase(); 137 if (changeKey.length() <= 0 || changeKey.equals(resources.getString("no"))) 138 return false; 139 else if (changeKey.equals(resources.getString("yes"))) 140 return true; 141 } 142 } 143 144 149 protected final Console printf(String format) { 150 return con.printf(format, null); 151 } 152 153 protected final Console printf(String format, Object arg) { 154 return con.printf(format, new Object [] { arg }); 155 } 156 157 protected final String readLine(String format) { 158 return con.readLine(format, null); 159 } 160 161 protected final String readLine(String format, Object arg) { 162 return con.readLine(format, new Object [] { arg }); 163 } 164 165 protected final char[] readPassword(String format) { 166 return con.readPassword(format, null); 167 } 168 169 173 private static class PromptingLock { } 174 } 175 | Popular Tags |