Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/util/Password.java
38830 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627import java.io.*;28import java.nio.*;29import java.nio.charset.*;30import java.util.Arrays;3132/**33* A utility class for reading passwords34*35*/36public class Password {37/** Reads user password from given input stream. */38public static char[] readPassword(InputStream in) throws IOException {39return readPassword(in, false);40}4142/** Reads user password from given input stream.43* @param isEchoOn true if the password should be echoed on the screen44*/45@SuppressWarnings("fallthrough")46public static char[] readPassword(InputStream in, boolean isEchoOn)47throws IOException {4849char[] consoleEntered = null;50byte[] consoleBytes = null;5152try {53// Use the new java.io.Console class54Console con = null;55if (!isEchoOn && in == System.in && ((con = System.console()) != null)) {56consoleEntered = con.readPassword();57// readPassword returns "" if you just print ENTER,58// to be compatible with old Password class, change to null59if (consoleEntered != null && consoleEntered.length == 0) {60return null;61}62consoleBytes = convertToBytes(consoleEntered);63in = new ByteArrayInputStream(consoleBytes);64}6566// Rest of the lines still necessary for KeyStoreLoginModule67// and when there is no console.6869char[] lineBuffer;70char[] buf;71int i;7273buf = lineBuffer = new char[128];7475int room = buf.length;76int offset = 0;77int c;7879boolean done = false;80while (!done) {81switch (c = in.read()) {82case -1:83case '\n':84done = true;85break;8687case '\r':88int c2 = in.read();89if ((c2 != '\n') && (c2 != -1)) {90if (!(in instanceof PushbackInputStream)) {91in = new PushbackInputStream(in);92}93((PushbackInputStream)in).unread(c2);94} else {95done = true;96break;97}98/* fall through */99default:100if (--room < 0) {101buf = new char[offset + 128];102room = buf.length - offset - 1;103System.arraycopy(lineBuffer, 0, buf, 0, offset);104Arrays.fill(lineBuffer, ' ');105lineBuffer = buf;106}107buf[offset++] = (char) c;108break;109}110}111112if (offset == 0) {113return null;114}115116char[] ret = new char[offset];117System.arraycopy(buf, 0, ret, 0, offset);118Arrays.fill(buf, ' ');119120return ret;121} finally {122if (consoleEntered != null) {123Arrays.fill(consoleEntered, ' ');124}125if (consoleBytes != null) {126Arrays.fill(consoleBytes, (byte)0);127}128}129}130131/**132* Change a password read from Console.readPassword() into133* its original bytes.134*135* @param pass a char[]136* @return its byte[] format, similar to new String(pass).getBytes()137*/138private static byte[] convertToBytes(char[] pass) {139if (enc == null) {140synchronized (Password.class) {141enc = sun.misc.SharedSecrets.getJavaIOAccess()142.charset()143.newEncoder()144.onMalformedInput(CodingErrorAction.REPLACE)145.onUnmappableCharacter(CodingErrorAction.REPLACE);146}147}148byte[] ba = new byte[(int)(enc.maxBytesPerChar() * pass.length)];149ByteBuffer bb = ByteBuffer.wrap(ba);150synchronized (enc) {151enc.reset().encode(CharBuffer.wrap(pass), bb, true);152}153if (bb.position() < ba.length) {154ba[bb.position()] = '\n';155}156return ba;157}158private static volatile CharsetEncoder enc;159}160161162