source: trunk/src/gcc/libjava/java/security/MessageDigest.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.9 KB
Line 
1/* MessageDigest.java --- The message digest interface.
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.security;
39
40public abstract class MessageDigest extends MessageDigestSpi
41{
42 private String algorithm;
43 private Provider provider;
44 private byte[] lastDigest;
45
46 /**
47 Creates a MessageDigest representing the specified
48 algorithm.
49
50 @param algorithm the name of digest algorithm to choose
51 */
52 protected MessageDigest(String algorithm)
53 {
54 this.algorithm = algorithm;
55 provider = null;
56 }
57
58 /**
59 Gets an instance of the MessageDigest class representing
60 the specified digest. If the algorithm is not found then,
61 it throws NoSuchAlgorithmException.
62
63 @param algorithm the name of digest algorithm to choose
64 @return a MessageDigest representing the desired algorithm
65
66 @exception NoSuchAlgorithmException if the algorithm is not implemented by providers
67 */
68 public static MessageDigest getInstance(String algorithm)
69 throws NoSuchAlgorithmException
70 {
71 Provider[] p = Security.getProviders();
72 String name = "MessageDigest." + algorithm;
73
74 for (int i = 0; i < p.length; i++)
75 {
76 String classname = p[i].getProperty(name);
77 if (classname != null)
78 return getInstance(classname, algorithm, p[i]);
79 }
80
81 throw new NoSuchAlgorithmException(algorithm);
82 }
83
84 /**
85 Gets an instance of the MessageDigest class representing
86 the specified digest from the specified provider. If the
87 algorithm is not found then, it throws NoSuchAlgorithmException.
88 If the provider is not found, then it throws
89 NoSuchProviderException.
90
91 @param algorithm the name of digest algorithm to choose
92 @param provider the name of the provider to find the algorithm in
93 @return a MessageDigest representing the desired algorithm
94
95 @exception NoSuchAlgorithmException if the algorithm is not implemented by the provider
96 @exception NoSuchProviderException if the provider is not found
97 */
98
99 public static MessageDigest getInstance(String algorithm, String provider)
100 throws NoSuchAlgorithmException, NoSuchProviderException
101 {
102 Provider p = Security.getProvider(provider);
103
104 if (p == null)
105 throw new NoSuchProviderException(provider);
106
107 return getInstance(p.getProperty("MessageDigest." + algorithm),
108 algorithm, p);
109 }
110
111 private static MessageDigest getInstance(String classname,
112 String algorithm,
113 Provider provider)
114 throws NoSuchAlgorithmException
115 {
116 if (classname == null)
117 throw new NoSuchAlgorithmException(algorithm);
118
119 try
120 {
121 MessageDigest m =
122 (MessageDigest) Class.forName(classname).newInstance();
123 m.algorithm = algorithm;
124 m.provider = provider;
125 return m;
126 }
127 catch (ClassNotFoundException cnfe)
128 {
129 throw new NoSuchAlgorithmException(algorithm + ": Class not found.");
130 }
131 catch (InstantiationException ie)
132 {
133 throw new NoSuchAlgorithmException(algorithm
134 + ": Class instantiation failed.");
135 }
136 catch (IllegalAccessException iae)
137 {
138 throw new NoSuchAlgorithmException(algorithm + ": Illegal Access");
139 }
140 }
141
142
143 /**
144 Gets the provider that the MessageDigest is from.
145
146 @return the provider the this MessageDigest
147 */
148 public final Provider getProvider()
149 {
150 return provider;
151 }
152
153 /**
154 Updates the digest with the byte.
155
156 @param input byte to update the digest with
157 */
158 public void update(byte input)
159 {
160 engineUpdate(input);
161 }
162
163 /**
164 Updates the digest with the bytes from the array from the
165 specified offset to the specified length.
166
167 @param input bytes to update the digest with
168 @param offset the offset to start at
169 @param len length of the data to update with
170 */
171 public void update(byte[]input, int offset, int len)
172 {
173 engineUpdate(input, 0, input.length);
174 }
175
176 /**
177 Updates the digest with the bytes from the array.
178
179 @param input bytes to update the digest with
180 */
181 public void update(byte[]input)
182 {
183 engineUpdate(input, 0, input.length);
184 }
185
186 /**
187 Computes the digest of the stored data.
188
189 @return a byte array representing the message digest
190 */
191 public byte[] digest()
192 {
193 return lastDigest = engineDigest();
194 }
195
196 /**
197 Computes the final digest of the stored bytes and returns
198 them.
199
200 @param buf An array of bytes to store the digest
201 @param offset An offset to start storing the digest at
202 @param len The length of the buffer
203 @return Returns the length of the buffer
204 */
205 public int digest(byte[]buf, int offset, int len) throws DigestException
206 {
207 return engineDigest(buf, offset, len);
208 }
209
210 /**
211 Computes a final update using the input array of bytes,
212 then computes a final digest and returns it. It calls
213 update(input) and then digest();
214
215 @param buf An array of bytes to perform final update with
216 @return a byte array representing the message digest
217 */
218 public byte[] digest(byte[]input)
219 {
220 update(input);
221 return digest();
222 }
223
224 /**
225 Returns a representation of the MessageDigest as a String.
226
227 @return a string representing the message digest
228 */
229 public String toString()
230 {
231 return (getClass()).getName()
232 + " Message Digest <" + digestToString() + ">";
233 }
234
235 /**
236 Does a simple byte comparison of the two digests.
237
238 @param digesta first digest to compare
239 @param digestb second digest to compare
240 @return true if they are equal, false otherwise
241 */
242 public static boolean isEqual(byte[]digesta, byte[]digestb)
243 {
244 if (digesta.length != digestb.length)
245 return false;
246
247 for (int i = digesta.length - 1; i >= 0; --i)
248 if (digesta[i] != digestb[i])
249 return false;
250
251 return true;
252 }
253
254
255 /**
256 Resets the message digest.
257 */
258 public void reset()
259 {
260 engineReset();
261 }
262
263 /**
264 Gets the name of the algorithm currently used.
265 The names of algorithms are usually SHA-1 or MD5.
266
267 @return name of algorithm.
268 */
269 public final String getAlgorithm()
270 {
271 return algorithm;
272 }
273
274 /**
275 Gets the length of the message digest.
276 The default is zero which means that this message digest
277 does not implement this function.
278
279 @return length of the message digest
280 */
281 public final int getDigestLength()
282 {
283 return engineGetDigestLength();
284 }
285
286 /**
287 Returns a clone of this class if supported.
288 If it does not then it throws CloneNotSupportedException.
289 The cloning of this class depends on whether the subclass
290 MessageDigestSpi implements Cloneable which contains the
291 actual implementation of the appropriate algorithm.
292
293 @return clone of this class
294
295 @exception CloneNotSupportedException this class does not support cloning
296 */
297 public Object clone() throws CloneNotSupportedException
298 {
299 if (this instanceof Cloneable)
300 return super.clone();
301 else
302 throw new CloneNotSupportedException();
303 }
304
305 private String digestToString()
306 {
307 byte[] digest = lastDigest;
308
309 if (digest == null)
310 return "incomplete";
311
312 StringBuffer buf = new StringBuffer();
313 int len = digest.length;
314 for (int i = 0; i < len; ++i)
315 {
316 byte b = digest[i];
317 byte high = (byte) ((b & 0xff) >>> 4);
318 byte low = (byte) (b & 0xf);
319
320 buf.append(high > 9 ? ('a' - 10) + high : '0' + high);
321 buf.append(low > 9 ? ('a' - 10) + low : '0' + low);
322 }
323
324 return buf.toString();
325 }
326
327}
Note: See TracBrowser for help on using the repository browser.