1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.jetty.security;
16
17 import java.security.MessageDigest;
18
19 import org.mortbay.log.Log;
20 import org.mortbay.util.StringUtil;
21 import org.mortbay.util.TypeUtil;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public abstract class Credential
39 {
40
41
42
43
44
45
46
47
48 public abstract boolean check(Object credentials);
49
50
51
52
53
54
55
56
57
58 public static Credential getCredential(String credential)
59 {
60 if (credential.startsWith(Crypt.__TYPE))
61 return new Crypt(credential);
62 if (credential.startsWith(MD5.__TYPE))
63 return new MD5(credential);
64
65 return new Password(credential);
66 }
67
68
69
70
71
72 public static class Crypt extends Credential
73 {
74 public static final String __TYPE="CRYPT:";
75
76 private String _cooked;
77 Crypt(String cooked)
78 {
79 _cooked=cooked.startsWith(Crypt.__TYPE)
80 ?cooked.substring(__TYPE.length())
81 :cooked;
82 }
83
84 public boolean check(Object credentials)
85 {
86 if (!(credentials instanceof String) &&
87 !(credentials instanceof Password))
88 Log.warn("Can't check "+credentials.getClass()+" against CRYPT");
89
90 String passwd = credentials.toString();
91 return _cooked.equals(UnixCrypt.crypt(passwd,_cooked));
92 }
93
94 public static String crypt(String user,String pw)
95 {
96 return "CRYPT:"+UnixCrypt.crypt(pw,user);
97 }
98 }
99
100
101
102
103 public static class MD5 extends Credential
104 {
105 public static final String __TYPE="MD5:";
106 public static final Object __md5Lock = new Object();
107 private static MessageDigest __md;
108
109 private byte[] _digest;
110
111
112 MD5(String digest)
113 {
114 digest=digest.startsWith(__TYPE)
115 ?digest.substring(__TYPE.length())
116 :digest;
117 _digest=TypeUtil.parseBytes(digest,16);
118 }
119
120
121 public byte[] getDigest()
122 {
123 return _digest;
124 }
125
126
127 public boolean check(Object credentials)
128 {
129 try
130 {
131 byte[] digest=null;
132
133 if (credentials instanceof Password ||
134 credentials instanceof String)
135 {
136 synchronized(__md5Lock)
137 {
138 if (__md==null)
139 __md = MessageDigest.getInstance("MD5");
140 __md.reset();
141 __md.update(credentials.toString().getBytes(StringUtil.__ISO_8859_1));
142 digest=__md.digest();
143 }
144 if (digest==null || digest.length!=_digest.length)
145 return false;
146 for (int i=0;i<digest.length;i++)
147 if (digest[i]!=_digest[i])
148 return false;
149 return true;
150 }
151 else if (credentials instanceof MD5)
152 {
153 MD5 md5 = (MD5)credentials;
154 if (_digest.length!=md5._digest.length)
155 return false;
156 for (int i=0;i<_digest.length;i++)
157 if (_digest[i]!=md5._digest[i])
158 return false;
159 return true;
160 }
161 else if(credentials instanceof Credential)
162 {
163
164
165 return ((Credential)credentials).check(this);
166 }
167 else
168 {
169 Log.warn("Can't check "+credentials.getClass()+" against MD5");
170 return false;
171 }
172 }
173 catch (Exception e)
174 {
175 Log.warn(e);
176 return false;
177 }
178 }
179
180
181 public static String digest(String password)
182 {
183 try
184 {
185 byte[] digest;
186 synchronized(__md5Lock)
187 {
188 if (__md==null)
189 {
190 try{__md = MessageDigest.getInstance("MD5");}
191 catch (Exception e ) {Log.warn(e);return null;}
192 }
193
194 __md.reset();
195 __md.update(password.getBytes(StringUtil.__ISO_8859_1));
196 digest=__md.digest();
197 }
198
199 return __TYPE+TypeUtil.toString(digest,16);
200 }
201 catch (Exception e)
202 {
203 Log.warn(e);
204 return null;
205 }
206 }
207 }
208 }