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