jdk/src/share/classes/sun/security/util/Password.java
changeset 2 90ce3da70b43
child 3624 e214b718aeef
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.security.util;
       
    27 
       
    28 import java.io.*;
       
    29 import java.nio.*;
       
    30 import java.nio.charset.*;
       
    31 import java.util.Arrays;
       
    32 
       
    33 /**
       
    34  * A utility class for reading passwords
       
    35  *
       
    36  */
       
    37 public class Password {
       
    38     /** Reads user password from given input stream. */
       
    39     public static char[] readPassword(InputStream in) throws IOException {
       
    40 
       
    41         char[] consoleEntered = null;
       
    42         byte[] consoleBytes = null;
       
    43 
       
    44         try {
       
    45             // Use the new java.io.Console class
       
    46             Console con = null;
       
    47             if (in == System.in && ((con = System.console()) != null)) {
       
    48                 consoleEntered = con.readPassword();
       
    49                 // readPassword returns "" if you just print ENTER,
       
    50                 // to be compatible with old Password class, change to null
       
    51                 if (consoleEntered != null && consoleEntered.length == 0) {
       
    52                     return null;
       
    53                 }
       
    54                 consoleBytes = convertToBytes(consoleEntered);
       
    55                 in = new ByteArrayInputStream(consoleBytes);
       
    56             }
       
    57 
       
    58             // Rest of the lines still necessary for KeyStoreLoginModule
       
    59             // and when there is no console.
       
    60 
       
    61             char[] lineBuffer;
       
    62             char[] buf;
       
    63             int i;
       
    64 
       
    65             buf = lineBuffer = new char[128];
       
    66 
       
    67             int room = buf.length;
       
    68             int offset = 0;
       
    69             int c;
       
    70 
       
    71             boolean done = false;
       
    72             while (!done) {
       
    73                 switch (c = in.read()) {
       
    74                   case -1:
       
    75                   case '\n':
       
    76                       done = true;
       
    77                       break;
       
    78 
       
    79                   case '\r':
       
    80                     int c2 = in.read();
       
    81                     if ((c2 != '\n') && (c2 != -1)) {
       
    82                         if (!(in instanceof PushbackInputStream)) {
       
    83                             in = new PushbackInputStream(in);
       
    84                         }
       
    85                         ((PushbackInputStream)in).unread(c2);
       
    86                     } else {
       
    87                         done = true;
       
    88                         break;
       
    89                     }
       
    90 
       
    91                   default:
       
    92                     if (--room < 0) {
       
    93                         buf = new char[offset + 128];
       
    94                         room = buf.length - offset - 1;
       
    95                         System.arraycopy(lineBuffer, 0, buf, 0, offset);
       
    96                         Arrays.fill(lineBuffer, ' ');
       
    97                         lineBuffer = buf;
       
    98                     }
       
    99                     buf[offset++] = (char) c;
       
   100                     break;
       
   101                 }
       
   102             }
       
   103 
       
   104             if (offset == 0) {
       
   105                 return null;
       
   106             }
       
   107 
       
   108             char[] ret = new char[offset];
       
   109             System.arraycopy(buf, 0, ret, 0, offset);
       
   110             Arrays.fill(buf, ' ');
       
   111 
       
   112             return ret;
       
   113         } finally {
       
   114             if (consoleEntered != null) {
       
   115                 Arrays.fill(consoleEntered, ' ');
       
   116             }
       
   117             if (consoleBytes != null) {
       
   118                 Arrays.fill(consoleBytes, (byte)0);
       
   119             }
       
   120         }
       
   121     }
       
   122 
       
   123     /**
       
   124      * Change a password read from Console.readPassword() into
       
   125      * its original bytes.
       
   126      *
       
   127      * @param pass a char[]
       
   128      * @return its byte[] format, similar to new String(pass).getBytes()
       
   129      */
       
   130     private static byte[] convertToBytes(char[] pass) {
       
   131         if (enc == null) {
       
   132             synchronized (Password.class) {
       
   133                 enc = sun.misc.SharedSecrets.getJavaIOAccess()
       
   134                         .charset()
       
   135                         .newEncoder()
       
   136                         .onMalformedInput(CodingErrorAction.REPLACE)
       
   137                         .onUnmappableCharacter(CodingErrorAction.REPLACE);
       
   138             }
       
   139         }
       
   140         byte[] ba = new byte[(int)(enc.maxBytesPerChar() * pass.length)];
       
   141         ByteBuffer bb = ByteBuffer.wrap(ba);
       
   142         synchronized (enc) {
       
   143             enc.reset().encode(CharBuffer.wrap(pass), bb, true);
       
   144         }
       
   145         if (bb.position() < ba.length) {
       
   146             ba[bb.position()] = '\n';
       
   147         }
       
   148         return ba;
       
   149     }
       
   150     private static volatile CharsetEncoder enc;
       
   151 }