1
2
3
4
5 package jsdsi.util;
6
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import jsdsi.JsdsiRuntimeException;
11
12
13 /***
14 * <p>Experimental class and as such may be removed without warning.</p>
15 * Enum class for MessageDigest algorythms.
16 *
17 * @author Sean Radford
18 * @version $Revision: 1.4 $ $Date: 2004/06/09 16:34:41 $
19 */
20 public class DigestEnum extends AlgorithmEnum {
21
22 /***
23 * The name of this Digest when part of a JDK Signautre algorithm
24 */
25 private String jdkSigName;
26
27 /***
28 * @param jdkName
29 * @param spkiName
30 */
31 protected DigestEnum(String jdkName, String spkiName) {
32 super(jdkName, spkiName);
33 this.jdkSigName = jdkName;
34 }
35
36 /***
37 * @param jdkName
38 * @param spkiName
39 */
40 protected DigestEnum(String jdkName, String spkiName, String jdkSigName) {
41 super(jdkName, spkiName);
42 this.jdkSigName = jdkSigName;
43 }
44
45 /***
46 * The MD2 message digest algorithm as defined in RFC 1319.
47 */
48 public static final DigestEnum MD2 = new DigestEnum("MD2", "md2");
49
50 /***
51 * The MD5 message digest algorithm as defined in RFC 1321.
52 */
53 public static final DigestEnum MD5 = new DigestEnum("MD5", "md5");
54
55 /***
56 * The Secure Hash Algorithm, as defined in Secure Hash Standard, NIST FIPS 180-1.
57 */
58 public static final DigestEnum SHA1 = new DigestEnum("SHA-1", "sha1", "SHA1");
59
60 /***
61 * 256-bit hash function intended to provide 128 bits of security against collision attacks.
62 */
63 public static final DigestEnum SHA256 = new DigestEnum("SHA-256", "sha256", "SHA256");
64
65 /***
66 * 384 byte SHA
67 */
68 public static final DigestEnum SHA384 = new DigestEnum("SHA-384", "sha384", "SHA384");
69
70 /***
71 * 512-bit hash function intended to provide 256 bits of security.
72 */
73 public static final DigestEnum SHA512 = new DigestEnum("SHA-512", "sha512", "SHA512");
74
75 private static final Map jdkMap = new HashMap();
76
77 private static final Map jdkSigMap = new HashMap();
78
79 private static final Map spkiMap = new HashMap();
80
81 static {
82 jdkMap.put("MD2", DigestEnum.MD2);
83 jdkMap.put("MD5", DigestEnum.MD5);
84 jdkMap.put("SHA-1", SHA1);
85 jdkMap.put("SHA-256", SHA256);
86 jdkMap.put("SHA-384", SHA384);
87 jdkMap.put("SHA-512", SHA512);
88
89 jdkSigMap.put("MD2", DigestEnum.MD2);
90 jdkSigMap.put("MD5", DigestEnum.MD5);
91 jdkSigMap.put("SHA1", SHA1);
92 jdkSigMap.put("SHA256", SHA256);
93 jdkSigMap.put("SHA384", SHA384);
94 jdkSigMap.put("SHA512", SHA512);
95
96 spkiMap.put("md2", MD2);
97 spkiMap.put("md5", MD5);
98 spkiMap.put("sha1", SHA1);
99 spkiMap.put("sha256", SHA256);
100 spkiMap.put("sha384", SHA384);
101 spkiMap.put("sha512", SHA512);
102 }
103
104 /***
105 * Given a JDK name for a MessageDigest algorythm, return its DigestEnum
106 * @param jdkName JDK name
107 * @return the DigestEnum
108 */
109 public static DigestEnum fromJdk(String jdkName) {
110 DigestEnum e = (DigestEnum) jdkMap.get(jdkName);
111 if (e==null) {
112 throw new JsdsiRuntimeException("DigestEnum not found for JDK name: "+jdkName);
113 }
114 return e;
115 }
116
117 /***
118 * Given a name for a MessageDigest algorythm from a JDK Signature, return its DigestEnum
119 * @param jdkName JDK name
120 * @return the DigestEnum
121 */
122 public static DigestEnum fromJdkSignature(String jdkName) {
123 DigestEnum e = (DigestEnum) jdkSigMap.get(jdkName);
124 if (e==null) {
125 throw new JsdsiRuntimeException("DigestEnum not found for JDK Signature name: "+jdkName);
126 }
127 return e;
128 }
129
130 /***
131 * Given a SPKI name for a MessageDigest algorythm, return its DigestEnum
132 * @param spkiName SPKI name
133 * @return the DigestEnum
134 */
135 public static DigestEnum fromSpki(String spkiName) {
136 DigestEnum e = (DigestEnum) spkiMap.get(spkiName);
137 if (e==null) {
138 throw new JsdsiRuntimeException("DigestEnum not found for SPKI name: "+spkiName);
139 }
140 return e;
141 }
142
143 /***
144 * Returns the name of this digest when part of a JDK Signature algorythm
145 * @return the name
146 */
147 public String jdkSignatureName() {
148 return this.jdkSigName;
149 }
150
151 }