1 /*
2 * Copyright �, Aegeus Technology Limited.
3 * All rights reserved.
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.2 $ $Date: 2004/11/08 12:08:08 $
19 */
20 public class DigestAlgoEnum 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 DigestAlgoEnum(String jdkName, String spkiName) {
32 super(jdkName, spkiName);
33 this.jdkSigName = jdkName;
34 }
35
36 /***
37 * @param jdkName
38 * @param spkiName
39 */
40 protected DigestAlgoEnum(String jdkName,
41 String spkiName, String jdkSigName) {
42 super(jdkName, spkiName);
43 this.jdkSigName = jdkSigName;
44 }
45
46 /***
47 * The MD2 message digest algorithm as defined in RFC 1319.
48 */
49 public static final DigestAlgoEnum MD2 = new DigestAlgoEnum("MD2", "md2");
50
51 /***
52 * The MD5 message digest algorithm as defined in RFC 1321.
53 */
54 public static final DigestAlgoEnum MD5 = new DigestAlgoEnum("MD5", "md5");
55
56 /***
57 * The Secure Hash Algorithm, as defined in Secure Hash Standard, NIST FIPS 180-1.
58 */
59 public static final DigestAlgoEnum SHA1 = new DigestAlgoEnum("SHA-1",
60 "sha1",
61 "SHA1");
62
63 /***
64 * 256-bit hash function intended to provide 128 bits of security against collision attacks.
65 */
66 public static final DigestAlgoEnum SHA256 = new DigestAlgoEnum("SHA-256",
67 "sha256",
68 "SHA256");
69
70 /***
71 * 384 byte SHA
72 */
73 public static final DigestAlgoEnum SHA384 = new DigestAlgoEnum("SHA-384",
74 "sha384",
75 "SHA384");
76
77 /***
78 * 512-bit hash function intended to provide 256 bits of security.
79 */
80 public static final DigestAlgoEnum SHA512 = new DigestAlgoEnum("SHA-512",
81 "sha512",
82 "SHA512");
83
84 private static final Map jdkMap = new HashMap();
85
86 private static final Map jdkSigMap = new HashMap();
87
88 private static final Map spkiMap = new HashMap();
89
90 static {
91 jdkMap.put("MD2", DigestAlgoEnum.MD2);
92 jdkMap.put("MD5", DigestAlgoEnum.MD5);
93 jdkMap.put("SHA-1", SHA1);
94 jdkMap.put("SHA-256", SHA256);
95 jdkMap.put("SHA-384", SHA384);
96 jdkMap.put("SHA-512", SHA512);
97
98 jdkSigMap.put("MD2", DigestAlgoEnum.MD2);
99 jdkSigMap.put("MD5", DigestAlgoEnum.MD5);
100 jdkSigMap.put("SHA1", SHA1);
101 jdkSigMap.put("SHA256", SHA256);
102 jdkSigMap.put("SHA384", SHA384);
103 jdkSigMap.put("SHA512", SHA512);
104
105 spkiMap.put("md2", MD2);
106 spkiMap.put("md5", MD5);
107 spkiMap.put("sha1", SHA1);
108 spkiMap.put("sha256", SHA256);
109 spkiMap.put("sha384", SHA384);
110 spkiMap.put("sha512", SHA512);
111 }
112
113 /***
114 * Given a JDK name for a MessageDigest algorythm, return its DigestAlgoEnum
115 * @param jdkName JDK name
116 * @return the DigestAlgoEnum
117 */
118 public static DigestAlgoEnum fromJdk(String jdkName) {
119 DigestAlgoEnum e = (DigestAlgoEnum) jdkMap.get(jdkName);
120 if (e==null) {
121 throw new JsdsiRuntimeException("DigestAlgoEnum not found for JDK name: "+jdkName);
122 }
123 return e;
124 }
125
126 /***
127 * Given a name for a MessageDigest algorythm from a JDK Signature, return its DigestAlgoEnum
128 * @param jdkName JDK name
129 * @return the DigestAlgoEnum
130 * @throws JsdsiRuntimeException on error
131 */
132 public static DigestAlgoEnum fromJdkSignature(String jdkName) {
133 DigestAlgoEnum e = (DigestAlgoEnum) jdkSigMap.get(jdkName);
134 if (e==null) {
135 throw new JsdsiRuntimeException("DigestAlgoEnum not found for JDK Signature name: "+jdkName);
136 }
137 return e;
138 }
139
140 /***
141 * Given a SPKI name for a MessageDigest algorythm, return its DigestAlgoEnum
142 * @param spkiName SPKI name
143 * @return the DigestAlgoEnum
144 * @throws JsdsiRuntimeException on error
145 */
146 public static DigestAlgoEnum fromSpki(String spkiName) {
147 DigestAlgoEnum e = (DigestAlgoEnum) spkiMap.get(spkiName);
148 if (e==null) {
149 throw new JsdsiRuntimeException("DigestAlgoEnum not found for SPKI name: "+spkiName);
150 }
151 return e;
152 }
153
154 /***
155 * Returns the name of this digest when part of a JDK Signature algorythm
156 * @return the name
157 */
158 public String jdkSignatureName() {
159 return this.jdkSigName;
160 }
161
162 }
This page was automatically generated by Maven