1 |
|
|
2 |
/* MessageDigest.java --- The message digest interface. |
/* MessageDigest.java --- The message digest interface. |
3 |
Copyright (C) 1999 Free Software Foundation, Inc. |
Copyright (C) 1999, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
6 |
|
|
41 |
public abstract class MessageDigest extends MessageDigestSpi |
public abstract class MessageDigest extends MessageDigestSpi |
42 |
{ |
{ |
43 |
private String algorithm; |
private String algorithm; |
44 |
private Provider provider; |
Provider provider; |
45 |
private byte[] lastDigest; |
private byte[] lastDigest; |
46 |
|
|
47 |
/** |
/** |
64 |
@param algorithm the name of digest algorithm to choose |
@param algorithm the name of digest algorithm to choose |
65 |
@return a MessageDigest representing the desired algorithm |
@return a MessageDigest representing the desired algorithm |
66 |
|
|
67 |
@exception NoSuchAlgorithmException if the algorithm is not implemented by providers |
@exception NoSuchAlgorithmException if the algorithm is not implemented by |
68 |
|
providers |
69 |
*/ |
*/ |
70 |
public static MessageDigest getInstance(String algorithm) |
public static MessageDigest getInstance(String algorithm) |
71 |
throws NoSuchAlgorithmException |
throws NoSuchAlgorithmException |
72 |
{ |
{ |
73 |
Provider[] p = Security.getProviders(); |
Provider[] p = Security.getProviders(); |
|
String name = "MessageDigest." + algorithm; |
|
|
|
|
74 |
for (int i = 0; i < p.length; i++) |
for (int i = 0; i < p.length; i++) |
75 |
{ |
{ |
76 |
String classname = p[i].getProperty(name); |
try |
77 |
if (classname != null) |
{ |
78 |
return getInstance(classname, algorithm, p[i]); |
return getInstance(algorithm, p[i]); |
79 |
|
} |
80 |
|
catch (NoSuchAlgorithmException ignored) {} |
81 |
} |
} |
82 |
|
|
83 |
throw new NoSuchAlgorithmException(algorithm); |
throw new NoSuchAlgorithmException(algorithm); |
94 |
@param provider the name of the provider to find the algorithm in |
@param provider the name of the provider to find the algorithm in |
95 |
@return a MessageDigest representing the desired algorithm |
@return a MessageDigest representing the desired algorithm |
96 |
|
|
97 |
@exception NoSuchAlgorithmException if the algorithm is not implemented by the provider |
@exception NoSuchAlgorithmException if the algorithm is not implemented by |
98 |
|
the provider |
99 |
@exception NoSuchProviderException if the provider is not found |
@exception NoSuchProviderException if the provider is not found |
100 |
*/ |
*/ |
101 |
|
|
107 |
if (p == null) |
if (p == null) |
108 |
throw new NoSuchProviderException(provider); |
throw new NoSuchProviderException(provider); |
109 |
|
|
110 |
return getInstance(p.getProperty("MessageDigest." + algorithm), |
return getInstance(algorithm, p); |
111 |
algorithm, p); |
} |
112 |
|
|
113 |
|
private static MessageDigest getInstance(String algorithm, Provider p) |
114 |
|
throws NoSuchAlgorithmException |
115 |
|
{ |
116 |
|
// try the name as is |
117 |
|
String className = p.getProperty("MessageDigest." + algorithm); |
118 |
|
if (className == null) { // try all uppercase |
119 |
|
String upper = algorithm.toUpperCase(); |
120 |
|
className = p.getProperty("MessageDigest." + upper); |
121 |
|
if (className == null) { // try if it's an alias |
122 |
|
String alias = p.getProperty("Alg.Alias.MessageDigest." +algorithm); |
123 |
|
if (alias == null) { // try all-uppercase alias name |
124 |
|
alias = p.getProperty("Alg.Alias.MessageDigest." +upper); |
125 |
|
if (alias == null) { // spit the dummy |
126 |
|
throw new NoSuchAlgorithmException(algorithm); |
127 |
|
} |
128 |
|
} |
129 |
|
className = p.getProperty("MessageDigest." + alias); |
130 |
|
if (className == null) { |
131 |
|
throw new NoSuchAlgorithmException(algorithm); |
132 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
|
return getInstance(className, algorithm, p); |
136 |
} |
} |
137 |
|
|
138 |
private static MessageDigest getInstance(String classname, |
private static MessageDigest getInstance(String classname, |
143 |
if (classname == null) |
if (classname == null) |
144 |
throw new NoSuchAlgorithmException(algorithm); |
throw new NoSuchAlgorithmException(algorithm); |
145 |
|
|
146 |
|
MessageDigest result = null; |
147 |
try |
try |
148 |
{ |
{ |
149 |
MessageDigest m = |
Object obj = Class.forName(classname).newInstance(); |
150 |
(MessageDigest) Class.forName(classname).newInstance(); |
if (obj instanceof MessageDigest) { |
151 |
m.algorithm = algorithm; |
result = (MessageDigest) obj; |
152 |
m.provider = provider; |
result.algorithm = algorithm; |
153 |
return m; |
} else if (obj instanceof MessageDigestSpi) { |
154 |
|
result = new DummyMessageDigest((MessageDigestSpi) obj, algorithm); |
155 |
|
} else { |
156 |
|
throw new ClassCastException("Class "+classname+" from Provider " |
157 |
|
+provider.getName() |
158 |
|
+" does not extend java.security.MessageDigestSpi"); |
159 |
|
} |
160 |
|
result.provider = provider; |
161 |
|
return result; |
162 |
} |
} |
163 |
catch (ClassNotFoundException cnfe) |
catch (ClassNotFoundException cnfe) |
164 |
{ |
{ |
248 |
then computes a final digest and returns it. It calls |
then computes a final digest and returns it. It calls |
249 |
update(input) and then digest(); |
update(input) and then digest(); |
250 |
|
|
251 |
@param buf An array of bytes to perform final update with |
@param input An array of bytes to perform final update with |
252 |
@return a byte array representing the message digest |
@return a byte array representing the message digest |
253 |
*/ |
*/ |
254 |
public byte[] digest(byte[]input) |
public byte[] digest(byte[]input) |