1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.http.security;
15
16 import java.io.IOException;
17 import java.util.Arrays;
18
19 import org.eclipse.jetty.util.log.Log;
20 import org.eclipse.jetty.util.log.Logger;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class Password extends Credential
51 {
52 private static final Logger LOG = Log.getLogger(Password.class);
53
54 private static final long serialVersionUID = 5062906681431569445L;
55
56 public static final String __OBFUSCATE = "OBF:";
57
58 private String _pw;
59
60
61
62
63
64
65
66 public Password(String password)
67 {
68 _pw = password;
69
70
71 while (_pw != null && _pw.startsWith(__OBFUSCATE))
72 _pw = deobfuscate(_pw);
73 }
74
75
76 @Override
77 public String toString()
78 {
79 return _pw;
80 }
81
82
83 public String toStarString()
84 {
85 return "*****************************************************".substring(0, _pw.length());
86 }
87
88
89 @Override
90 public boolean check(Object credentials)
91 {
92 if (this == credentials) return true;
93
94 if (credentials instanceof Password) return credentials.equals(_pw);
95
96 if (credentials instanceof String) return credentials.equals(_pw);
97
98 if (credentials instanceof char[]) return Arrays.equals(_pw.toCharArray(), (char[]) credentials);
99
100 if (credentials instanceof Credential) return ((Credential) credentials).check(_pw);
101
102 return false;
103 }
104
105
106 @Override
107 public boolean equals(Object o)
108 {
109 if (this == o)
110 return true;
111
112 if (null == o)
113 return false;
114
115 if (o instanceof Password)
116 {
117 Password p = (Password) o;
118
119 return p._pw == _pw || (null != _pw && _pw.equals(p._pw));
120 }
121
122 if (o instanceof String)
123 return o.equals(_pw);
124
125 return false;
126 }
127
128
129 @Override
130 public int hashCode()
131 {
132 return null == _pw ? super.hashCode() : _pw.hashCode();
133 }
134
135
136 public static String obfuscate(String s)
137 {
138 StringBuilder buf = new StringBuilder();
139 byte[] b = s.getBytes();
140
141 buf.append(__OBFUSCATE);
142 for (int i = 0; i < b.length; i++)
143 {
144 byte b1 = b[i];
145 byte b2 = b[s.length() - (i + 1)];
146 int i1 = 127 + b1 + b2;
147 int i2 = 127 + b1 - b2;
148 int i0 = i1 * 256 + i2;
149 String x = Integer.toString(i0, 36);
150
151 switch (x.length())
152 {
153 case 1:
154 buf.append('0');
155 buf.append('0');
156 buf.append('0');
157 buf.append(x);
158 break;
159 case 2:
160 buf.append('0');
161 buf.append('0');
162 buf.append(x);
163 break;
164 case 3:
165 buf.append('0');
166 buf.append(x);
167 break;
168 default:
169 buf.append(x);
170 break;
171 }
172 }
173 return buf.toString();
174
175 }
176
177
178 public static String deobfuscate(String s)
179 {
180 if (s.startsWith(__OBFUSCATE)) s = s.substring(4);
181
182 byte[] b = new byte[s.length() / 2];
183 int l = 0;
184 for (int i = 0; i < s.length(); i += 4)
185 {
186 String x = s.substring(i, i + 4);
187 int i0 = Integer.parseInt(x, 36);
188 int i1 = (i0 / 256);
189 int i2 = (i0 % 256);
190 b[l++] = (byte) ((i1 + i2 - 254) / 2);
191 }
192
193 return new String(b, 0, l);
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public static Password getPassword(String realm, String dft, String promptDft)
212 {
213 String passwd = System.getProperty(realm, dft);
214 if (passwd == null || passwd.length() == 0)
215 {
216 try
217 {
218 System.out.print(realm + ((promptDft != null && promptDft.length() > 0) ? " [dft]" : "") + " : ");
219 System.out.flush();
220 byte[] buf = new byte[512];
221 int len = System.in.read(buf);
222 if (len > 0) passwd = new String(buf, 0, len).trim();
223 }
224 catch (IOException e)
225 {
226 LOG.warn(Log.EXCEPTION, e);
227 }
228 if (passwd == null || passwd.length() == 0) passwd = promptDft;
229 }
230 return new Password(passwd);
231 }
232
233
234
235
236
237 public static void main(String[] arg)
238 {
239 if (arg.length != 1 && arg.length != 2)
240 {
241 System.err.println("Usage - java org.eclipse.jetty.security.Password [<user>] <password>");
242 System.err.println("If the password is ?, the user will be prompted for the password");
243 System.exit(1);
244 }
245 String p = arg[arg.length == 1 ? 0 : 1];
246 Password pw = new Password(p);
247 System.err.println(pw.toString());
248 System.err.println(obfuscate(pw.toString()));
249 System.err.println(Credential.MD5.digest(p));
250 if (arg.length == 2) System.err.println(Credential.Crypt.crypt(arg[0], pw.toString()));
251 }
252 }