View Javadoc

1   /*
2    * Copyright ©, Aegeus Technology Limited.
3    * All rights reserved.
4    */
5   package jsdsi.util;
6   
7   
8   import jsdsi.JsdsiRuntimeException;
9   
10  
11  /***
12   * <p>Experimental class and as such may be removed without warning.</p>
13   * Enum class for Signature algorythms.
14   * 
15   * @author Sean Radford
16   * @version $Revision: 1.5 $ $Date: 2004/06/09 16:34:41 $
17   */
18  public class SignatureEnum extends AlgorithmEnum {
19  
20      private DigestEnum digest;
21      
22      private KeyEnum key;
23      
24      /***
25       * @param jdkName
26       * @param spkiName
27       */
28      protected SignatureEnum(DigestEnum digest, KeyEnum key) {
29          super(Algorithms.jdkSignatureName(digest, key), Algorithms.spkiSignatureName(digest, key));
30          this.digest = digest;
31          this.key = key;
32      }
33  
34      /***
35       * Return the DigestEnum for this SignatureEnum
36       * @return the DigestEnum
37       */
38      public DigestEnum getDigestEnum() {
39          return this.digest;
40      }
41      
42      /***
43       * Return the KeyEnum for this SignatureEnum
44       * @return the KeyEnum
45       */
46      public KeyEnum getKeyEnum() {
47          return this.key;
48      }
49      
50      /***
51       * Create a SignatureEnum
52       * @param digest the DigestEnum
53       * @param key the KeyEnum
54       * @return the SignatureEnum
55       */
56      public static SignatureEnum create(DigestEnum digest, KeyEnum key) {
57          return new SignatureEnum(digest, key);
58      }
59      
60      /***
61       * Given a JDK name for a Signature algorythm, return its SignatureEnum
62       * @param jdkName JDK name
63       * @return the SignatureEnum
64       */
65      public static SignatureEnum fromJdk(String jdkName) {
66          String[] bits = jdkName.split("with");
67          if (bits.length!=2) {
68              throw new JsdsiRuntimeException("Illegal JDK SignatureAlgo: "+jdkName);
69          }
70          DigestEnum digest = DigestEnum.fromJdkSignature( bits[0] );
71          KeyEnum key = KeyEnum.fromJdk( bits[1] );
72          return new SignatureEnum(digest, key);
73      }
74      
75      /***
76       * Given a SPKI name for a Signature algorythm, return its SignatureEnum
77       * @param spkiName SPKI name
78       * @return the SignatureEnum
79       */
80      public static SignatureEnum fromSpki(String spkiName) {
81          int pos = spkiName.lastIndexOf("-");
82          if (pos==-1) {
83              throw new JsdsiRuntimeException("Illegal SPKI SignatureAlgo: "+spkiName);
84          }
85          String s = spkiName.substring(pos+1);
86          DigestEnum digest = DigestEnum.fromSpki( s );
87          s = spkiName.substring(0, pos-1);
88          KeyEnum key = KeyEnum.fromSpki( s );
89          return new SignatureEnum(digest, key);
90      }
91      
92  }