1 |
/* Signature.java --- Signature Class |
/* Signature.java --- Signature Class |
2 |
Copyright (C) 1999 Free Software Foundation, Inc. |
Copyright (C) 1999, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
58 |
|
|
59 |
1. Initialing |
1. Initialing |
60 |
|
|
61 |
* It must be initialized with a private key for |
* It must be initialized with a private key for signing. |
62 |
signing. |
* It must be initialized with a public key for verifying. |
|
* It must be initialized with a public key for |
|
|
verifying. |
|
63 |
|
|
64 |
2. Updating |
2. Updating |
65 |
|
|
66 |
Update the bytes for signing or verifying with calls |
Update the bytes for signing or verifying with calls to update. |
|
to update. |
|
67 |
|
|
68 |
3. Signing or Verify the signature on the currently stored |
3. Signing or Verify the signature on the currently stored |
69 |
bytes by calling sign or verify. |
bytes by calling sign or verify. |
97 |
protected int state = UNINITIALIZED; |
protected int state = UNINITIALIZED; |
98 |
|
|
99 |
private String algorithm; |
private String algorithm; |
100 |
private Provider provider; |
Provider provider; |
101 |
|
|
102 |
/** |
/** |
103 |
Creates a new signature for this algorithm. |
Creates a new signature for this algorithm. |
110 |
state = UNINITIALIZED; |
state = UNINITIALIZED; |
111 |
} |
} |
112 |
|
|
113 |
/** |
/** |
114 |
Gets an instance of the Signature class representing |
Gets an instance of the Signature class representing |
115 |
the specified signature. If the algorithm is not found then, |
the specified signature. If the algorithm is not found then, |
116 |
it throws NoSuchAlgorithmException. |
it throws NoSuchAlgorithmException. |
118 |
@param algorithm the name of signature algorithm to choose |
@param algorithm the name of signature algorithm to choose |
119 |
@return a Signature repesenting the desired algorithm |
@return a Signature repesenting the desired algorithm |
120 |
|
|
121 |
@throws NoSuchAlgorithmException if the algorithm is not implemented by providers |
@throws NoSuchAlgorithmException if the algorithm is not implemented by |
122 |
|
providers |
123 |
*/ |
*/ |
124 |
public static Signature getInstance(String algorithm) |
public static Signature getInstance(String algorithm) |
125 |
throws NoSuchAlgorithmException |
throws NoSuchAlgorithmException |
126 |
{ |
{ |
|
String name = "Signature." + algorithm; |
|
127 |
Provider[] p = Security.getProviders(); |
Provider[] p = Security.getProviders(); |
128 |
|
|
129 |
for (int i = 0; i < p.length; i++) |
for (int i = 0; i < p.length; i++) |
130 |
{ |
{ |
131 |
String classname = p[i].getProperty(name); |
try |
132 |
if (classname != null) |
{ |
133 |
return getInstance(classname, algorithm, p[i]); |
return getInstance(algorithm, p[i]); |
134 |
|
} |
135 |
|
catch (NoSuchAlgorithmException ignored) {} |
136 |
} |
} |
137 |
|
|
138 |
throw new NoSuchAlgorithmException(algorithm); |
throw new NoSuchAlgorithmException(algorithm); |
149 |
@param provider the name of the provider to find the algorithm in |
@param provider the name of the provider to find the algorithm in |
150 |
@return a Signature repesenting the desired algorithm |
@return a Signature repesenting the desired algorithm |
151 |
|
|
152 |
@throws NoSuchAlgorithmException if the algorithm is not implemented by the provider |
@throws NoSuchAlgorithmException if the algorithm is not implemented by |
153 |
|
the provider |
154 |
@throws NoSuchProviderException if the provider is not found |
@throws NoSuchProviderException if the provider is not found |
155 |
*/ |
*/ |
156 |
public static Signature getInstance(String algorithm, String provider) |
public static Signature getInstance(String algorithm, String provider) |
158 |
{ |
{ |
159 |
Provider p = Security.getProvider(provider); |
Provider p = Security.getProvider(provider); |
160 |
if (p == null) |
if (p == null) |
161 |
throw new NoSuchProviderException(); |
throw new NoSuchProviderException(provider); |
162 |
|
|
163 |
|
return getInstance(algorithm, p); |
164 |
|
} |
165 |
|
|
166 |
return getInstance(p.getProperty("Signature." + algorithm), algorithm, p); |
private static Signature getInstance(String algorithm, Provider p) |
167 |
|
throws NoSuchAlgorithmException |
168 |
|
{ |
169 |
|
// try the name as is |
170 |
|
String className = p.getProperty("Signature." + algorithm); |
171 |
|
if (className == null) { // try all uppercase |
172 |
|
String upper = algorithm.toUpperCase(); |
173 |
|
className = p.getProperty("Signature." + upper); |
174 |
|
if (className == null) { // try if it's an alias |
175 |
|
String alias = p.getProperty("Alg.Alias.Signature." + algorithm); |
176 |
|
if (alias == null) { |
177 |
|
alias = p.getProperty("Alg.Alias.Signature." + upper); |
178 |
|
if (alias == null) { // spit the dummy |
179 |
|
throw new NoSuchAlgorithmException(algorithm); |
180 |
|
} |
181 |
|
} |
182 |
|
className = p.getProperty("Signature." + alias); |
183 |
|
if (className == null) { |
184 |
|
throw new NoSuchAlgorithmException(algorithm); |
185 |
|
} |
186 |
|
} |
187 |
|
} |
188 |
|
return getInstance(className, algorithm, p); |
189 |
} |
} |
190 |
|
|
191 |
private static Signature getInstance(String classname, |
private static Signature getInstance(String classname, |
198 |
Object o = Class.forName(classname).newInstance(); |
Object o = Class.forName(classname).newInstance(); |
199 |
Signature sig; |
Signature sig; |
200 |
if (o instanceof SignatureSpi) |
if (o instanceof SignatureSpi) |
201 |
sig = (Signature) (new DummySignature((SignatureSpi) o, algorithm)); |
sig = new DummySignature((SignatureSpi) o, algorithm); |
202 |
else |
else |
203 |
{ |
{ |
204 |
sig = (Signature) o; |
sig = (Signature) o; |
225 |
/** |
/** |
226 |
Gets the provider that the Signature is from. |
Gets the provider that the Signature is from. |
227 |
|
|
228 |
@return the provider the this Signature |
@return the provider of this Signature |
229 |
*/ |
*/ |
230 |
public final Provider getProvider() |
public final Provider getProvider() |
231 |
{ |
{ |
335 |
initial state and can be used to generate additional |
initial state and can be used to generate additional |
336 |
signatures. |
signatures. |
337 |
|
|
338 |
@param outbuff array of bytes |
@param outbuf array of bytes |
339 |
@param offset the offset to start at in the array |
@param offset the offset to start at in the array |
340 |
@param len the length of the bytes to put into the array. |
@param len the length of the bytes to put into the array. |
341 |
Neither this method or the GNU provider will |
Neither this method or the GNU provider will |
350 |
|
|
351 |
@since JDK 1.2 |
@since JDK 1.2 |
352 |
*/ |
*/ |
353 |
public final int sign(byte[]outbuf, int offset, int len) |
public final int sign(byte[] outbuf, int offset, int len) |
354 |
throws SignatureException |
throws SignatureException |
355 |
{ |
{ |
356 |
if (state == SIGN) |
if (state == SIGN) |