1 /*
2 * Copyright �, Aegeus Technology Limited.
3 * All rights reserved.
4 */
5 package jsdsi.util;
6
7 import java.security.MessageDigest;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.NoSuchProviderException;
10 import java.security.Signature;
11
12 import jsdsi.JsdsiRuntimeException;
13
14
15 /***
16 * <p>Experimental class and as such may be removed without warning.</p>
17 * Utility class for Signatures.
18 *
19 * @author Sean Radford
20 * @version $Revision: 1.7 $ $Date: 2004/11/08 12:08:08 $
21 */
22 public class SignatureUtils {
23
24 /***
25 *
26 */
27 private SignatureUtils() {
28 super();
29 }
30
31 /***
32 * Return a java.security.Signature. The required algorythm is usually generated from {@link Algorithms}:
33 * <pre>
34 * Algorithms.jdkSignatureName(DigestAlgoEnum.XXX, KeyEnum.XXX)
35 * </pre>
36 * @param jdkSignatureAlgo signature algorythm required in JDK format
37 * @return the Signature
38 * @throws jsdsi.JsdsiRuntimeException on error
39 */
40 public static Signature getJdkSignature(String jdkSignatureAlgo) {
41 return getJdkSignature(jdkSignatureAlgo, null);
42 }
43
44 /***
45 * Return a java.security.Signature. The required algorythm is usually generated from {@link Algorithms}:
46 * <pre>
47 * Algorithms.jdkSignatureName(DigestAlgoEnum.XXX, KeyEnum.XXX)
48 * </pre>
49 * @param jdkSignatureAlgo signature algorythm required in JDK format
50 * @param provider the Provider to use
51 * @return the Signature
52 * @throws jsdsi.JsdsiRuntimeException on error
53 */
54 public static Signature getJdkSignature(String jdkSignatureAlgo,
55 String provider) {
56 if (jdkSignatureAlgo == null || jdkSignatureAlgo.equals("")) {
57 throw new IllegalArgumentException("jdkSignatureAlgo is NULL");
58 }
59 try {
60 if (provider==null) {
61 return Signature.getInstance(jdkSignatureAlgo);
62 } else {
63 return Signature.getInstance(jdkSignatureAlgo, provider);
64 }
65 } catch (NoSuchAlgorithmException e) {
66 throw new JsdsiRuntimeException("No such Signature algorythm: " + jdkSignatureAlgo);
67 } catch (NoSuchProviderException e) {
68 throw new JsdsiRuntimeException("No such Signature provider: " + provider);
69 }
70 }
71
72 /***
73 * Returns the MessageDigester for a JDK Signature algorythm
74 * @param jdkSignatureAlgo the Signature algorythm in JDK format
75 * @return the MessageDigest to use
76 */
77 public static MessageDigest getDigesterForJdkSignature(String jdkSignatureAlgo) {
78 if (jdkSignatureAlgo == null || jdkSignatureAlgo.equals("")) {
79 throw new IllegalArgumentException("jdkSignatureAlgo is NULL");
80 }
81 int pos = jdkSignatureAlgo.indexOf("with");
82 if (pos==-1) {
83 throw new JsdsiRuntimeException("Illegal jdkSignatureAlgo. No 'with' found: "+jdkSignatureAlgo);
84 }
85 String jdkName = jdkSignatureAlgo.substring(0, pos);
86 DigestAlgoEnum enum = DigestAlgoEnum.fromJdkSignature(jdkName);
87 return DigestUtils.getDigest(enum);
88 }
89
90
91 }
This page was automatically generated by Maven