0N/A/*
0N/A * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.util;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.charset.*;
0N/Aimport java.util.Arrays;
0N/A
0N/A/**
0N/A * A utility class for reading passwords
0N/A *
0N/A */
0N/Apublic class Password {
0N/A /** Reads user password from given input stream. */
0N/A public static char[] readPassword(InputStream in) throws IOException {
0N/A return readPassword(in, false);
0N/A }
0N/A
0N/A /** Reads user password from given input stream.
0N/A * @param isEchoOn true if the password should be echoed on the screen
0N/A */
0N/A public static char[] readPassword(InputStream in, boolean isEchoOn)
0N/A throws IOException {
0N/A
0N/A char[] consoleEntered = null;
0N/A byte[] consoleBytes = null;
0N/A
0N/A try {
0N/A // Use the new java.io.Console class
0N/A Console con = null;
0N/A if (!isEchoOn && in == System.in && ((con = System.console()) != null)) {
0N/A consoleEntered = con.readPassword();
0N/A // readPassword returns "" if you just print ENTER,
0N/A // to be compatible with old Password class, change to null
0N/A if (consoleEntered != null && consoleEntered.length == 0) {
0N/A return null;
0N/A }
0N/A consoleBytes = convertToBytes(consoleEntered);
0N/A in = new ByteArrayInputStream(consoleBytes);
0N/A }
0N/A
0N/A // Rest of the lines still necessary for KeyStoreLoginModule
0N/A // and when there is no console.
0N/A
0N/A char[] lineBuffer;
0N/A char[] buf;
0N/A int i;
0N/A
0N/A buf = lineBuffer = new char[128];
0N/A
0N/A int room = buf.length;
0N/A int offset = 0;
0N/A int c;
0N/A
0N/A boolean done = false;
0N/A while (!done) {
0N/A switch (c = in.read()) {
0N/A case -1:
0N/A case '\n':
0N/A done = true;
0N/A break;
0N/A
0N/A case '\r':
0N/A int c2 = in.read();
0N/A if ((c2 != '\n') && (c2 != -1)) {
0N/A if (!(in instanceof PushbackInputStream)) {
0N/A in = new PushbackInputStream(in);
0N/A }
0N/A ((PushbackInputStream)in).unread(c2);
0N/A } else {
0N/A done = true;
0N/A break;
0N/A }
0N/A
0N/A default:
0N/A if (--room < 0) {
0N/A buf = new char[offset + 128];
0N/A room = buf.length - offset - 1;
0N/A System.arraycopy(lineBuffer, 0, buf, 0, offset);
0N/A Arrays.fill(lineBuffer, ' ');
0N/A lineBuffer = buf;
0N/A }
0N/A buf[offset++] = (char) c;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (offset == 0) {
0N/A return null;
0N/A }
0N/A
0N/A char[] ret = new char[offset];
0N/A System.arraycopy(buf, 0, ret, 0, offset);
0N/A Arrays.fill(buf, ' ');
0N/A
0N/A return ret;
0N/A } finally {
0N/A if (consoleEntered != null) {
0N/A Arrays.fill(consoleEntered, ' ');
0N/A }
0N/A if (consoleBytes != null) {
0N/A Arrays.fill(consoleBytes, (byte)0);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Change a password read from Console.readPassword() into
0N/A * its original bytes.
0N/A *
0N/A * @param pass a char[]
0N/A * @return its byte[] format, similar to new String(pass).getBytes()
0N/A */
0N/A private static byte[] convertToBytes(char[] pass) {
0N/A if (enc == null) {
0N/A synchronized (Password.class) {
0N/A enc = sun.misc.SharedSecrets.getJavaIOAccess()
0N/A .charset()
0N/A .newEncoder()
0N/A .onMalformedInput(CodingErrorAction.REPLACE)
0N/A .onUnmappableCharacter(CodingErrorAction.REPLACE);
0N/A }
0N/A }
0N/A byte[] ba = new byte[(int)(enc.maxBytesPerChar() * pass.length)];
0N/A ByteBuffer bb = ByteBuffer.wrap(ba);
0N/A synchronized (enc) {
0N/A enc.reset().encode(CharBuffer.wrap(pass), bb, true);
0N/A }
0N/A if (bb.position() < ba.length) {
0N/A ba[bb.position()] = '\n';
0N/A }
0N/A return ba;
0N/A }
0N/A private static volatile CharsetEncoder enc;
0N/A}
0N/A