63 if (token == null || token.isEmpty()) {
64 continue;
65 }
66
67 elements.add(token);
68 }
69 }
70 return elements;
71 }
72
73 /**
74 * Decompose the standard algorithm name into sub-elements.
75 * <p>
76 * For example, we need to decompose "SHA1WithRSA" into "SHA1" and "RSA"
77 * so that we can check the "SHA1" and "RSA" algorithm constraints
78 * separately.
79 * <p>
80 * Please override the method if need to support more name pattern.
81 */
82 public Set<String> decompose(String algorithm) {
83 if (algorithm == null || algorithm.length() == 0) {
84 return new HashSet<>();
85 }
86
87 Set<String> elements = decomposeImpl(algorithm);
88
89 // In Java standard algorithm name specification, for different
90 // purpose, the SHA-1 and SHA-2 algorithm names are different. For
91 // example, for MessageDigest, the standard name is "SHA-256", while
92 // for Signature, the digest algorithm component is "SHA256" for
93 // signature algorithm "SHA256withRSA". So we need to check both
94 // "SHA-256" and "SHA256" to make the right constraint checking.
95
96 // handle special name: SHA-1 and SHA1
97 if (elements.contains("SHA1") && !elements.contains("SHA-1")) {
98 elements.add("SHA-1");
99 }
100 if (elements.contains("SHA-1") && !elements.contains("SHA1")) {
101 elements.add("SHA1");
102 }
103
150 aliases = new String[] {algorithm};
151 }
152
153 return Arrays.asList(aliases);
154 }
155
156 private static void hasLoop(Set<String> elements, String find, String replace) {
157 if (elements.contains(find)) {
158 if (!elements.contains(replace)) {
159 elements.add(replace);
160 }
161 elements.remove(find);
162 }
163 }
164
165 /*
166 * This decomposes a standard name into sub-elements with a consistent
167 * message digest algorithm name to avoid overly complicated checking.
168 */
169 public static Set<String> decomposeOneHash(String algorithm) {
170 if (algorithm == null || algorithm.length() == 0) {
171 return new HashSet<>();
172 }
173
174 Set<String> elements = decomposeImpl(algorithm);
175
176 hasLoop(elements, "SHA-1", "SHA1");
177 hasLoop(elements, "SHA-224", "SHA224");
178 hasLoop(elements, "SHA-256", "SHA256");
179 hasLoop(elements, "SHA-384", "SHA384");
180 hasLoop(elements, "SHA-512", "SHA512");
181
182 return elements;
183 }
184
185 /*
186 * The provided message digest algorithm name will return a consistent
187 * naming scheme.
188 */
189 public static String hashName(String algorithm) {
190 return algorithm.replace("-", "");
|
63 if (token == null || token.isEmpty()) {
64 continue;
65 }
66
67 elements.add(token);
68 }
69 }
70 return elements;
71 }
72
73 /**
74 * Decompose the standard algorithm name into sub-elements.
75 * <p>
76 * For example, we need to decompose "SHA1WithRSA" into "SHA1" and "RSA"
77 * so that we can check the "SHA1" and "RSA" algorithm constraints
78 * separately.
79 * <p>
80 * Please override the method if need to support more name pattern.
81 */
82 public Set<String> decompose(String algorithm) {
83 if (algorithm == null || algorithm.isEmpty()) {
84 return new HashSet<>();
85 }
86
87 Set<String> elements = decomposeImpl(algorithm);
88
89 // In Java standard algorithm name specification, for different
90 // purpose, the SHA-1 and SHA-2 algorithm names are different. For
91 // example, for MessageDigest, the standard name is "SHA-256", while
92 // for Signature, the digest algorithm component is "SHA256" for
93 // signature algorithm "SHA256withRSA". So we need to check both
94 // "SHA-256" and "SHA256" to make the right constraint checking.
95
96 // handle special name: SHA-1 and SHA1
97 if (elements.contains("SHA1") && !elements.contains("SHA-1")) {
98 elements.add("SHA-1");
99 }
100 if (elements.contains("SHA-1") && !elements.contains("SHA1")) {
101 elements.add("SHA1");
102 }
103
150 aliases = new String[] {algorithm};
151 }
152
153 return Arrays.asList(aliases);
154 }
155
156 private static void hasLoop(Set<String> elements, String find, String replace) {
157 if (elements.contains(find)) {
158 if (!elements.contains(replace)) {
159 elements.add(replace);
160 }
161 elements.remove(find);
162 }
163 }
164
165 /*
166 * This decomposes a standard name into sub-elements with a consistent
167 * message digest algorithm name to avoid overly complicated checking.
168 */
169 public static Set<String> decomposeOneHash(String algorithm) {
170 if (algorithm == null || algorithm.isEmpty()) {
171 return new HashSet<>();
172 }
173
174 Set<String> elements = decomposeImpl(algorithm);
175
176 hasLoop(elements, "SHA-1", "SHA1");
177 hasLoop(elements, "SHA-224", "SHA224");
178 hasLoop(elements, "SHA-256", "SHA256");
179 hasLoop(elements, "SHA-384", "SHA384");
180 hasLoop(elements, "SHA-512", "SHA512");
181
182 return elements;
183 }
184
185 /*
186 * The provided message digest algorithm name will return a consistent
187 * naming scheme.
188 */
189 public static String hashName(String algorithm) {
190 return algorithm.replace("-", "");
|