203 |
return getInstance(algorithm, p); |
return getInstance(algorithm, p); |
204 |
} |
} |
205 |
|
|
206 |
private static Signature getInstance(String algorithm, Provider p) |
/** |
207 |
|
* Generates a <code>Signature</code> object implementing the specified |
208 |
|
* algorithm, as supplied from the specified provider, if such an algorithm |
209 |
|
* is available from the provider. Note: the provider doesn't have to be |
210 |
|
* registered. |
211 |
|
* |
212 |
|
* @param algorithm the name of the algorithm requested. See Appendix A in |
213 |
|
* the Java Cryptography Architecture API Specification & Reference for |
214 |
|
* information about standard algorithm names. |
215 |
|
* @param provider the provider. |
216 |
|
* @return the new <code>Signature</code> object. |
217 |
|
* @throws NoSuchAlgorithmException if the <code>algorithm</code> is not |
218 |
|
* available in the package supplied by the requested <code>provider</code>. |
219 |
|
* @throws IllegalArgumentException if the <code>provider</code> is |
220 |
|
* <code>null</code>. |
221 |
|
* @since 1.4 |
222 |
|
* @see Provider |
223 |
|
*/ |
224 |
|
public static Signature getInstance(String algorithm, Provider provider) |
225 |
throws NoSuchAlgorithmException |
throws NoSuchAlgorithmException |
226 |
{ |
{ |
227 |
|
if (provider == null) |
228 |
|
throw new IllegalArgumentException(); |
229 |
|
|
230 |
// try the name as is |
// try the name as is |
231 |
String className = p.getProperty("Signature." + algorithm); |
String className = provider.getProperty("Signature." + algorithm); |
232 |
if (className == null) // try all uppercase |
if (className == null) // try all uppercase |
233 |
{ |
{ |
234 |
String upper = algorithm.toUpperCase(); |
String upper = algorithm.toUpperCase(); |
235 |
className = p.getProperty("Signature." + upper); |
className = provider.getProperty("Signature." + upper); |
236 |
if (className == null) // try if it's an alias |
if (className == null) // try if it's an alias |
237 |
{ |
{ |
238 |
String alias = p.getProperty("Alg.Alias.Signature." + algorithm); |
String alias = provider.getProperty("Alg.Alias.Signature." + algorithm); |
239 |
if (alias == null) |
if (alias == null) |
240 |
{ |
{ |
241 |
alias = p.getProperty("Alg.Alias.Signature." + upper); |
alias = provider.getProperty("Alg.Alias.Signature." + upper); |
242 |
if (alias == null) // spit the dummy |
if (alias == null) // spit the dummy |
243 |
throw new NoSuchAlgorithmException(algorithm); |
throw new NoSuchAlgorithmException(algorithm); |
244 |
} |
} |
245 |
className = p.getProperty("Signature." + alias); |
className = provider.getProperty("Signature." + alias); |
246 |
if (className == null) |
if (className == null) |
247 |
throw new NoSuchAlgorithmException(algorithm); |
throw new NoSuchAlgorithmException(algorithm); |
248 |
} |
} |
249 |
} |
} |
250 |
return getInstance(className, algorithm, p); |
return getInstance(className, algorithm, provider); |
251 |
} |
} |
252 |
|
|
253 |
private static Signature getInstance(String classname, String algorithm, |
private static Signature getInstance(String classname, String algorithm, |
323 |
* encoded properly or does not include required parameter information or |
* encoded properly or does not include required parameter information or |
324 |
* cannot be used for digital signature purposes. |
* cannot be used for digital signature purposes. |
325 |
*/ |
*/ |
326 |
public final void initVerify(Certificate certificate) |
public final void initVerify(Certificate certificate) |
327 |
throws InvalidKeyException |
throws InvalidKeyException |
328 |
{ |
{ |
329 |
state = VERIFY; |
state = VERIFY; |
451 |
} |
} |
452 |
|
|
453 |
/** |
/** |
454 |
|
* <p>Verifies the passed-in <code>signature</code> in the specified array of |
455 |
|
* bytes, starting at the specified <code>offset</code>.</p> |
456 |
|
* |
457 |
|
* <p>A call to this method resets this signature object to the state it was |
458 |
|
* in when previously initialized for verification via a call to |
459 |
|
* <code>initVerify(PublicKey)</code>. That is, the object is reset and |
460 |
|
* available to verify another signature from the identity whose public key |
461 |
|
* was specified in the call to <code>initVerify()</code>.</p> |
462 |
|
* |
463 |
|
* @param signature the signature bytes to be verified. |
464 |
|
* @param offset the offset to start from in the array of bytes. |
465 |
|
* @param length the number of bytes to use, starting at offset. |
466 |
|
* @return <code>true</code> if the signature was verified, <code>false</code> |
467 |
|
* if not. |
468 |
|
* @throws SignatureException if this signature object is not initialized |
469 |
|
* properly, or the passed-in <code>signature</code> is improperly encoded or |
470 |
|
* of the wrong type, etc. |
471 |
|
* @throws IllegalArgumentException if the <code>signature</code> byte array |
472 |
|
* is <code>null</code>, or the <code>offset</code> or <code>length</code> is |
473 |
|
* less than <code>0</code>, or the sum of the <code>offset</code> and |
474 |
|
* <code>length</code> is greater than the length of the <code>signature</code> |
475 |
|
* byte array. |
476 |
|
*/ |
477 |
|
public final boolean verify(byte[] signature, int offset, int length) |
478 |
|
throws SignatureException |
479 |
|
{ |
480 |
|
if (state != VERIFY) |
481 |
|
throw new SignatureException("illegal state"); |
482 |
|
|
483 |
|
if (signature == null) |
484 |
|
throw new IllegalArgumentException("signaure is null"); |
485 |
|
if (offset < 0) |
486 |
|
throw new IllegalArgumentException("offset is less than 0"); |
487 |
|
if (length < 0) |
488 |
|
throw new IllegalArgumentException("length is less than 0"); |
489 |
|
if (offset + length < signature.length) |
490 |
|
throw new IllegalArgumentException("range is out of bounds"); |
491 |
|
|
492 |
|
state = UNINITIALIZED; |
493 |
|
return engineVerify(signature, offset, length); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
* Updates the data to be signed or verified by a byte. |
* Updates the data to be signed or verified by a byte. |
498 |
* |
* |
499 |
* @param b the byte to use for the update. |
* @param b the byte to use for the update. |
604 |
} |
} |
605 |
|
|
606 |
/** |
/** |
607 |
|
* <p>Returns the parameters used with this signature object.</p> |
608 |
|
* |
609 |
|
* <p>The returned parameters may be the same that were used to initialize |
610 |
|
* this signature, or may contain a combination of default and randomly |
611 |
|
* generated parameter values used by the underlying signature implementation |
612 |
|
* if this signature requires algorithm parameters but was not initialized |
613 |
|
* with any. |
614 |
|
* |
615 |
|
* @return the parameters used with this signature, or <code>null</code> if |
616 |
|
* this signature does not use any parameters. |
617 |
|
* @see #setParameter(AlgorithmParameterSpec) |
618 |
|
*/ |
619 |
|
public final AlgorithmParameters getParameters() |
620 |
|
{ |
621 |
|
return engineGetParameters(); |
622 |
|
} |
623 |
|
|
624 |
|
/** |
625 |
* Gets the value of the specified algorithm parameter. This method supplies |
* Gets the value of the specified algorithm parameter. This method supplies |
626 |
* a general-purpose mechanism through which it is possible to get the various |
* a general-purpose mechanism through which it is possible to get the various |
627 |
* parameters of this object. A parameter may be any settable parameter for |
* parameters of this object. A parameter may be any settable parameter for |