History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/util/Password.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/util/Password.java+159−0
@@ -0,0 +1,159 @@
1/*
2 * Copyright (c) 2003, 2009, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.security.util;
27
28import java.io.*;
29import java.nio.*;
30import java.nio.charset.*;
31import java.util.Arrays;
32
33/**
34 * A utility class for reading passwords
35 *
36 */
37public class Password {
38 /** Reads user password from given input stream. */
39 public static char[] readPassword(InputStream in) throws IOException {
40 return readPassword(in, false);
41 }
42
43 /** Reads user password from given input stream.
44 * @param isEchoOn true if the password should be echoed on the screen
45 */
46 public static char[] readPassword(InputStream in, boolean isEchoOn)
47 throws IOException {
48
49 char[] consoleEntered = null;
50 byte[] consoleBytes = null;
51
52 try {
53 // Use the new java.io.Console class
54 Console con = null;
55 if (!isEchoOn && in == System.in && ((con = System.console()) != null)) {
56 consoleEntered = con.readPassword();
57 // readPassword returns "" if you just print ENTER,
58 // to be compatible with old Password class, change to null
59 if (consoleEntered != null && consoleEntered.length == 0) {
60 return null;
61 }
62 consoleBytes = convertToBytes(consoleEntered);
63 in = new ByteArrayInputStream(consoleBytes);
64 }
65
66 // Rest of the lines still necessary for KeyStoreLoginModule
67 // and when there is no console.
68
69 char[] lineBuffer;
70 char[] buf;
71 int i;
72
73 buf = lineBuffer = new char[128];
74
75 int room = buf.length;
76 int offset = 0;
77 int c;
78
79 boolean done = false;
80 while (!done) {
81 switch (c = in.read()) {
82 case -1:
83 case '\n':
84 done = true;
85 break;
86
87 case '\r':
88 int c2 = in.read();
89 if ((c2 != '\n') && (c2 != -1)) {
90 if (!(in instanceof PushbackInputStream)) {
91 in = new PushbackInputStream(in);
92 }
93 ((PushbackInputStream)in).unread(c2);
94 } else {
95 done = true;
96 break;
97 }
98
99 default:
100 if (--room < 0) {
101 buf = new char[offset + 128];
102 room = buf.length - offset - 1;
103 System.arraycopy(lineBuffer, 0, buf, 0, offset);
104 Arrays.fill(lineBuffer, ' ');
105 lineBuffer = buf;
106 }
107 buf[offset++] = (char) c;
108 break;
109 }
110 }
111
112 if (offset == 0) {
113 return null;
114 }
115
116 char[] ret = new char[offset];
117 System.arraycopy(buf, 0, ret, 0, offset);
118 Arrays.fill(buf, ' ');
119
120 return ret;
121 } finally {
122 if (consoleEntered != null) {
123 Arrays.fill(consoleEntered, ' ');
124 }
125 if (consoleBytes != null) {
126 Arrays.fill(consoleBytes, (byte)0);
127 }
128 }
129 }
130
131 /**
132 * Change a password read from Console.readPassword() into
133 * its original bytes.
134 *
135 * @param pass a char[]
136 * @return its byte[] format, similar to new String(pass).getBytes()
137 */
138 private static byte[] convertToBytes(char[] pass) {
139 if (enc == null) {
140 synchronized (Password.class) {
141 enc = sun.misc.SharedSecrets.getJavaIOAccess()
142 .charset()
143 .newEncoder()
144 .onMalformedInput(CodingErrorAction.REPLACE)
145 .onUnmappableCharacter(CodingErrorAction.REPLACE);
146 }
147 }
148 byte[] ba = new byte[(int)(enc.maxBytesPerChar() * pass.length)];
149 ByteBuffer bb = ByteBuffer.wrap(ba);
150 synchronized (enc) {
151 enc.reset().encode(CharBuffer.wrap(pass), bb, true);
152 }
153 if (bb.position() < ba.length) {
154 ba[bb.position()] = '\n';
155 }
156 return ba;
157 }
158 private static volatile CharsetEncoder enc;
159}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t