List of usage examples for java.io Console readPassword
public char[] readPassword(String fmt, Object... args)
From source file:it.flavianopetrocchi.jpdfbookmarks.JPdfBookmarks.java
private byte[] askUserPassword() { //avoid the use of strings when dealing with passwords they remain in memomory Console cons; char[] passwdChars = null; byte[] passwdBytes = null; if ((cons = System.console()) != null && (passwdChars = cons.readPassword("[%s:]", Res.getString("PASSWORD"))) != null) { passwdBytes = Ut.arrayOfCharsToArrayOfBytes(passwdChars); Arrays.fill(passwdChars, ' '); } else {//w w w . j av a 2 s. co m out.print("[" + Res.getString("LABEL_PASSWORD") + "]"); out.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); passwdChars = new char[MAX_PASSWORD_LEN]; try { int charsRead = in.read(passwdChars, 0, MAX_PASSWORD_LEN); //remove \r and \n from the password for (int i = charsRead - 1; i >= 0; i--) { if (passwdChars[i] == '\r' || passwdChars[i] == '\n') { charsRead--; } else { break; } } char[] trimmedPasswd = Arrays.copyOf(passwdChars, charsRead); Arrays.fill(passwdChars, ' '); passwdBytes = Ut.arrayOfCharsToArrayOfBytes(trimmedPasswd); Arrays.fill(trimmedPasswd, ' '); } catch (IOException ex) { } } return passwdBytes; }
From source file:org.roda.core.RodaCoreFactory.java
private static String readPassword(final String message) throws IOException { final Console console = System.console(); if (console == null) { final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print(String.format("%s (INSECURE - password will be shown): ", message)); return reader.readLine(); } else {/*from ww w. j ava 2 s . c om*/ return new String(console.readPassword("%s: ", message)); } }
From source file:org.rhq.server.control.RHQControl.java
private void promptForProperty(PropertiesFileUpdate pfu, Properties props, String propertiesFileName, String propertyName, boolean obfuscate, boolean encode, boolean hideInput) throws Exception { String propertyValue = props.getProperty(propertyName); if (StringUtil.isBlank(propertyValue)) { // prompt for the property value Console console = System.console(); console.format("\nThe [%s] property is required but not set in [%s].\n", propertyName, propertiesFileName);// www . j a v a 2 s . c om console.format("Do you want to set [%s] value now?\n", propertyName); String response = ""; while (!(response.startsWith("n") || response.startsWith("y"))) { response = String.valueOf(console.readLine("%s", "yes|no: ")).toLowerCase(); } if (response.startsWith("n")) { throw new RHQControlException("Please update the [" + propertiesFileName + "] file as required."); } do { propertyValue = ""; while (StringUtil.isBlank(propertyValue)) { if (!hideInput) { propertyValue = String.valueOf(console.readLine("%s", propertyName + (((obfuscate || encode) ? " (enter as plain text): " : ": ")))); } else { propertyValue = String.valueOf(console.readPassword("%s", propertyName + (((obfuscate || encode) ? " (enter as plain text): " : ": ")))); } } if (!hideInput) { console.format("Is [" + propertyValue + "] correct?\n"); response = ""; while (!(response.startsWith("n") || response.startsWith("y"))) { response = String.valueOf(console.readLine("%s", "yes|no: ")).toLowerCase(); } } else { console.format("Confirm:\n"); String confirmValue = String.valueOf(console.readPassword("%s", propertyName + (((obfuscate || encode) ? " (enter as plain text): " : ": ")))); response = (propertyValue.equals(confirmValue) ? "yes" : "no"); } } while (response.startsWith("n")); propertyValue = obfuscate ? Obfuscator.encode(propertyValue) : propertyValue; propertyValue = encode ? CryptoUtil.createPasswordHash("MD5", CryptoUtil.BASE64_ENCODING, null, null, propertyValue) : propertyValue; props.setProperty(propertyName, propertyValue); pfu.update(props); } }